forked from takanakahiko/slack-emoji-meister
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: takanakahiko#9 Conform to standard lint rule
- Loading branch information
Showing
7 changed files
with
125 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |