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 const {require} = ext.backgroundPage.getWindow(); | 20 const {require} = ext.backgroundPage.getWindow(); |
21 | 21 |
22 const {Filter} = require("filterClasses"); | 22 const {Filter} = require("filterClasses"); |
23 const {FilterStorage} = require("filterStorage"); | 23 const {FilterStorage} = require("filterStorage"); |
24 const {Prefs} = require("prefs"); | |
25 const {checkWhitelisted} = require("whitelisting"); | 24 const {checkWhitelisted} = require("whitelisting"); |
26 const {getDecodedHostname} = require("url"); | 25 const {getDecodedHostname} = require("url"); |
27 | 26 |
28 let page = null; | 27 let page = null; |
29 | 28 |
| 29 function createPageObject(tab) |
| 30 { |
| 31 if (!tab) |
| 32 return null; |
| 33 |
| 34 // Create a lightweight page object that resembles ext.Page, but with only |
| 35 // an id and an optional url property. This is needed because functions like |
| 36 // checkWhitelisted and getBlockedPerPage require an ext.Page-like object. |
| 37 // Once all the code for the popup has been modified to use messaging instead |
| 38 // of calling functions directly, this hack can be removed. |
| 39 let pageObject = {id: tab.id}; |
| 40 |
| 41 if (tab.url) |
| 42 pageObject.url = new URL(tab.url); |
| 43 |
| 44 return pageObject; |
| 45 } |
| 46 |
| 47 function getPref(key, callback) |
| 48 { |
| 49 chrome.runtime.sendMessage({type: "prefs.get", key}, callback); |
| 50 } |
| 51 |
| 52 function togglePref(key, callback) |
| 53 { |
| 54 chrome.runtime.sendMessage({type: "prefs.toggle", key}, callback); |
| 55 } |
| 56 |
| 57 function whenPageReady(pageId) |
| 58 { |
| 59 return new Promise(resolve => |
| 60 { |
| 61 let handlePageReady = () => |
| 62 { |
| 63 ext.onMessage.removeListener(onMessage); |
| 64 resolve(); |
| 65 }; |
| 66 |
| 67 let onMessage = (message, sender) => |
| 68 { |
| 69 if (message.type == "composer.ready" && sender.page && |
| 70 sender.page.id == pageId) |
| 71 { |
| 72 handlePageReady(); |
| 73 } |
| 74 }; |
| 75 |
| 76 ext.onMessage.addListener(onMessage); |
| 77 |
| 78 chrome.runtime.sendMessage({type: "composer.isPageReady", pageId}, ready => |
| 79 { |
| 80 if (ready) |
| 81 handlePageReady(); |
| 82 }); |
| 83 }); |
| 84 } |
| 85 |
30 function onLoad() | 86 function onLoad() |
31 { | 87 { |
32 ext.pages.query({active: true, lastFocusedWindow: true}, pages => | 88 chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => |
33 { | 89 { |
34 page = pages[0]; | 90 page = createPageObject(tabs[0]); |
35 | 91 |
36 // Mark page as 'local' or 'nohtml' to hide non-relevant elements | 92 // Mark page as 'local' to hide non-relevant elements |
37 if (!page || (page.url.protocol != "http:" && | 93 if (!page || (page.url.protocol != "http:" && |
38 page.url.protocol != "https:")) | 94 page.url.protocol != "https:")) |
| 95 { |
39 document.body.classList.add("local"); | 96 document.body.classList.add("local"); |
40 else if (!require("filterComposer").isPageReady(page)) | 97 document.body.classList.remove("nohtml"); |
| 98 } |
| 99 else |
41 { | 100 { |
42 document.body.classList.add("nohtml"); | 101 whenPageReady(page.id).then(() => |
43 require("messaging").getPort(window).on( | 102 { |
44 "composer.ready", (message, sender) => | 103 document.body.classList.remove("nohtml"); |
45 { | 104 }); |
46 if (sender.page.id == page.id) | |
47 document.body.classList.remove("nohtml"); | |
48 } | |
49 ); | |
50 } | 105 } |
51 | 106 |
52 // Ask content script whether clickhide is active. If so, show | 107 // Ask content script whether clickhide is active. If so, show |
53 // cancel button. If that isn't the case, ask background.html | 108 // cancel button. If that isn't the case, ask background.html |
54 // whether it has cached filters. If so, ask the user whether she | 109 // whether it has cached filters. If so, ask the user whether she |
55 // wants those filters. Otherwise, we are in default state. | 110 // wants those filters. Otherwise, we are in default state. |
56 if (page) | 111 if (page) |
57 { | 112 { |
58 if (checkWhitelisted(page)) | 113 if (checkWhitelisted(page)) |
59 document.body.classList.add("disabled"); | 114 document.body.classList.add("disabled"); |
60 | 115 |
61 page.sendMessage({type: "composer.content.getState"}, response => | 116 chrome.tabs.sendMessage(page.id, { |
| 117 type: "composer.content.getState" |
| 118 }, |
| 119 response => |
62 { | 120 { |
63 if (response && response.active) | 121 if (response && response.active) |
64 document.body.classList.add("clickhide-active"); | 122 document.body.classList.add("clickhide-active"); |
65 }); | 123 }); |
66 } | 124 } |
67 }); | 125 }); |
68 | 126 |
69 document.getElementById("enabled").addEventListener( | 127 document.getElementById("enabled").addEventListener( |
70 "click", toggleEnabled, false | 128 "click", toggleEnabled, false |
71 ); | 129 ); |
72 document.getElementById("clickhide").addEventListener( | 130 document.getElementById("clickhide").addEventListener( |
73 "click", activateClickHide, false | 131 "click", activateClickHide, false |
74 ); | 132 ); |
75 document.getElementById("clickhide-cancel").addEventListener( | 133 document.getElementById("clickhide-cancel").addEventListener( |
76 "click", cancelClickHide, false | 134 "click", cancelClickHide, false |
77 ); | 135 ); |
78 document.getElementById("options").addEventListener("click", () => | 136 document.getElementById("options").addEventListener("click", () => |
79 { | 137 { |
80 ext.showOptions(); | 138 ext.showOptions(); |
81 }, false); | 139 }, false); |
82 | 140 |
83 // Set up collapsing of menu items | 141 // Set up collapsing of menu items |
84 for (let collapser of document.getElementsByClassName("collapse")) | 142 for (let collapser of document.getElementsByClassName("collapse")) |
85 { | 143 { |
86 collapser.addEventListener("click", toggleCollapse, false); | 144 collapser.addEventListener("click", toggleCollapse, false); |
87 if (!Prefs[collapser.dataset.option]) | 145 getPref(collapser.dataset.option, value => |
88 { | 146 { |
89 document.getElementById( | 147 if (value) |
90 collapser.dataset.collapsable | 148 { |
91 ).classList.add("collapsed"); | 149 document.getElementById( |
92 } | 150 collapser.dataset.collapsible |
| 151 ).classList.remove("collapsed"); |
| 152 } |
| 153 }); |
93 } | 154 } |
94 } | 155 } |
95 | 156 |
96 function toggleEnabled() | 157 function toggleEnabled() |
97 { | 158 { |
98 let disabled = document.body.classList.toggle("disabled"); | 159 let disabled = document.body.classList.toggle("disabled"); |
99 if (disabled) | 160 if (disabled) |
100 { | 161 { |
101 let host = getDecodedHostname(page.url).replace(/^www\./, ""); | 162 let host = getDecodedHostname(page.url).replace(/^www\./, ""); |
102 let filter = Filter.fromText("@@||" + host + "^$document"); | 163 let filter = Filter.fromText("@@||" + host + "^$document"); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 window.clearTimeout(activateClickHide.timeout); | 199 window.clearTimeout(activateClickHide.timeout); |
139 activateClickHide.timeout = null; | 200 activateClickHide.timeout = null; |
140 } | 201 } |
141 document.body.classList.remove("clickhide-active"); | 202 document.body.classList.remove("clickhide-active"); |
142 page.sendMessage({type: "composer.content.finished"}); | 203 page.sendMessage({type: "composer.content.finished"}); |
143 } | 204 } |
144 | 205 |
145 function toggleCollapse(event) | 206 function toggleCollapse(event) |
146 { | 207 { |
147 let collapser = event.currentTarget; | 208 let collapser = event.currentTarget; |
148 Prefs[collapser.dataset.option] = !Prefs[collapser.dataset.option]; | 209 let collapsible = document.getElementById(collapser.dataset.collapsible); |
149 collapser.parentNode.classList.toggle("collapsed"); | 210 collapsible.classList.toggle("collapsed"); |
| 211 togglePref(collapser.dataset.option); |
150 } | 212 } |
151 | 213 |
152 document.addEventListener("DOMContentLoaded", onLoad, false); | 214 document.addEventListener("DOMContentLoaded", onLoad, false); |
OLD | NEW |