Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
278 additions
and
284 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// @ts-check | ||
|
||
// Make a promise-returning versions of the chrome API method. | ||
export function makePromiseVersion(theThis, theMethod, theMethodName) { | ||
return (...theArguments) => { | ||
return new Promise((resolve, reject) => { | ||
theMethod.apply( | ||
theThis, | ||
theArguments.concat((result) => { | ||
return chrome.runtime.lastError == null | ||
? resolve(result) | ||
: reject({ | ||
function: theMethodName, | ||
arguments: theArguments, | ||
error: chrome.runtime.lastError, | ||
}); | ||
}), | ||
); | ||
}); | ||
}; | ||
} | ||
|
||
// Make a promise-returning versions of the chrome API method, | ||
// given a string like "chrome.windows.create". | ||
export function assignPromiseVersionByMethodName(dstRoot, fullMethodName) { | ||
// Example: let fullMethodName = 'chrome.some.thing.tabs.create' | ||
let methodParentParts = fullMethodName.split('.'); | ||
// Now methodParentParts == ['chrome', 'some', 'thing', 'tabs', 'create'] | ||
|
||
let [methodName] = methodParentParts.splice(-1); | ||
// Now methodParentParts == ['chrome', 'some', 'thing', 'tabs'] | ||
// Now methodName == 'create' | ||
|
||
let srcParent = window; | ||
for (let methodParentPart of methodParentParts) { | ||
srcParent = srcParent[methodParentPart]; | ||
} | ||
// Now srcParent == window.chrome.some.thing.tabs | ||
// srcMethod = srcParent[methodName]; // TODO: is this line necessary? | ||
|
||
let dstParent = dstRoot; | ||
for (let methodParentPart of methodParentParts.slice(1)) { | ||
if (dstParent[methodParentPart] == null) dstParent[methodParentPart] = {}; | ||
dstParent = dstParent[methodParentPart]; | ||
} | ||
// Now dstParent == dstRoot.some.thing.tabs | ||
dstParent[methodName] = this.makePromiseVersion( | ||
srcParent, | ||
srcParent[methodName], | ||
fullMethodName, | ||
); | ||
} | ||
|
||
export function getPromiseVersions(fullMethodNames) { | ||
let dstRoot = {}; | ||
for (let fullMethodName of fullMethodNames) { | ||
this.assignPromiseVersionByMethodName(dstRoot, fullMethodName); | ||
} | ||
return dstRoot; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// @ts-check | ||
|
||
// https://gist.github.com/josh/8177583 | ||
export const Ready = new Promise((resolve) => { | ||
if (document.readyState === 'complete') { | ||
resolve(); | ||
} else { | ||
let onReady = () => { | ||
resolve(); | ||
document.removeEventListener('DOMContentLoaded', onReady, true); | ||
window.removeEventListener('load', onReady, true); | ||
}; | ||
document.addEventListener('DOMContentLoaded', onReady, true); | ||
window.addEventListener('load', onReady, true); | ||
} | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// @ts-check | ||
|
||
// Return the item in items with the largest getKeyByItem(item). | ||
export function max(items, getKeyByItem) { | ||
return items.reduce((item1, item2) => { | ||
return getKeyByItem(item1) > getKeyByItem(item2) ? item1 : item2; | ||
}); | ||
} | ||
|
||
// Zip that works with iterables. | ||
// From http://stackoverflow.com/a/10284006/782045 | ||
export function zip(rows) { | ||
return rows[0].map((_, c) => { | ||
return rows.map((row) => row[c]); | ||
}); | ||
} | ||
|
||
// Like zip, but with objects. | ||
// Example: | ||
// - input: {num: [1, 2, 3], str: ["a", "b", "c"]} | ||
// - output: [{num: 1, str: "a"}, {num: 2, str: "b"}, {num: 3, str: "c"}] | ||
export function mapzip(unzipped) { | ||
let zipped = []; | ||
for (let key of Object.keys(unzipped)) { | ||
let vals = unzipped[key]; | ||
for (let i = 0; i < vals.length; i++) { | ||
zipped[i] = zipped[i] || {}; | ||
zipped[i][key] = vals[i]; | ||
} | ||
} | ||
return zipped; | ||
} |
Oops, something went wrong.