| Left: | ||
| Right: |
| 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) | |
|
Sebastian Noack
2017/09/20 18:57:02
I suppose this hack is matter to be removed in a l
Manish Jethani
2017/09/21 06:11:16
Done.
By the way, there is a "filters.isPageWhite
Sebastian Noack
2017/09/21 22:57:16
Well spotted. This is relic from Safari support. I
| |
| 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. | |
| 36 let page = {id: tab.id}; | |
| 37 | |
| 38 if (tab.url) | |
| 39 page.url = new URL(tab.url); | |
| 40 | |
| 41 return page; | |
| 42 } | |
| 43 | |
| 44 function getPref(key, callback) | |
| 45 { | |
| 46 chrome.runtime.sendMessage({type: "prefs.get", key}, callback); | |
| 47 } | |
| 48 | |
| 49 function togglePref(key, callback) | |
| 50 { | |
| 51 chrome.runtime.sendMessage({type: "prefs.toggle", key}, callback); | |
| 52 } | |
| 53 | |
| 54 function whenPageReady(pageId) | |
| 55 { | |
| 56 let listener = null; | |
| 57 | |
| 58 let cleanUp = () => | |
| 59 { | |
| 60 ext.onMessage.removeListener(listener); | |
| 61 }; | |
| 62 | |
| 63 // Make a promise that fulfills when a condition has been met. | |
| 64 let makePromise = executor => new Promise(resolve => | |
| 65 { | |
| 66 executor(condition => | |
| 67 { | |
| 68 if (condition) | |
| 69 resolve(); | |
| 70 }); | |
| 71 }); | |
| 72 | |
| 73 // Make a promise that fulfills when one of a list of conditions has been | |
| 74 // met. | |
| 75 let race = executors => Promise.race(executors.map(makePromise)); | |
| 76 | |
| 77 let promise = race([ | |
| 78 // Wait for a message from the page announcing that it's ready, whenever | |
| 79 // it's ready. | |
| 80 resolveIf => | |
| 81 { | |
| 82 ext.onMessage.addListener(listener = (message, sender) => | |
| 83 { | |
| 84 resolveIf(message.type == "composer.ready" && sender.page && | |
| 85 sender.page.id == pageId); | |
| 86 }); | |
| 87 }, | |
| 88 // Ask the page if it's already ready. | |
| 89 resolveIf => | |
| 90 { | |
| 91 chrome.runtime.sendMessage({type: "composer.isPageReady", pageId}, | |
| 92 resolveIf); | |
| 93 } | |
| 94 ]); | |
| 95 | |
| 96 // Clean up on either fulfillment or rejection, but throw the error back on | |
| 97 // rejection. | |
| 98 promise.then(cleanUp, error => | |
|
Manish Jethani
2017/09/19 18:07:24
We need to clean up on both fulfillment and reject
| |
| 99 { | |
| 100 cleanUp(); | |
| 101 throw error; | |
| 102 }); | |
| 103 | |
| 104 return promise; | |
|
Sebastian Noack
2017/09/20 18:57:02
It seems the logic here would be much simpler and
Manish Jethani
2017/09/21 04:53:59
Yeah, that was a bit overkill.
Done.
| |
| 105 } | |
| 106 | |
| 30 function onLoad() | 107 function onLoad() |
| 31 { | 108 { |
| 32 ext.pages.query({active: true, lastFocusedWindow: true}, pages => | 109 chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => |
| 33 { | 110 { |
| 34 page = pages[0]; | 111 page = createPageObject(tabs[0]); |
| 35 | 112 |
| 36 // Mark page as 'local' or 'nohtml' to hide non-relevant elements | 113 // Mark page as 'local' to hide non-relevant elements |
| 37 if (!page || (page.url.protocol != "http:" && | 114 if (!page || (page.url.protocol != "http:" && |
| 38 page.url.protocol != "https:")) | 115 page.url.protocol != "https:")) |
| 116 { | |
| 39 document.body.classList.add("local"); | 117 document.body.classList.add("local"); |
| 40 else if (!require("filterComposer").isPageReady(page)) | 118 document.body.classList.remove("nohtml"); |
| 119 } | |
| 120 else | |
| 41 { | 121 { |
| 42 document.body.classList.add("nohtml"); | 122 whenPageReady(page.id).then(() => |
| 43 require("messaging").getPort(window).on( | 123 { |
| 44 "composer.ready", (message, sender) => | 124 document.body.classList.remove("nohtml"); |
| 45 { | 125 }); |
| 46 if (sender.page.id == page.id) | |
| 47 document.body.classList.remove("nohtml"); | |
| 48 } | |
| 49 ); | |
| 50 } | 126 } |
| 51 | 127 |
| 52 // Ask content script whether clickhide is active. If so, show | 128 // Ask content script whether clickhide is active. If so, show |
| 53 // cancel button. If that isn't the case, ask background.html | 129 // cancel button. If that isn't the case, ask background.html |
| 54 // whether it has cached filters. If so, ask the user whether she | 130 // whether it has cached filters. If so, ask the user whether she |
| 55 // wants those filters. Otherwise, we are in default state. | 131 // wants those filters. Otherwise, we are in default state. |
| 56 if (page) | 132 if (page) |
| 57 { | 133 { |
| 58 if (checkWhitelisted(page)) | 134 if (checkWhitelisted(page)) |
| 59 document.body.classList.add("disabled"); | 135 document.body.classList.add("disabled"); |
| 60 | 136 |
| 61 page.sendMessage({type: "composer.content.getState"}, response => | 137 chrome.tabs.sendMessage(page.id, { |
| 138 type: "composer.content.getState" | |
| 139 }, | |
| 140 response => | |
| 62 { | 141 { |
| 63 if (response && response.active) | 142 if (response && response.active) |
| 64 document.body.classList.add("clickhide-active"); | 143 document.body.classList.add("clickhide-active"); |
| 65 }); | 144 }); |
| 66 } | 145 } |
| 67 }); | 146 }); |
| 68 | 147 |
| 69 document.getElementById("enabled").addEventListener( | 148 document.getElementById("enabled").addEventListener( |
| 70 "click", toggleEnabled, false | 149 "click", toggleEnabled, false |
| 71 ); | 150 ); |
| 72 document.getElementById("clickhide").addEventListener( | 151 document.getElementById("clickhide").addEventListener( |
| 73 "click", activateClickHide, false | 152 "click", activateClickHide, false |
| 74 ); | 153 ); |
| 75 document.getElementById("clickhide-cancel").addEventListener( | 154 document.getElementById("clickhide-cancel").addEventListener( |
| 76 "click", cancelClickHide, false | 155 "click", cancelClickHide, false |
| 77 ); | 156 ); |
| 78 document.getElementById("options").addEventListener("click", () => | 157 document.getElementById("options").addEventListener("click", () => |
| 79 { | 158 { |
| 80 ext.showOptions(); | 159 ext.showOptions(); |
| 81 }, false); | 160 }, false); |
| 82 | 161 |
| 83 // Set up collapsing of menu items | 162 // Set up collapsing of menu items |
| 84 for (let collapser of document.getElementsByClassName("collapse")) | 163 for (let collapser of document.getElementsByClassName("collapse")) |
| 85 { | 164 { |
| 86 collapser.addEventListener("click", toggleCollapse, false); | 165 collapser.addEventListener("click", toggleCollapse, false); |
| 87 if (!Prefs[collapser.dataset.option]) | 166 getPref(collapser.dataset.option, value => |
| 88 { | 167 { |
| 89 document.getElementById( | 168 if (value) |
| 90 collapser.dataset.collapsable | 169 { |
| 91 ).classList.add("collapsed"); | 170 document.getElementById( |
| 92 } | 171 collapser.dataset.collapsible |
| 172 ).classList.remove("collapsed"); | |
| 173 } | |
| 174 }); | |
| 93 } | 175 } |
| 94 } | 176 } |
| 95 | 177 |
| 96 function toggleEnabled() | 178 function toggleEnabled() |
| 97 { | 179 { |
| 98 let disabled = document.body.classList.toggle("disabled"); | 180 let disabled = document.body.classList.toggle("disabled"); |
| 99 if (disabled) | 181 if (disabled) |
| 100 { | 182 { |
| 101 let host = getDecodedHostname(page.url).replace(/^www\./, ""); | 183 let host = getDecodedHostname(page.url).replace(/^www\./, ""); |
| 102 let filter = Filter.fromText("@@||" + host + "^$document"); | 184 let filter = Filter.fromText("@@||" + host + "^$document"); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 138 window.clearTimeout(activateClickHide.timeout); | 220 window.clearTimeout(activateClickHide.timeout); |
| 139 activateClickHide.timeout = null; | 221 activateClickHide.timeout = null; |
| 140 } | 222 } |
| 141 document.body.classList.remove("clickhide-active"); | 223 document.body.classList.remove("clickhide-active"); |
| 142 page.sendMessage({type: "composer.content.finished"}); | 224 page.sendMessage({type: "composer.content.finished"}); |
| 143 } | 225 } |
| 144 | 226 |
| 145 function toggleCollapse(event) | 227 function toggleCollapse(event) |
| 146 { | 228 { |
| 147 let collapser = event.currentTarget; | 229 let collapser = event.currentTarget; |
| 148 Prefs[collapser.dataset.option] = !Prefs[collapser.dataset.option]; | 230 let collapsible = document.getElementById(collapser.dataset.collapsible); |
| 149 collapser.parentNode.classList.toggle("collapsed"); | 231 collapsible.classList.toggle("collapsed"); |
| 232 togglePref(collapser.dataset.option); | |
| 150 } | 233 } |
| 151 | 234 |
| 152 document.addEventListener("DOMContentLoaded", onLoad, false); | 235 document.addEventListener("DOMContentLoaded", onLoad, false); |
| OLD | NEW |