OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 "use strict"; | 18 "use strict"; |
19 | 19 |
20 let tab = null; | 20 let tab = null; |
21 | 21 |
22 function getPref(key, callback) | 22 function getPref(key, callback) |
23 { | 23 { |
24 chrome.runtime.sendMessage({type: "prefs.get", key}, callback); | 24 browser.runtime.sendMessage({type: "prefs.get", key}, callback); |
25 } | 25 } |
26 | 26 |
27 function togglePref(key, callback) | 27 function togglePref(key, callback) |
28 { | 28 { |
29 chrome.runtime.sendMessage({type: "prefs.toggle", key}, callback); | 29 browser.runtime.sendMessage({type: "prefs.toggle", key}, callback); |
30 } | 30 } |
31 | 31 |
32 function isPageWhitelisted(callback) | 32 function isPageWhitelisted(callback) |
33 { | 33 { |
34 chrome.runtime.sendMessage({type: "filters.isWhitelisted", tab}, callback); | 34 browser.runtime.sendMessage({type: "filters.isWhitelisted", tab}, callback); |
35 } | 35 } |
36 | 36 |
37 function whenPageReady() | 37 function whenPageReady() |
38 { | 38 { |
39 return new Promise(resolve => | 39 return new Promise(resolve => |
40 { | 40 { |
41 function onMessage(message, sender) | 41 function onMessage(message, sender) |
42 { | 42 { |
43 if (message.type == "composer.ready" && sender.page && | 43 if (message.type == "composer.ready" && sender.page && |
44 sender.page.id == tab.id) | 44 sender.page.id == tab.id) |
45 { | 45 { |
46 chrome.runtime.onMessage.removeListener(onMessage); | 46 browser.runtime.onMessage.removeListener(onMessage); |
47 resolve(); | 47 resolve(); |
48 } | 48 } |
49 } | 49 } |
50 | 50 |
51 chrome.runtime.onMessage.addListener(onMessage); | 51 browser.runtime.onMessage.addListener(onMessage); |
52 | 52 |
53 chrome.runtime.sendMessage({ | 53 browser.runtime.sendMessage({ |
54 type: "composer.isPageReady", | 54 type: "composer.isPageReady", |
55 pageId: tab.id | 55 pageId: tab.id |
56 }, | 56 }, |
57 ready => | 57 ready => |
58 { | 58 { |
59 if (ready) | 59 if (ready) |
60 { | 60 { |
61 chrome.runtime.onMessage.removeListener(onMessage); | 61 browser.runtime.onMessage.removeListener(onMessage); |
62 resolve(); | 62 resolve(); |
63 } | 63 } |
64 }); | 64 }); |
65 }); | 65 }); |
66 } | 66 } |
67 | 67 |
68 function onLoad() | 68 function onLoad() |
69 { | 69 { |
70 chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => | 70 browser.tabs.query({active: true, lastFocusedWindow: true}, tabs => |
71 { | 71 { |
72 if (tabs.length > 0) | 72 if (tabs.length > 0) |
73 tab = {id: tabs[0].id, url: tabs[0].url}; | 73 tab = {id: tabs[0].id, url: tabs[0].url}; |
74 | 74 |
75 let urlProtocol = tab && tab.url && new URL(tab.url).protocol; | 75 let urlProtocol = tab && tab.url && new URL(tab.url).protocol; |
76 | 76 |
77 // Mark page as 'local' to hide non-relevant elements | 77 // Mark page as 'local' to hide non-relevant elements |
78 if (urlProtocol != "http:" && urlProtocol != "https:") | 78 if (urlProtocol != "http:" && urlProtocol != "https:") |
79 { | 79 { |
80 document.body.classList.add("local"); | 80 document.body.classList.add("local"); |
(...skipping 12 matching lines...) Expand all Loading... |
93 // whether it has cached filters. If so, ask the user whether she | 93 // whether it has cached filters. If so, ask the user whether she |
94 // wants those filters. Otherwise, we are in default state. | 94 // wants those filters. Otherwise, we are in default state. |
95 if (tab) | 95 if (tab) |
96 { | 96 { |
97 isPageWhitelisted(whitelisted => | 97 isPageWhitelisted(whitelisted => |
98 { | 98 { |
99 if (whitelisted) | 99 if (whitelisted) |
100 document.body.classList.add("disabled"); | 100 document.body.classList.add("disabled"); |
101 }); | 101 }); |
102 | 102 |
103 chrome.tabs.sendMessage(tab.id, { | 103 browser.tabs.sendMessage(tab.id, { |
104 type: "composer.content.getState" | 104 type: "composer.content.getState" |
105 }, | 105 }, |
106 response => | 106 response => |
107 { | 107 { |
108 if (response && response.active) | 108 if (response && response.active) |
109 document.body.classList.add("clickhide-active"); | 109 document.body.classList.add("clickhide-active"); |
110 }); | 110 }); |
111 } | 111 } |
112 }); | 112 }); |
113 | 113 |
114 document.getElementById("enabled").addEventListener( | 114 document.getElementById("enabled").addEventListener( |
115 "click", toggleEnabled, false | 115 "click", toggleEnabled, false |
116 ); | 116 ); |
117 document.getElementById("clickhide").addEventListener( | 117 document.getElementById("clickhide").addEventListener( |
118 "click", activateClickHide, false | 118 "click", activateClickHide, false |
119 ); | 119 ); |
120 document.getElementById("clickhide-cancel").addEventListener( | 120 document.getElementById("clickhide-cancel").addEventListener( |
121 "click", cancelClickHide, false | 121 "click", cancelClickHide, false |
122 ); | 122 ); |
123 document.getElementById("options").addEventListener("click", () => | 123 document.getElementById("options").addEventListener("click", () => |
124 { | 124 { |
125 chrome.runtime.sendMessage({type: "app.open", what: "options"}); | 125 browser.runtime.sendMessage({type: "app.open", what: "options"}); |
126 window.close(); | 126 window.close(); |
127 }, false); | 127 }, false); |
128 | 128 |
129 // Set up collapsing of menu items | 129 // Set up collapsing of menu items |
130 for (let collapser of document.getElementsByClassName("collapse")) | 130 for (let collapser of document.getElementsByClassName("collapse")) |
131 { | 131 { |
132 collapser.addEventListener("click", toggleCollapse, false); | 132 collapser.addEventListener("click", toggleCollapse, false); |
133 getPref(collapser.dataset.option, value => | 133 getPref(collapser.dataset.option, value => |
134 { | 134 { |
135 if (value) | 135 if (value) |
136 { | 136 { |
137 document.getElementById( | 137 document.getElementById( |
138 collapser.dataset.collapsible | 138 collapser.dataset.collapsible |
139 ).classList.remove("collapsed"); | 139 ).classList.remove("collapsed"); |
140 } | 140 } |
141 }); | 141 }); |
142 } | 142 } |
143 } | 143 } |
144 | 144 |
145 function toggleEnabled() | 145 function toggleEnabled() |
146 { | 146 { |
147 let disabled = document.body.classList.toggle("disabled"); | 147 let disabled = document.body.classList.toggle("disabled"); |
148 chrome.runtime.sendMessage({ | 148 browser.runtime.sendMessage({ |
149 type: disabled ? "filters.whitelist" : "filters.unwhitelist", | 149 type: disabled ? "filters.whitelist" : "filters.unwhitelist", |
150 tab | 150 tab |
151 }); | 151 }); |
152 } | 152 } |
153 | 153 |
154 function activateClickHide() | 154 function activateClickHide() |
155 { | 155 { |
156 document.body.classList.add("clickhide-active"); | 156 document.body.classList.add("clickhide-active"); |
157 chrome.tabs.sendMessage(tab.id, { | 157 browser.tabs.sendMessage(tab.id, { |
158 type: "composer.content.startPickingElement" | 158 type: "composer.content.startPickingElement" |
159 }); | 159 }); |
160 | 160 |
161 // Close the popup after a few seconds, so user doesn't have to | 161 // Close the popup after a few seconds, so user doesn't have to |
162 activateClickHide.timeout = window.setTimeout(window.close, 5000); | 162 activateClickHide.timeout = window.setTimeout(window.close, 5000); |
163 } | 163 } |
164 | 164 |
165 function cancelClickHide() | 165 function cancelClickHide() |
166 { | 166 { |
167 if (activateClickHide.timeout) | 167 if (activateClickHide.timeout) |
168 { | 168 { |
169 window.clearTimeout(activateClickHide.timeout); | 169 window.clearTimeout(activateClickHide.timeout); |
170 activateClickHide.timeout = null; | 170 activateClickHide.timeout = null; |
171 } | 171 } |
172 document.body.classList.remove("clickhide-active"); | 172 document.body.classList.remove("clickhide-active"); |
173 chrome.tabs.sendMessage(tab.id, {type: "composer.content.finished"}); | 173 browser.tabs.sendMessage(tab.id, {type: "composer.content.finished"}); |
174 } | 174 } |
175 | 175 |
176 function toggleCollapse(event) | 176 function toggleCollapse(event) |
177 { | 177 { |
178 let collapser = event.currentTarget; | 178 let collapser = event.currentTarget; |
179 let collapsible = document.getElementById(collapser.dataset.collapsible); | 179 let collapsible = document.getElementById(collapser.dataset.collapsible); |
180 collapsible.classList.toggle("collapsed"); | 180 collapsible.classList.toggle("collapsed"); |
181 togglePref(collapser.dataset.option); | 181 togglePref(collapser.dataset.option); |
182 } | 182 } |
183 | 183 |
184 document.addEventListener("DOMContentLoaded", onLoad, false); | 184 document.addEventListener("DOMContentLoaded", onLoad, false); |
OLD | NEW |