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: Refactor whenPageReady Created Sept. 19, 2017, 6:01 p.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)
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 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 whenPageReady(pageId) 32 function isPageWhitelisted(callback)
55 { 33 {
56 let listener = null; 34 chrome.runtime.sendMessage({type: "filters.isWhitelisted", tab}, callback);
35 }
57 36
58 let cleanUp = () => 37 function whenPageReady()
38 {
39 return new Promise(resolve =>
59 { 40 {
60 ext.onMessage.removeListener(listener); 41 function onMessage(message, sender)
61 }; 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 }
62 50
63 // Make a promise that fulfills when a condition has been met. 51 ext.onMessage.addListener(onMessage);
64 let makePromise = executor => new Promise(resolve => 52
65 { 53 chrome.runtime.sendMessage({
66 executor(condition => 54 type: "composer.isPageReady",
55 pageId: tab.id
56 },
57 ready =>
67 { 58 {
68 if (condition) 59 if (ready)
60 {
61 ext.onMessage.removeListener(onMessage);
69 resolve(); 62 resolve();
63 }
70 }); 64 });
71 }); 65 });
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 } 66 }
106 67
107 function onLoad() 68 function onLoad()
108 { 69 {
109 chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => 70 chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs =>
110 { 71 {
111 page = createPageObject(tabs[0]); 72 if (tabs.length > 0)
73 tab = {id: tabs[0].id, url: tabs[0].url};
74
75 let urlProtocol = tab && tab.url && new URL(tab.url).protocol;
112 76
113 // Mark page as 'local' to hide non-relevant elements 77 // Mark page as 'local' to hide non-relevant elements
114 if (!page || (page.url.protocol != "http:" && 78 if (urlProtocol != "http:" && urlProtocol != "https:")
115 page.url.protocol != "https:"))
116 { 79 {
117 document.body.classList.add("local"); 80 document.body.classList.add("local");
118 document.body.classList.remove("nohtml"); 81 document.body.classList.remove("nohtml");
119 } 82 }
120 else 83 else
121 { 84 {
122 whenPageReady(page.id).then(() => 85 whenPageReady().then(() =>
123 { 86 {
124 document.body.classList.remove("nohtml"); 87 document.body.classList.remove("nohtml");
125 }); 88 });
126 } 89 }
127 90
128 // Ask content script whether clickhide is active. If so, show 91 // Ask content script whether clickhide is active. If so, show
129 // cancel button. If that isn't the case, ask background.html 92 // cancel button. If that isn't the case, ask background.html
130 // 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
131 // wants those filters. Otherwise, we are in default state. 94 // wants those filters. Otherwise, we are in default state.
132 if (page) 95 if (tab)
133 { 96 {
134 if (checkWhitelisted(page)) 97 isPageWhitelisted(whitelisted =>
135 document.body.classList.add("disabled"); 98 {
99 if (whitelisted)
100 document.body.classList.add("disabled");
101 });
136 102
137 chrome.tabs.sendMessage(page.id, { 103 chrome.tabs.sendMessage(tab.id, {
138 type: "composer.content.getState" 104 type: "composer.content.getState"
139 }, 105 },
140 response => 106 response =>
141 { 107 {
142 if (response && response.active) 108 if (response && response.active)
143 document.body.classList.add("clickhide-active"); 109 document.body.classList.add("clickhide-active");
144 }); 110 });
145 } 111 }
146 }); 112 });
147 113
148 document.getElementById("enabled").addEventListener( 114 document.getElementById("enabled").addEventListener(
149 "click", toggleEnabled, false 115 "click", toggleEnabled, false
150 ); 116 );
151 document.getElementById("clickhide").addEventListener( 117 document.getElementById("clickhide").addEventListener(
152 "click", activateClickHide, false 118 "click", activateClickHide, false
153 ); 119 );
154 document.getElementById("clickhide-cancel").addEventListener( 120 document.getElementById("clickhide-cancel").addEventListener(
155 "click", cancelClickHide, false 121 "click", cancelClickHide, false
156 ); 122 );
157 document.getElementById("options").addEventListener("click", () => 123 document.getElementById("options").addEventListener("click", () =>
158 { 124 {
159 ext.showOptions(); 125 ext.showOptions(window.close);
160 }, false); 126 }, false);
161 127
162 // Set up collapsing of menu items 128 // Set up collapsing of menu items
163 for (let collapser of document.getElementsByClassName("collapse")) 129 for (let collapser of document.getElementsByClassName("collapse"))
164 { 130 {
165 collapser.addEventListener("click", toggleCollapse, false); 131 collapser.addEventListener("click", toggleCollapse, false);
166 getPref(collapser.dataset.option, value => 132 getPref(collapser.dataset.option, value =>
167 { 133 {
168 if (value) 134 if (value)
169 { 135 {
170 document.getElementById( 136 document.getElementById(
171 collapser.dataset.collapsible 137 collapser.dataset.collapsible
172 ).classList.remove("collapsed"); 138 ).classList.remove("collapsed");
173 } 139 }
174 }); 140 });
175 } 141 }
176 } 142 }
177 143
178 function toggleEnabled() 144 function toggleEnabled()
179 { 145 {
180 let disabled = document.body.classList.toggle("disabled"); 146 let disabled = document.body.classList.toggle("disabled");
181 if (disabled) 147 chrome.runtime.sendMessage({
182 { 148 type: disabled ? "filters.whitelist" : "filters.unwhitelist",
183 let host = getDecodedHostname(page.url).replace(/^www\./, ""); 149 tab
184 let filter = Filter.fromText("@@||" + host + "^$document"); 150 });
185 if (filter.subscriptions.length && filter.disabled)
186 filter.disabled = false;
187 else
188 {
189 filter.disabled = false;
190 FilterStorage.addFilter(filter);
191 }
192 }
193 else
194 {
195 // Remove any exception rules applying to this URL
196 let filter = checkWhitelisted(page);
197 while (filter)
198 {
199 FilterStorage.removeFilter(filter);
200 if (filter.subscriptions.length)
201 filter.disabled = true;
202 filter = checkWhitelisted(page);
203 }
204 }
205 } 151 }
206 152
207 function activateClickHide() 153 function activateClickHide()
208 { 154 {
209 document.body.classList.add("clickhide-active"); 155 document.body.classList.add("clickhide-active");
210 page.sendMessage({type: "composer.content.startPickingElement"}); 156 chrome.tabs.sendMessage(tab.id, {
157 type: "composer.content.startPickingElement"
158 });
211 159
212 // 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
213 activateClickHide.timeout = window.setTimeout(ext.closePopup, 5000); 161 activateClickHide.timeout = window.setTimeout(window.close, 5000);
214 } 162 }
215 163
216 function cancelClickHide() 164 function cancelClickHide()
217 { 165 {
218 if (activateClickHide.timeout) 166 if (activateClickHide.timeout)
219 { 167 {
220 window.clearTimeout(activateClickHide.timeout); 168 window.clearTimeout(activateClickHide.timeout);
221 activateClickHide.timeout = null; 169 activateClickHide.timeout = null;
222 } 170 }
223 document.body.classList.remove("clickhide-active"); 171 document.body.classList.remove("clickhide-active");
224 page.sendMessage({type: "composer.content.finished"}); 172 chrome.tabs.sendMessage(tab.id, {type: "composer.content.finished"});
225 } 173 }
226 174
227 function toggleCollapse(event) 175 function toggleCollapse(event)
228 { 176 {
229 let collapser = event.currentTarget; 177 let collapser = event.currentTarget;
230 let collapsible = document.getElementById(collapser.dataset.collapsible); 178 let collapsible = document.getElementById(collapser.dataset.collapsible);
231 collapsible.classList.toggle("collapsed"); 179 collapsible.classList.toggle("collapsed");
232 togglePref(collapser.dataset.option); 180 togglePref(collapser.dataset.option);
233 } 181 }
234 182
235 document.addEventListener("DOMContentLoaded", onLoad, false); 183 document.addEventListener("DOMContentLoaded", onLoad, false);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld