Skip to content

Commit

Permalink
fix: takanakahiko#9 Conform to standard lint rule
Browse files Browse the repository at this point in the history
  • Loading branch information
uneco committed Jan 16, 2019
1 parent 0e7cf59 commit 0485366
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 156 deletions.
30 changes: 17 additions & 13 deletions app/scripts/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,30 @@
import {
getSessionInfo,
openLoginForm,
uploadEmoji
uploadEmoji,
} from './sub_modules/slack'

const onClickContextMenus = async (info: chrome.contextMenus.OnClickData, tab: chrome.tabs.Tab) => {
if( !info.srcUrl ) return;
const image_url = info.srcUrl;
const team_name = prompt(chrome.i18n.getMessage('promptTeamName')) || '';
const emoji_name = prompt(chrome.i18n.getMessage('promptEmojiName')) || '';
const session_info = await getSessionInfo(team_name)
if(!session_info) return openLoginForm(team_name)
await uploadEmoji(team_name, emoji_name , image_url, session_info)
if (!info.srcUrl) {
return
}
const imageUrl = info.srcUrl
const teamName = prompt(chrome.i18n.getMessage('promptTeamName')) || ''
const emojiName = prompt(chrome.i18n.getMessage('promptEmojiName')) || ''
const sessionInfo = await getSessionInfo(teamName)
if (!sessionInfo) {
return openLoginForm(teamName)
}
await uploadEmoji(teamName, emojiName , imageUrl, sessionInfo)
}

chrome.runtime.onInstalled.addListener((details) => {
console.log('previousVersion', details.previousVersion);
console.log('previousVersion', details.previousVersion)
chrome.contextMenus.create({
title: chrome.i18n.getMessage('contextMenuTitle'),
contexts: ['image'],
onclick: onClickContextMenus
});
});
onclick: onClickContextMenus,
})
})

console.log(`backgound`);
console.log('backgound')
2 changes: 1 addition & 1 deletion app/scripts/contentscript.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Enable chromereload by uncommenting this line:
// import 'chromereload/devonly'

console.log(`content script`);
console.log('content script')
2 changes: 1 addition & 1 deletion app/scripts/options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Enable chromereload by uncommenting this line:
// import 'chromereload/devonly'

console.log(`options`);
console.log('options')
2 changes: 1 addition & 1 deletion app/scripts/popup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Enable chromereload by uncommenting this line:
// import 'chromereload/devonly'

console.log(`popup`);
console.log('popup')
98 changes: 54 additions & 44 deletions app/scripts/sub_modules/slack.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,65 @@
import { httpGet, httpPostForm, getBase64Image } from './util';
import { httpGet, httpPostForm, getBase64Image } from './util'

interface SessionInfo {
api_token: string;
version_uid: string;
version_ts: string;
api_token: string
version_uid: string
version_ts: string
}

export const getSessionInfo = async(team_name: string): Promise<SessionInfo | undefined> => {
const emoji_customize_url = 'https://' + team_name + '.slack.com/customize/emoji';
const ret = await httpGet(emoji_customize_url);
export const getSessionInfo = async (teamName: string): Promise<SessionInfo | undefined> => {
const emojiCustomizeUrl = 'https://' + teamName + '.slack.com/customize/emoji'
const ret = await httpGet(emojiCustomizeUrl)

if (ret.responseURL !== emoji_customize_url) return;
if (ret.responseUrl !== emojiCustomizeUrl) {
return
}

const api_token_matches = ret.responseText.match(/api_token: "(.+?)"/);
if (!api_token_matches || !api_token_matches[1]) return;
const apiTokenMatches = ret.responseText.match(/api_token: "(.+?)"/)
if (!apiTokenMatches || !apiTokenMatches[1]) {
return
}

const version_uid_matches = ret.responseText.match(/version_uid: "(.+?)"/);
if (!version_uid_matches || !version_uid_matches[1]) return;
const versionUidMatches = ret.responseText.match(/version_uid: "(.+?)"/)
if (!versionUidMatches || !versionUidMatches[1]) {
return
}

const version_ts_matches = ret.responseText.match(/version_ts: "(.+?)"/);
if (!version_ts_matches || !version_ts_matches[1]) return;
const versionTsMatches = ret.responseText.match(/version_ts: "(.+?)"/)
if (!versionTsMatches || !versionTsMatches[1]) {
return
}

return {
api_token: api_token_matches[1],
version_uid: version_uid_matches[1],
version_ts: version_ts_matches[1],
};
};

export const openLoginForm = (team_name: string) => {
alert('Please login to https://' + team_name + '.slack.com');
api_token: apiTokenMatches[1],
version_uid: versionUidMatches[1],
version_ts: versionTsMatches[1],
}
}

export const openLoginForm = (teamName: string) => {
alert('Please login to https://' + teamName + '.slack.com')
chrome.tabs.create({
url: 'https://' + team_name + '.slack.com'
});
};

const getXId = (session_info: SessionInfo): string => {
const version_uid_top = session_info.version_uid.substr(0, 8);
return `${version_uid_top}-${Date.now() / 1000}`;
};

export const uploadEmoji = async(team_name: string, emoji_name: string, image_url: string, session_info: SessionInfo) => {
const emoji_customize_url = 'https://' + team_name + '.slack.com/api/emoji.add?_x_id=' + getXId(session_info);
const form_data = {
'mode': 'data',
'name': emoji_name,
'image': await getBase64Image(image_url),
'token': session_info.api_token
};
const header = { };
const ret = await httpPostForm(emoji_customize_url, form_data, header);
if ( JSON.parse(ret.responseText).ok ) return true;
return false;
};
url: 'https://' + teamName + '.slack.com',
})
}

const getXId = (sessionInfo: SessionInfo): string => {
const versionUidTop = sessionInfo.version_uid.substr(0, 8)
return `${versionUidTop}-${Date.now() / 1000}`
}

export const uploadEmoji = async (teamName: string, emojiName: string, imageUrl: string, sessionInfo: SessionInfo) => {
const emojiCustomizeUrl = 'https://' + teamName + '.slack.com/api/emoji.add?_x_id=' + getXId(sessionInfo)
const formData = {
mode: 'data',
name: emojiName,
image: await getBase64Image(imageUrl),
token: sessionInfo.api_token,
}
const header = {}
const ret = await httpPostForm(emojiCustomizeUrl, formData, header)
if (JSON.parse(ret.responseText).ok) {
return true
}
return false
}
85 changes: 43 additions & 42 deletions app/scripts/sub_modules/util.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
export const httpGet = async(
url: string,
header: { [s: string]: string } = {}
): Promise<XMLHttpRequest> => {
return new Promise<XMLHttpRequest>((resolve, _reject) => {
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function (event) {
if (xhr.readyState !== 4) return;
return resolve(xhr); // OK
};
xhr.open('GET', url, true); // Async
Object.keys(header).forEach(function (key) {
xhr.setRequestHeader( key, header[key] );
});
xhr.withCredentials = true;
xhr.send();
});
};
export const httpGet = async (url: string, header: { [s: string]: string } = {}): Promise<XMLHttpRequest> => {
return new Promise<XMLHttpRequest>((resolve, reject) => {
const xhr = new XMLHttpRequest()
xhr.onreadystatechange = (event) => {
if (xhr.readyState !== 4) {
return
}
return resolve(xhr) // OK
}
xhr.open('GET', url, true) // Async
Object.keys(header).forEach((key) => {
xhr.setRequestHeader(key, header[key])
})
xhr.withCredentials = true
xhr.send()
})
}

export const httpPostForm = async(
export const httpPostForm = async (
url: string,
form_data: { [s: string]: string | Blob },
header: { [s: string]: string } = {}
formData: { [s: string]: string | Blob },
header: { [s: string]: string } = {},
): Promise<XMLHttpRequest> => {
return new Promise<XMLHttpRequest>((resolve, _reject) => {
const xhr = new XMLHttpRequest();
const form = new FormData();
Object.keys(form_data).forEach(function (key) {
form.append(key, form_data[key]);
});
xhr.onreadystatechange = function (event) {
if (xhr.readyState !== 4) return;
return resolve(xhr); // OK
};
xhr.open('POST', url, true); // Async
Object.keys(header).forEach(function (key) {
xhr.setRequestHeader( key, header[key] );
});
xhr.withCredentials = true;
xhr.send(form);
});
};
return new Promise<XMLHttpRequest>((resolve, reject) => {
const xhr = new XMLHttpRequest()
const form = new FormData()
Object.keys(formData).forEach((key) => {
form.append(key, formData[key])
})
xhr.onreadystatechange = (event) => {
if (xhr.readyState !== 4) {
return
}
return resolve(xhr) // OK
}
xhr.open('POST', url, true) // Async
Object.keys(header).forEach((key) => {
xhr.setRequestHeader(key, header[key])
})
xhr.withCredentials = true
xhr.send(form)
})
}

export const getBase64Image = async(url: string): Promise<Blob> => {
return await fetch(url).then(r => r.blob());
};
export const getBase64Image = async (url: string): Promise<Blob> => {
return await fetch(url).then((r) => r.blob())
}
62 changes: 8 additions & 54 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -1,58 +1,12 @@
{
"extends": "tslint:recommended",
"rules": {
"class-name": true,
"comment-format": [
true,
"check-space"
],
"indent": [
true,
"spaces"
],
"no-duplicate-variable": true,
"no-eval": true,
"no-internal-module": true,
"no-trailing-whitespace": true,
"no-unsafe-finally": true,
"no-var-keyword": true,
"one-line": [
true,
"check-open-brace",
"check-whitespace"
],
"quotemark": [
true,
"single"
],
"semicolon": [
true,
"always"
],
"triple-equals": [
true,
"allow-null-check"
],
"typedef-whitespace": [
true,
{
"call-signature": "nospace",
"index-signature": "nospace",
"parameter": "nospace",
"property-declaration": "nospace",
"variable-declaration": "nospace"
}
],
"variable-name": [
true,
"ban-keywords"
],
"whitespace": [
true,
"check-branch",
"check-decl",
"check-operator",
"check-separator",
"check-type"
]
"ordered-imports": false,
"member-ordering": false,
"object-literal-sort-keys": false,
"interface-name": false,
"semicolon": [true, "never"],
"quotemark": [true, "single"],
"no-console": false
}
}

0 comments on commit 0485366

Please sign in to comment.