LEFT | RIGHT |
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 const {require} = ext.backgroundPage.getWindow(); | 20 let tab = null; |
21 | 21 |
22 const {Filter} = require("filterClasses"); | 22 function getPref(key, callback) |
23 const {FilterStorage} = require("filterStorage"); | 23 { |
24 const {checkWhitelisted} = require("whitelisting"); | 24 chrome.runtime.sendMessage({type: "prefs.get", key}, callback); |
25 const {getDecodedHostname} = require("url"); | 25 } |
26 | 26 |
27 let page = null; | 27 function togglePref(key, callback) |
| 28 { |
| 29 chrome.runtime.sendMessage({type: "prefs.toggle", key}, callback); |
| 30 } |
| 31 |
| 32 function isPageWhitelisted(callback) |
| 33 { |
| 34 chrome.runtime.sendMessage({type: "filters.isWhitelisted", tab}, callback); |
| 35 } |
| 36 |
| 37 function whenPageReady() |
| 38 { |
| 39 return new Promise(resolve => |
| 40 { |
| 41 function onMessage(message, sender) |
| 42 { |
| 43 if (message.type == "composer.ready" && sender.page && |
| 44 sender.page.id == tab.id) |
| 45 { |
| 46 ext.onMessage.removeListener(onMessage); |
| 47 resolve(); |
| 48 } |
| 49 } |
| 50 |
| 51 ext.onMessage.addListener(onMessage); |
| 52 |
| 53 chrome.runtime.sendMessage({ |
| 54 type: "composer.isPageReady", |
| 55 pageId: tab.id |
| 56 }, |
| 57 ready => |
| 58 { |
| 59 if (ready) |
| 60 { |
| 61 ext.onMessage.removeListener(onMessage); |
| 62 resolve(); |
| 63 } |
| 64 }); |
| 65 }); |
| 66 } |
28 | 67 |
29 function onLoad() | 68 function onLoad() |
30 { | 69 { |
31 ext.pages.query({active: true, lastFocusedWindow: true}, pages => | 70 chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => |
32 { | 71 { |
33 page = pages[0]; | 72 if (tabs.length > 0) |
| 73 tab = {id: tabs[0].id, url: tabs[0].url}; |
34 | 74 |
35 // Mark page as 'local' or 'nohtml' to hide non-relevant elements | 75 let urlProtocol = tab && tab.url && new URL(tab.url).protocol; |
36 if (!page || (page.url.protocol != "http:" && | 76 |
37 page.url.protocol != "https:")) | 77 // Mark page as 'local' to hide non-relevant elements |
| 78 if (urlProtocol != "http:" && urlProtocol != "https:") |
| 79 { |
38 document.body.classList.add("local"); | 80 document.body.classList.add("local"); |
39 else if (!require("filterComposer").isPageReady(page)) | 81 document.body.classList.remove("nohtml"); |
| 82 } |
| 83 else |
40 { | 84 { |
41 document.body.classList.add("nohtml"); | 85 whenPageReady().then(() => |
42 require("messaging").getPort(window).on( | 86 { |
43 "composer.ready", (message, sender) => | 87 document.body.classList.remove("nohtml"); |
44 { | 88 }); |
45 if (sender.page.id == page.id) | |
46 document.body.classList.remove("nohtml"); | |
47 } | |
48 ); | |
49 } | 89 } |
50 | 90 |
51 // Ask content script whether clickhide is active. If so, show | 91 // Ask content script whether clickhide is active. If so, show |
52 // cancel button. If that isn't the case, ask background.html | 92 // cancel button. If that isn't the case, ask background.html |
53 // 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 |
54 // wants those filters. Otherwise, we are in default state. | 94 // wants those filters. Otherwise, we are in default state. |
55 if (page) | 95 if (tab) |
56 { | 96 { |
57 if (checkWhitelisted(page)) | 97 isPageWhitelisted(whitelisted => |
58 document.body.classList.add("disabled"); | 98 { |
| 99 if (whitelisted) |
| 100 document.body.classList.add("disabled"); |
| 101 }); |
59 | 102 |
60 page.sendMessage({type: "composer.content.getState"}, response => | 103 chrome.tabs.sendMessage(tab.id, { |
| 104 type: "composer.content.getState" |
| 105 }, |
| 106 response => |
61 { | 107 { |
62 if (response && response.active) | 108 if (response && response.active) |
63 document.body.classList.add("clickhide-active"); | 109 document.body.classList.add("clickhide-active"); |
64 }); | 110 }); |
65 } | 111 } |
66 }); | 112 }); |
67 | 113 |
68 document.getElementById("enabled").addEventListener( | 114 document.getElementById("enabled").addEventListener( |
69 "click", toggleEnabled, false | 115 "click", toggleEnabled, false |
70 ); | 116 ); |
71 document.getElementById("clickhide").addEventListener( | 117 document.getElementById("clickhide").addEventListener( |
72 "click", activateClickHide, false | 118 "click", activateClickHide, false |
73 ); | 119 ); |
74 document.getElementById("clickhide-cancel").addEventListener( | 120 document.getElementById("clickhide-cancel").addEventListener( |
75 "click", cancelClickHide, false | 121 "click", cancelClickHide, false |
76 ); | 122 ); |
77 document.getElementById("options").addEventListener("click", () => | 123 document.getElementById("options").addEventListener("click", () => |
78 { | 124 { |
79 ext.showOptions(); | 125 ext.showOptions(window.close); |
80 }, false); | 126 }, false); |
81 | 127 |
82 // Set up collapsing of menu items | 128 // Set up collapsing of menu items |
83 for (let collapser of document.getElementsByClassName("collapse")) | 129 for (let collapser of document.getElementsByClassName("collapse")) |
84 { | 130 { |
85 collapser.addEventListener("click", toggleCollapse, false); | 131 collapser.addEventListener("click", toggleCollapse, false); |
86 chrome.runtime.sendMessage({ | 132 getPref(collapser.dataset.option, value => |
87 type: "prefs.get", | |
88 key: collapser.dataset.option | |
89 }, | |
90 value => | |
91 { | 133 { |
92 if (value) | 134 if (value) |
93 { | 135 { |
94 document.getElementById( | 136 document.getElementById( |
95 collapser.dataset.collapsible | 137 collapser.dataset.collapsible |
96 ).classList.remove("collapsed"); | 138 ).classList.remove("collapsed"); |
97 } | 139 } |
98 }); | 140 }); |
99 } | 141 } |
100 } | 142 } |
101 | 143 |
102 function toggleEnabled() | 144 function toggleEnabled() |
103 { | 145 { |
104 let disabled = document.body.classList.toggle("disabled"); | 146 let disabled = document.body.classList.toggle("disabled"); |
105 if (disabled) | 147 chrome.runtime.sendMessage({ |
106 { | 148 type: disabled ? "filters.whitelist" : "filters.unwhitelist", |
107 let host = getDecodedHostname(page.url).replace(/^www\./, ""); | 149 tab |
108 let filter = Filter.fromText("@@||" + host + "^$document"); | 150 }); |
109 if (filter.subscriptions.length && filter.disabled) | |
110 filter.disabled = false; | |
111 else | |
112 { | |
113 filter.disabled = false; | |
114 FilterStorage.addFilter(filter); | |
115 } | |
116 } | |
117 else | |
118 { | |
119 // Remove any exception rules applying to this URL | |
120 let filter = checkWhitelisted(page); | |
121 while (filter) | |
122 { | |
123 FilterStorage.removeFilter(filter); | |
124 if (filter.subscriptions.length) | |
125 filter.disabled = true; | |
126 filter = checkWhitelisted(page); | |
127 } | |
128 } | |
129 } | 151 } |
130 | 152 |
131 function activateClickHide() | 153 function activateClickHide() |
132 { | 154 { |
133 document.body.classList.add("clickhide-active"); | 155 document.body.classList.add("clickhide-active"); |
134 page.sendMessage({type: "composer.content.startPickingElement"}); | 156 chrome.tabs.sendMessage(tab.id, { |
| 157 type: "composer.content.startPickingElement" |
| 158 }); |
135 | 159 |
136 // Close the popup after a few seconds, so user doesn't have to | 160 // Close the popup after a few seconds, so user doesn't have to |
137 activateClickHide.timeout = window.setTimeout(ext.closePopup, 5000); | 161 activateClickHide.timeout = window.setTimeout(window.close, 5000); |
138 } | 162 } |
139 | 163 |
140 function cancelClickHide() | 164 function cancelClickHide() |
141 { | 165 { |
142 if (activateClickHide.timeout) | 166 if (activateClickHide.timeout) |
143 { | 167 { |
144 window.clearTimeout(activateClickHide.timeout); | 168 window.clearTimeout(activateClickHide.timeout); |
145 activateClickHide.timeout = null; | 169 activateClickHide.timeout = null; |
146 } | 170 } |
147 document.body.classList.remove("clickhide-active"); | 171 document.body.classList.remove("clickhide-active"); |
148 page.sendMessage({type: "composer.content.finished"}); | 172 chrome.tabs.sendMessage(tab.id, {type: "composer.content.finished"}); |
149 } | 173 } |
150 | 174 |
151 function toggleCollapse(event) | 175 function toggleCollapse(event) |
152 { | 176 { |
153 let collapser = event.currentTarget; | 177 let collapser = event.currentTarget; |
154 let collapsible = document.getElementById(collapser.dataset.collapsible); | 178 let collapsible = document.getElementById(collapser.dataset.collapsible); |
155 collapsible.classList.toggle("collapsed"); | 179 collapsible.classList.toggle("collapsed"); |
156 chrome.runtime.sendMessage({ | 180 togglePref(collapser.dataset.option); |
157 type: "prefs.toggle", | |
158 key: collapser.dataset.option | |
159 }); | |
160 } | 181 } |
161 | 182 |
162 document.addEventListener("DOMContentLoaded", onLoad, false); | 183 document.addEventListener("DOMContentLoaded", onLoad, false); |
LEFT | RIGHT |