-
Notifications
You must be signed in to change notification settings - Fork 0
/
onupdate.js
128 lines (102 loc) · 3.14 KB
/
onupdate.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
/**
* Main logic: initializes DevRuler extension on page load.
* Author: vince damiani 2019;
*/
/**
* There was an error executing the script.
* Display the popup's error message, and hide the normal UI.
* @param {string} error
*/
function reportExecuteScriptError(error) {
// document.querySelector("#popup-content").classList.add("hidden");
// document.querySelector("#error-content").classList.remove("hidden");
console.error(`Failed to execute DevRuler content script: ${error.message}`);
}
/**
* Just log the error to the console.
* @param {string} error
*/
function onError(error) {
console.error(`Could not show DevRuler: ${error}`);
}
/**
* Inject proper CSS and sends the appropriate command to
* the receiver instanciated by the `devruler.js` script.
* @param {object} tab involved, from storage settings
* @param {string} id of the tab
*/
function updateOnLoad(tab, id) {
if (tab.state) {
browser.tabs.insertCSS({file: '/css/ruler.css'}).then(() => {
browser.tabs.sendMessage(id, {
command: "showRuler"
}).catch(onError);
});
} else {
browser.tabs.removeCSS({file: '/css/ruler.css'}).then(() => {
browser.tabs.sendMessage(id, {
command: "resetRuler",
}).catch(onError);
});
}
if (tab.bsbState) {
browser.tabs.insertCSS({file: '/css/breakpoints.css'}).then(() => {
browser.tabs.sendMessage(id, {
command: "showBreakpoints"
}).catch(onError);
});
} else {
browser.tabs.removeCSS({file: '/css/breakpoints.css'}).then(() => {
browser.tabs.sendMessage(id, {
command: "resetBreakpoints",
}).catch(onError);
});
}
if (tab.state || tab.bsbState) {
browser.tabs.sendMessage(id, {
command: "updateStyles",
}).catch(onError);
}
}
/**
* Removes closed tabs from settings.
* @param {object} settings
*/
function resetSettingsTabs(settings) {
browser.tabs.query({})
.then(function(tabs){
settings.tabs.forEach(function(v, k) {
if (tabs.find(function(elm){ return elm.id == k }) == undefined) {
settings.tabs.delete(k);
}
});
browser.storage.local.set({ tabs: settings.tabs });
});
}
/**
* Initialize DevRuler at page load time (status = 'complete'),
* executing `devruler.js` script which is responsable to
* instanciate the command receiver.
* @param {string} id of the current tab
* @param {object} info of the current tab
* @param {object} current tab
*/
function initializeRuler(id, info, tab) {
if (info.status == "complete") {
browser.tabs.executeScript({file: "/content_scripts/devruler.js"})
.then(function() {
const gettingStoredSettings = browser.storage.local.get();
gettingStoredSettings.then(function(settings) {
resetSettingsTabs(settings);
if (settings.tabs && (settings.tabs instanceof Map)) {
if (settings.tabs.has(id)) {
updateOnLoad(settings.tabs.get(id), id);
}
} else {
browser.storage.local.set({ tabs: new Map() });
}
}).catch(onError);
}).catch(reportExecuteScriptError)
}
}
browser.tabs.onUpdated.addListener(initializeRuler);