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 checkPageReady(pageId, callback) |
| 30 { |
| 31 chrome.runtime.sendMessage({type: "composer.isPageReady", pageId}, callback); |
| 32 } |
| 33 |
| 34 function waitForPageReady(pageId, callback) |
| 35 { |
| 36 let listener = (message, sender) => |
| 37 { |
| 38 if (message.type == "composer.ready" && sender.page.id == pageId) |
| 39 { |
| 40 ext.onMessage.removeListener(listener); |
| 41 callback(); |
| 42 } |
| 43 }; |
| 44 |
| 45 ext.onMessage.addListener(listener); |
| 46 } |
| 47 |
30 function onLoad() | 48 function onLoad() |
31 { | 49 { |
32 ext.pages.query({active: true, lastFocusedWindow: true}, pages => | 50 ext.pages.query({active: true, lastFocusedWindow: true}, pages => |
33 { | 51 { |
34 page = pages[0]; | 52 page = pages[0]; |
35 | 53 |
36 // Mark page as 'local' or 'nohtml' to hide non-relevant elements | 54 // Mark page as 'local' or 'nohtml' to hide non-relevant elements |
37 if (!page || (page.url.protocol != "http:" && | 55 if (!page || (page.url.protocol != "http:" && |
38 page.url.protocol != "https:")) | 56 page.url.protocol != "https:")) |
| 57 { |
39 document.body.classList.add("local"); | 58 document.body.classList.add("local"); |
40 else if (!require("filterComposer").isPageReady(page)) | 59 } |
| 60 else |
41 { | 61 { |
42 document.body.classList.add("nohtml"); | 62 checkPageReady(page.id, ready => |
43 require("messaging").getPort(window).on( | 63 { |
44 "composer.ready", (message, sender) => | 64 if (!ready) |
45 { | 65 { |
46 if (sender.page.id == page.id) | 66 document.body.classList.add("nohtml"); |
| 67 waitForPageReady(page.id, () => |
| 68 { |
47 document.body.classList.remove("nohtml"); | 69 document.body.classList.remove("nohtml"); |
| 70 }); |
48 } | 71 } |
49 ); | 72 }); |
50 } | 73 } |
51 | 74 |
52 // Ask content script whether clickhide is active. If so, show | 75 // Ask content script whether clickhide is active. If so, show |
53 // cancel button. If that isn't the case, ask background.html | 76 // cancel button. If that isn't the case, ask background.html |
54 // whether it has cached filters. If so, ask the user whether she | 77 // whether it has cached filters. If so, ask the user whether she |
55 // wants those filters. Otherwise, we are in default state. | 78 // wants those filters. Otherwise, we are in default state. |
56 if (page) | 79 if (page) |
57 { | 80 { |
58 if (checkWhitelisted(page)) | 81 if (checkWhitelisted(page)) |
59 document.body.classList.add("disabled"); | 82 document.body.classList.add("disabled"); |
(...skipping 17 matching lines...) Expand all Loading... |
77 ); | 100 ); |
78 document.getElementById("options").addEventListener("click", () => | 101 document.getElementById("options").addEventListener("click", () => |
79 { | 102 { |
80 ext.showOptions(); | 103 ext.showOptions(); |
81 }, false); | 104 }, false); |
82 | 105 |
83 // Set up collapsing of menu items | 106 // Set up collapsing of menu items |
84 for (let collapser of document.getElementsByClassName("collapse")) | 107 for (let collapser of document.getElementsByClassName("collapse")) |
85 { | 108 { |
86 collapser.addEventListener("click", toggleCollapse, false); | 109 collapser.addEventListener("click", toggleCollapse, false); |
87 if (!Prefs[collapser.dataset.option]) | 110 ext.prefs.get(collapser.dataset.option, value => |
88 { | 111 { |
89 document.getElementById( | 112 if (value) |
90 collapser.dataset.collapsable | 113 { |
91 ).classList.add("collapsed"); | 114 document.getElementById( |
92 } | 115 collapser.dataset.collapsible |
| 116 ).classList.remove("collapsed"); |
| 117 } |
| 118 }); |
93 } | 119 } |
94 } | 120 } |
95 | 121 |
96 function toggleEnabled() | 122 function toggleEnabled() |
97 { | 123 { |
98 let disabled = document.body.classList.toggle("disabled"); | 124 let disabled = document.body.classList.toggle("disabled"); |
99 if (disabled) | 125 if (disabled) |
100 { | 126 { |
101 let host = getDecodedHostname(page.url).replace(/^www\./, ""); | 127 let host = getDecodedHostname(page.url).replace(/^www\./, ""); |
102 let filter = Filter.fromText("@@||" + host + "^$document"); | 128 let filter = Filter.fromText("@@||" + host + "^$document"); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 window.clearTimeout(activateClickHide.timeout); | 164 window.clearTimeout(activateClickHide.timeout); |
139 activateClickHide.timeout = null; | 165 activateClickHide.timeout = null; |
140 } | 166 } |
141 document.body.classList.remove("clickhide-active"); | 167 document.body.classList.remove("clickhide-active"); |
142 page.sendMessage({type: "composer.content.finished"}); | 168 page.sendMessage({type: "composer.content.finished"}); |
143 } | 169 } |
144 | 170 |
145 function toggleCollapse(event) | 171 function toggleCollapse(event) |
146 { | 172 { |
147 let collapser = event.currentTarget; | 173 let collapser = event.currentTarget; |
148 Prefs[collapser.dataset.option] = !Prefs[collapser.dataset.option]; | 174 let collapsible = document.getElementById(collapser.dataset.collapsible); |
149 collapser.parentNode.classList.toggle("collapsed"); | 175 collapsible.classList.toggle("collapsed"); |
| 176 ext.prefs.toggle(collapser.dataset.option); |
150 } | 177 } |
151 | 178 |
152 document.addEventListener("DOMContentLoaded", onLoad, false); | 179 document.addEventListener("DOMContentLoaded", onLoad, false); |
OLD | NEW |