| Left: | ||
| Right: |
| 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 | |
| 22 const {Filter} = require("filterClasses"); | |
| 23 const {FilterStorage} = require("filterStorage"); | |
| 24 const {checkWhitelisted} = require("whitelisting"); | |
| 25 const {getDecodedHostname} = require("url"); | |
| 26 | |
| 27 let page = null; | |
| 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. | |
| 36 let page = {id: tab.id}; | |
| 37 | |
| 38 if (tab.url) | |
| 39 page.url = new URL(tab.url); | |
| 40 | |
| 41 return page; | |
| 42 } | |
| 43 | 21 |
| 44 function getPref(key, callback) | 22 function getPref(key, callback) |
| 45 { | 23 { |
| 46 chrome.runtime.sendMessage({type: "prefs.get", key}, callback); | 24 chrome.runtime.sendMessage({type: "prefs.get", key}, callback); |
| 47 } | 25 } |
| 48 | 26 |
| 49 function togglePref(key, callback) | 27 function togglePref(key, callback) |
| 50 { | 28 { |
| 51 chrome.runtime.sendMessage({type: "prefs.toggle", key}, callback); | 29 chrome.runtime.sendMessage({type: "prefs.toggle", key}, callback); |
| 52 } | 30 } |
| 53 | 31 |
| 54 function assertPageReady(pageId) | 32 function isPageWhitelisted(callback) |
| 55 { | 33 { |
| 56 return new Promise((resolve, reject) => | 34 chrome.runtime.sendMessage({type: "filters.isWhitelisted", tab}, callback); |
| 57 { | |
| 58 chrome.runtime.sendMessage({type: "composer.isPageReady", pageId}, ready => | |
| 59 { | |
| 60 if (ready) | |
| 61 resolve(); | |
| 62 else | |
| 63 reject(); | |
| 64 }); | |
| 65 }); | |
| 66 } | 35 } |
| 67 | 36 |
| 68 function waitForPageReady(pageId) | 37 function whenPageReady() |
| 69 { | 38 { |
| 70 return new Promise(resolve => | 39 return new Promise(resolve => |
| 71 { | 40 { |
| 72 let listener = (message, sender) => | 41 function onMessage(message, sender) |
| 73 { | 42 { |
| 74 if (message.type == "composer.ready" && sender.page.id == pageId) | 43 if (message.type == "composer.ready" && sender.page && |
| 44 sender.page.id == tab.id) | |
| 75 { | 45 { |
| 76 ext.onMessage.removeListener(listener); | 46 ext.onMessage.removeListener(onMessage); |
| 77 resolve(); | 47 resolve(); |
| 78 } | 48 } |
| 79 }; | 49 } |
| 80 | 50 |
| 81 ext.onMessage.addListener(listener); | 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 }); | |
| 82 }); | 65 }); |
| 83 } | |
| 84 | |
| 85 function whenPageReady(pageId) | |
| 86 { | |
| 87 return Promise.race([ | |
| 88 waitForPageReady(pageId), | |
| 89 // Check if the page is ready after adding the listener just in case we've | |
| 90 // already missed it. | |
| 91 new Promise(resolve => assertPageReady(pageId).then(resolve)) | |
|
Wladimir Palant
2017/09/19 10:17:14
You cannot just ignore the rejection reason of a p
Manish Jethani
2017/09/19 10:22:50
I've removed this odd use of promise rejection now
| |
| 92 ]); | |
| 93 } | 66 } |
| 94 | 67 |
| 95 function onLoad() | 68 function onLoad() |
| 96 { | 69 { |
| 97 chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => | 70 chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => |
| 98 { | 71 { |
| 99 page = createPageObject(tabs[0]); | 72 if (tabs.length > 0) |
| 73 tab = {id: tabs[0].id, url: tabs[0].url}; | |
| 100 | 74 |
| 101 // Mark page as 'local' or 'nohtml' to hide non-relevant elements | 75 let urlProtocol = tab && tab.url && new URL(tab.url).protocol; |
| 102 if (!page || (page.url.protocol != "http:" && | 76 |
| 103 page.url.protocol != "https:")) | 77 // Mark page as 'local' to hide non-relevant elements |
| 78 if (urlProtocol != "http:" && urlProtocol != "https:") | |
| 104 { | 79 { |
| 105 document.body.classList.add("local"); | 80 document.body.classList.add("local"); |
| 81 document.body.classList.remove("nohtml"); | |
| 106 } | 82 } |
| 107 else | 83 else |
| 108 { | 84 { |
| 109 assertPageReady(page.id).catch(() => | 85 whenPageReady().then(() => |
| 110 { | 86 { |
| 111 document.body.classList.add("nohtml"); | 87 document.body.classList.remove("nohtml"); |
| 112 whenPageReady(page.id).then(() => | |
|
Wladimir Palant
2017/09/19 10:17:14
I think that "nohtml" class should just be there i
Manish Jethani
2017/09/19 10:22:50
Acknowledged.
I'll look into this next.
Manish Jethani
2017/09/19 10:35:32
Done.
| |
| 113 { | |
| 114 document.body.classList.remove("nohtml"); | |
| 115 }); | |
| 116 }); | 88 }); |
| 117 } | 89 } |
| 118 | 90 |
| 119 // Ask content script whether clickhide is active. If so, show | 91 // Ask content script whether clickhide is active. If so, show |
| 120 // cancel button. If that isn't the case, ask background.html | 92 // cancel button. If that isn't the case, ask background.html |
| 121 // 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 |
| 122 // wants those filters. Otherwise, we are in default state. | 94 // wants those filters. Otherwise, we are in default state. |
| 123 if (page) | 95 if (tab) |
| 124 { | 96 { |
| 125 if (checkWhitelisted(page)) | 97 isPageWhitelisted(whitelisted => |
| 126 document.body.classList.add("disabled"); | 98 { |
| 99 if (whitelisted) | |
| 100 document.body.classList.add("disabled"); | |
| 101 }); | |
| 127 | 102 |
| 128 chrome.tabs.sendMessage(page.id, { | 103 chrome.tabs.sendMessage(tab.id, { |
| 129 type: "composer.content.getState" | 104 type: "composer.content.getState" |
| 130 }, | 105 }, |
| 131 response => | 106 response => |
| 132 { | 107 { |
| 133 if (response && response.active) | 108 if (response && response.active) |
| 134 document.body.classList.add("clickhide-active"); | 109 document.body.classList.add("clickhide-active"); |
| 135 }); | 110 }); |
| 136 } | 111 } |
| 137 }); | 112 }); |
| 138 | 113 |
| 139 document.getElementById("enabled").addEventListener( | 114 document.getElementById("enabled").addEventListener( |
| 140 "click", toggleEnabled, false | 115 "click", toggleEnabled, false |
| 141 ); | 116 ); |
| 142 document.getElementById("clickhide").addEventListener( | 117 document.getElementById("clickhide").addEventListener( |
| 143 "click", activateClickHide, false | 118 "click", activateClickHide, false |
| 144 ); | 119 ); |
| 145 document.getElementById("clickhide-cancel").addEventListener( | 120 document.getElementById("clickhide-cancel").addEventListener( |
| 146 "click", cancelClickHide, false | 121 "click", cancelClickHide, false |
| 147 ); | 122 ); |
| 148 document.getElementById("options").addEventListener("click", () => | 123 document.getElementById("options").addEventListener("click", () => |
| 149 { | 124 { |
| 150 ext.showOptions(); | 125 ext.showOptions(window.close); |
| 151 }, false); | 126 }, false); |
| 152 | 127 |
| 153 // Set up collapsing of menu items | 128 // Set up collapsing of menu items |
| 154 for (let collapser of document.getElementsByClassName("collapse")) | 129 for (let collapser of document.getElementsByClassName("collapse")) |
| 155 { | 130 { |
| 156 collapser.addEventListener("click", toggleCollapse, false); | 131 collapser.addEventListener("click", toggleCollapse, false); |
| 157 getPref(collapser.dataset.option, value => | 132 getPref(collapser.dataset.option, value => |
| 158 { | 133 { |
| 159 if (value) | 134 if (value) |
| 160 { | 135 { |
| 161 document.getElementById( | 136 document.getElementById( |
| 162 collapser.dataset.collapsible | 137 collapser.dataset.collapsible |
| 163 ).classList.remove("collapsed"); | 138 ).classList.remove("collapsed"); |
| 164 } | 139 } |
| 165 }); | 140 }); |
| 166 } | 141 } |
| 167 } | 142 } |
| 168 | 143 |
| 169 function toggleEnabled() | 144 function toggleEnabled() |
| 170 { | 145 { |
| 171 let disabled = document.body.classList.toggle("disabled"); | 146 let disabled = document.body.classList.toggle("disabled"); |
| 172 if (disabled) | 147 chrome.runtime.sendMessage({ |
| 173 { | 148 type: disabled ? "filters.whitelist" : "filters.unwhitelist", |
| 174 let host = getDecodedHostname(page.url).replace(/^www\./, ""); | 149 tab |
| 175 let filter = Filter.fromText("@@||" + host + "^$document"); | 150 }); |
| 176 if (filter.subscriptions.length && filter.disabled) | |
| 177 filter.disabled = false; | |
| 178 else | |
| 179 { | |
| 180 filter.disabled = false; | |
| 181 FilterStorage.addFilter(filter); | |
| 182 } | |
| 183 } | |
| 184 else | |
| 185 { | |
| 186 // Remove any exception rules applying to this URL | |
| 187 let filter = checkWhitelisted(page); | |
| 188 while (filter) | |
| 189 { | |
| 190 FilterStorage.removeFilter(filter); | |
| 191 if (filter.subscriptions.length) | |
| 192 filter.disabled = true; | |
| 193 filter = checkWhitelisted(page); | |
| 194 } | |
| 195 } | |
| 196 } | 151 } |
| 197 | 152 |
| 198 function activateClickHide() | 153 function activateClickHide() |
| 199 { | 154 { |
| 200 document.body.classList.add("clickhide-active"); | 155 document.body.classList.add("clickhide-active"); |
| 201 page.sendMessage({type: "composer.content.startPickingElement"}); | 156 chrome.tabs.sendMessage(tab.id, { |
| 157 type: "composer.content.startPickingElement" | |
| 158 }); | |
| 202 | 159 |
| 203 // 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 |
| 204 activateClickHide.timeout = window.setTimeout(ext.closePopup, 5000); | 161 activateClickHide.timeout = window.setTimeout(window.close, 5000); |
| 205 } | 162 } |
| 206 | 163 |
| 207 function cancelClickHide() | 164 function cancelClickHide() |
| 208 { | 165 { |
| 209 if (activateClickHide.timeout) | 166 if (activateClickHide.timeout) |
| 210 { | 167 { |
| 211 window.clearTimeout(activateClickHide.timeout); | 168 window.clearTimeout(activateClickHide.timeout); |
| 212 activateClickHide.timeout = null; | 169 activateClickHide.timeout = null; |
| 213 } | 170 } |
| 214 document.body.classList.remove("clickhide-active"); | 171 document.body.classList.remove("clickhide-active"); |
| 215 page.sendMessage({type: "composer.content.finished"}); | 172 chrome.tabs.sendMessage(tab.id, {type: "composer.content.finished"}); |
| 216 } | 173 } |
| 217 | 174 |
| 218 function toggleCollapse(event) | 175 function toggleCollapse(event) |
| 219 { | 176 { |
| 220 let collapser = event.currentTarget; | 177 let collapser = event.currentTarget; |
| 221 let collapsible = document.getElementById(collapser.dataset.collapsible); | 178 let collapsible = document.getElementById(collapser.dataset.collapsible); |
| 222 collapsible.classList.toggle("collapsed"); | 179 collapsible.classList.toggle("collapsed"); |
| 223 togglePref(collapser.dataset.option); | 180 togglePref(collapser.dataset.option); |
| 224 } | 181 } |
| 225 | 182 |
| 226 document.addEventListener("DOMContentLoaded", onLoad, false); | 183 document.addEventListener("DOMContentLoaded", onLoad, false); |
| LEFT | RIGHT |