Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: popup.js

Issue 29532767: Issue 5593 - Use messaging in popup for prefs, whitelisting, and stats (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Left Patch Set: Move functions into popup.js Created Sept. 18, 2017, 2:22 a.m.
Right Patch Set: Revert one more instance of ext.pages.open Created Sept. 27, 2017, 9:40 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « popup.html ('k') | skin/popup.css » ('j') | stats.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 checkPageReady(pageId, callback) 32 function isPageWhitelisted(callback)
55 { 33 {
56 chrome.runtime.sendMessage({type: "composer.isPageReady", pageId}, callback); 34 chrome.runtime.sendMessage({type: "filters.isWhitelisted", tab}, callback);
57 } 35 }
58 36
59 function waitForPageReady(pageId, callback) 37 function whenPageReady()
60 { 38 {
61 let listener = (message, sender) => 39 return new Promise(resolve =>
62 { 40 {
63 if (message.type == "composer.ready" && sender.page.id == pageId) 41 function onMessage(message, sender)
64 { 42 {
65 ext.onMessage.removeListener(listener); 43 if (message.type == "composer.ready" && sender.page &&
66 callback(); 44 sender.page.id == tab.id)
45 {
46 ext.onMessage.removeListener(onMessage);
47 resolve();
48 }
67 } 49 }
68 };
69 50
70 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 });
65 });
71 } 66 }
72 67
73 function onLoad() 68 function onLoad()
74 { 69 {
75 chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => 70 chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs =>
76 { 71 {
77 page = createPageObject(tabs[0]); 72 if (tabs.length > 0)
73 tab = {id: tabs[0].id, url: tabs[0].url};
78 74
79 // Mark page as 'local' or 'nohtml' to hide non-relevant elements 75 let urlProtocol = tab && tab.url && new URL(tab.url).protocol;
80 if (!page || (page.url.protocol != "http:" && 76
81 page.url.protocol != "https:")) 77 // Mark page as 'local' to hide non-relevant elements
78 if (urlProtocol != "http:" && urlProtocol != "https:")
82 { 79 {
83 document.body.classList.add("local"); 80 document.body.classList.add("local");
81 document.body.classList.remove("nohtml");
84 } 82 }
85 else 83 else
86 { 84 {
87 checkPageReady(page.id, ready => 85 whenPageReady().then(() =>
88 { 86 {
89 if (!ready) 87 document.body.classList.remove("nohtml");
90 {
91 document.body.classList.add("nohtml");
92 waitForPageReady(page.id, () =>
Wladimir Palant 2017/09/18 12:01:27 That's a potential race condition - what if "compo
Manish Jethani 2017/09/19 08:59:13 I've made the change, though I couldn't tell why t
93 {
94 document.body.classList.remove("nohtml");
95 });
96 }
97 }); 88 });
98 } 89 }
99 90
100 // Ask content script whether clickhide is active. If so, show 91 // Ask content script whether clickhide is active. If so, show
101 // cancel button. If that isn't the case, ask background.html 92 // cancel button. If that isn't the case, ask background.html
102 // 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
103 // wants those filters. Otherwise, we are in default state. 94 // wants those filters. Otherwise, we are in default state.
104 if (page) 95 if (tab)
105 { 96 {
106 if (checkWhitelisted(page)) 97 isPageWhitelisted(whitelisted =>
107 document.body.classList.add("disabled"); 98 {
99 if (whitelisted)
100 document.body.classList.add("disabled");
101 });
108 102
109 chrome.tabs.sendMessage(page.id, { 103 chrome.tabs.sendMessage(tab.id, {
110 type: "composer.content.getState" 104 type: "composer.content.getState"
111 }, 105 },
112 response => 106 response =>
113 { 107 {
114 if (response && response.active) 108 if (response && response.active)
115 document.body.classList.add("clickhide-active"); 109 document.body.classList.add("clickhide-active");
116 }); 110 });
117 } 111 }
118 }); 112 });
119 113
120 document.getElementById("enabled").addEventListener( 114 document.getElementById("enabled").addEventListener(
121 "click", toggleEnabled, false 115 "click", toggleEnabled, false
122 ); 116 );
123 document.getElementById("clickhide").addEventListener( 117 document.getElementById("clickhide").addEventListener(
124 "click", activateClickHide, false 118 "click", activateClickHide, false
125 ); 119 );
126 document.getElementById("clickhide-cancel").addEventListener( 120 document.getElementById("clickhide-cancel").addEventListener(
127 "click", cancelClickHide, false 121 "click", cancelClickHide, false
128 ); 122 );
129 document.getElementById("options").addEventListener("click", () => 123 document.getElementById("options").addEventListener("click", () =>
130 { 124 {
131 ext.showOptions(); 125 ext.showOptions(window.close);
132 }, false); 126 }, false);
133 127
134 // Set up collapsing of menu items 128 // Set up collapsing of menu items
135 for (let collapser of document.getElementsByClassName("collapse")) 129 for (let collapser of document.getElementsByClassName("collapse"))
136 { 130 {
137 collapser.addEventListener("click", toggleCollapse, false); 131 collapser.addEventListener("click", toggleCollapse, false);
138 getPref(collapser.dataset.option, value => 132 getPref(collapser.dataset.option, value =>
139 { 133 {
140 if (value) 134 if (value)
141 { 135 {
142 document.getElementById( 136 document.getElementById(
143 collapser.dataset.collapsible 137 collapser.dataset.collapsible
144 ).classList.remove("collapsed"); 138 ).classList.remove("collapsed");
145 } 139 }
146 }); 140 });
147 } 141 }
148 } 142 }
149 143
150 function toggleEnabled() 144 function toggleEnabled()
151 { 145 {
152 let disabled = document.body.classList.toggle("disabled"); 146 let disabled = document.body.classList.toggle("disabled");
153 if (disabled) 147 chrome.runtime.sendMessage({
154 { 148 type: disabled ? "filters.whitelist" : "filters.unwhitelist",
155 let host = getDecodedHostname(page.url).replace(/^www\./, ""); 149 tab
156 let filter = Filter.fromText("@@||" + host + "^$document"); 150 });
157 if (filter.subscriptions.length && filter.disabled)
158 filter.disabled = false;
159 else
160 {
161 filter.disabled = false;
162 FilterStorage.addFilter(filter);
163 }
164 }
165 else
166 {
167 // Remove any exception rules applying to this URL
168 let filter = checkWhitelisted(page);
169 while (filter)
170 {
171 FilterStorage.removeFilter(filter);
172 if (filter.subscriptions.length)
173 filter.disabled = true;
174 filter = checkWhitelisted(page);
175 }
176 }
177 } 151 }
178 152
179 function activateClickHide() 153 function activateClickHide()
180 { 154 {
181 document.body.classList.add("clickhide-active"); 155 document.body.classList.add("clickhide-active");
182 page.sendMessage({type: "composer.content.startPickingElement"}); 156 chrome.tabs.sendMessage(tab.id, {
157 type: "composer.content.startPickingElement"
158 });
183 159
184 // 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
185 activateClickHide.timeout = window.setTimeout(ext.closePopup, 5000); 161 activateClickHide.timeout = window.setTimeout(window.close, 5000);
186 } 162 }
187 163
188 function cancelClickHide() 164 function cancelClickHide()
189 { 165 {
190 if (activateClickHide.timeout) 166 if (activateClickHide.timeout)
191 { 167 {
192 window.clearTimeout(activateClickHide.timeout); 168 window.clearTimeout(activateClickHide.timeout);
193 activateClickHide.timeout = null; 169 activateClickHide.timeout = null;
194 } 170 }
195 document.body.classList.remove("clickhide-active"); 171 document.body.classList.remove("clickhide-active");
196 page.sendMessage({type: "composer.content.finished"}); 172 chrome.tabs.sendMessage(tab.id, {type: "composer.content.finished"});
197 } 173 }
198 174
199 function toggleCollapse(event) 175 function toggleCollapse(event)
200 { 176 {
201 let collapser = event.currentTarget; 177 let collapser = event.currentTarget;
202 let collapsible = document.getElementById(collapser.dataset.collapsible); 178 let collapsible = document.getElementById(collapser.dataset.collapsible);
203 collapsible.classList.toggle("collapsed"); 179 collapsible.classList.toggle("collapsed");
204 togglePref(collapser.dataset.option); 180 togglePref(collapser.dataset.option);
205 } 181 }
206 182
207 document.addEventListener("DOMContentLoaded", onLoad, false); 183 document.addEventListener("DOMContentLoaded", onLoad, false);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld