-
Notifications
You must be signed in to change notification settings - Fork 11
/
common.js
128 lines (104 loc) · 3.28 KB
/
common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const utils = {
/**
* waits for a specified number of milisconds
* @param {number} ms time to wait in miliseconds
* @returns {Promise} a Promise that will be fulfilled after the wait time is over
*/
wait: ms => new Promise(resolve => setTimeout(resolve, ms)),
// regular expression that parses tab options and title from bookmark title:
bmTitleParserRE: /^(\[(pinned)?\]\s)?(.*)$/,
isBMFolder: bm => bm.type === "folder" || !bm.url,
/** a filter function for urls that can be opened in a new tab via the API */
urlFilter: url => url.startsWith("http") || url.startsWith("view-source:"),
/**
* creates a title for a tab that encodes the pinned state
* this is as the bookmark title
* @param {tabs.Tab} tab the tab
* @returns {string} returns the bookmark title
*/
generateTabBMTitle: tab => (tab.pinned ? "[pinned] " : "") + tab.title.trim(),
containsEmptyTab: tabs => tabs.some(tab => tab.url === "about:newtab"),
/**
* @param {object} options optional query filters (currentWindow and pinned), falls back to user config
* @returns {Promise} the promise returned by the browser.tabs.query API
*/
getTabs: (options = {}) => {
let queryInfo = {};
if (options.currentWindow !== undefined) {
queryInfo.currentWindow = options.currentWindow;
}
let promise;
if (options.pinned === undefined) {
// fallback to the default setting (configured in the extension options)
promise = browser.storage.local.get("ignore-pinned").then(data => {
if (data["ignore-pinned"] || data["ignore-pinned"] === undefined) {
// exclude pinned tabs (default if "ignore-pinned" is not set)
queryInfo.pinned = false;
}
});
} else {
queryInfo.pinned = options.pinned;
promise = Promise.resolve();
}
return promise.then(() => browser.tabs.query(queryInfo));
},
getURLSearchParams: () => new URLSearchParams(document.location.search.substring(1)),
createHTMLElement: (tagName, attrs, classes, content) => {
let element = document.createElement(tagName);
// add attributes
Object.getOwnPropertyNames(attrs).forEach(k => {
element.setAttribute(k, attrs[k]);
});
// add classes
classes.forEach(c => { element.classList.add(c); });
if (content) {
element.innerText = content;
}
return element;
},
/* returns a promise that gets resolved if
* one of the given promises is resolved
* @param promises - array of promises
*/
promiseOne(promises) {
let resolved = false;
return new Promise((resolve, reject) => {
promises.forEach(p => {
p.then(() => {
if (!resolved) {
resolved = true;
resolve();
}
}, reject);
})
});
},
getActiveTab() {
return browser.tabs.query({
active: true,
currentWindow: true
}).then(tabs => tabs[0]);
},
waitUntilPageIsLoaded() {
return new Promise(resolve => {
window.addEventListener("load", resolve);
});
},
setBrowserActionIcon(icon) {
let map = new Map();
map.set("dark", "tabs-aside-16-dark.svg");
map.set("light", "tabs-aside-16-light.svg");
map.set("dynamic", "tabs-aside-16.svg");
if(map.has(icon)) {
let iconPath = "../icons/" + map.get(icon);
browser.browserAction.setIcon({
path: {
"16": iconPath,
"32": iconPath
}
}).catch(e => console.error(""+e));
} else {
console.error("[TA] ba-icon error");
}
}
}