-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.js
More file actions
80 lines (73 loc) · 1.95 KB
/
lib.js
File metadata and controls
80 lines (73 loc) · 1.95 KB
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
function wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/**
* Adapted from FoxScroller
*
* @param PXperS
* @returns {number[]}
*/
function get_pixel_speed(PXperS) {
var deltaPixel, deltaTime;
if (PXperS < 60) {
deltaPixel = 1;
deltaTime = 1000.0 / PXperS;
} else if (PXperS % 60 === 0) {
deltaPixel = PXperS / 60;
deltaTime = 1000.0 / 60;
} else {
deltaPixel = (PXperS - PXperS % 60) / 60 + 1;
deltaTime = 1000.0 / (PXperS / deltaPixel);
}
return [deltaPixel, deltaTime];
}
async function set_prop(prop, value) {
const key = `xscroller-${prop}`;
const obj = {}
obj[key] = value;
return await browser.storage.local.set(obj);
}
async function get_prop(prop, default_value=undefined) {
const key = `xscroller-${prop}`;
const response = await browser.storage.local.get(key);
if(!response || Object.keys(response).length === 0) {
return default_value;
} else {
return response[key];
}
}
async function clear_log() {
await set_prop('log', []);
}
function write_log(text) {
browser.runtime.sendMessage({action: 'log', log: text});
}
/**
* Check if two arrays of log messages are equal
*
* @param array1
* @param array2
* @returns boolean
*/
function logs_equal(array1, array2) {
if(typeof array1 !== typeof array2) {
return false;
}
return (array1.length === array2.length && array1.slice().every(function(value, index) {
return value.text === array2[index].text;
}));
}
/**
* Get tab ID
*
* Content scripts cannot read tab info, but they can send a message to the
* background script which will tell us the ID of the tab the message was
* send from in return.
*
* @returns {Promise<any>} Promise that will resolve to return the tab ID
*/
async function get_tab_id() {
return browser.runtime.sendMessage({
action: 'get-tab-id'
}).then((response) => { return response.id; })
}