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