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: Add comment explaining createPageObject hack Created Sept. 21, 2017, 6:07 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");
Manish Jethani 2017/09/21 08:02:36 Most uses of require are gone, and entirely from p
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. This is needed because functions like
36 // checkWhitelisted and getBlockedPerPage require an ext.Page-like object.
37 // Once all the code for the popup has been modified to use messaging instead
38 // of calling functions directly, this hack can be removed.
39 let pageObject = {id: tab.id};
40
41 if (tab.url)
42 pageObject.url = new URL(tab.url);
43
44 return pageObject;
45 }
46 21
47 function getPref(key, callback) 22 function getPref(key, callback)
48 { 23 {
49 chrome.runtime.sendMessage({type: "prefs.get", key}, callback); 24 chrome.runtime.sendMessage({type: "prefs.get", key}, callback);
50 } 25 }
51 26
52 function togglePref(key, callback) 27 function togglePref(key, callback)
53 { 28 {
54 chrome.runtime.sendMessage({type: "prefs.toggle", key}, callback); 29 chrome.runtime.sendMessage({type: "prefs.toggle", key}, callback);
55 } 30 }
56 31
57 function whenPageReady(pageId) 32 function isPageWhitelisted(callback)
33 {
34 chrome.runtime.sendMessage({type: "filters.isWhitelisted", tab}, callback);
35 }
36
37 function whenPageReady()
58 { 38 {
59 return new Promise(resolve => 39 return new Promise(resolve =>
60 { 40 {
61 let handlePageReady = () => 41 function onMessage(message, sender)
62 {
63 ext.onMessage.removeListener(onMessage);
64 resolve();
65 };
66
67 let onMessage = (message, sender) =>
68 { 42 {
69 if (message.type == "composer.ready" && sender.page && 43 if (message.type == "composer.ready" && sender.page &&
70 sender.page.id == pageId) 44 sender.page.id == tab.id)
71 { 45 {
72 handlePageReady(); 46 ext.onMessage.removeListener(onMessage);
47 resolve();
73 } 48 }
74 }; 49 }
75 50
76 ext.onMessage.addListener(onMessage); 51 ext.onMessage.addListener(onMessage);
77 52
78 chrome.runtime.sendMessage({type: "composer.isPageReady", pageId}, ready => 53 chrome.runtime.sendMessage({
54 type: "composer.isPageReady",
55 pageId: tab.id
56 },
57 ready =>
79 { 58 {
80 if (ready) 59 if (ready)
81 handlePageReady(); 60 {
61 ext.onMessage.removeListener(onMessage);
62 resolve();
63 }
82 }); 64 });
83 }); 65 });
84 } 66 }
85 67
86 function onLoad() 68 function onLoad()
87 { 69 {
88 chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => 70 chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs =>
89 { 71 {
90 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;
91 76
92 // Mark page as 'local' to hide non-relevant elements 77 // Mark page as 'local' to hide non-relevant elements
93 if (!page || (page.url.protocol != "http:" && 78 if (urlProtocol != "http:" && urlProtocol != "https:")
94 page.url.protocol != "https:"))
95 { 79 {
96 document.body.classList.add("local"); 80 document.body.classList.add("local");
97 document.body.classList.remove("nohtml"); 81 document.body.classList.remove("nohtml");
98 } 82 }
99 else 83 else
100 { 84 {
101 whenPageReady(page.id).then(() => 85 whenPageReady().then(() =>
102 { 86 {
103 document.body.classList.remove("nohtml"); 87 document.body.classList.remove("nohtml");
104 }); 88 });
105 } 89 }
106 90
107 // Ask content script whether clickhide is active. If so, show 91 // Ask content script whether clickhide is active. If so, show
108 // cancel button. If that isn't the case, ask background.html 92 // cancel button. If that isn't the case, ask background.html
109 // 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
110 // wants those filters. Otherwise, we are in default state. 94 // wants those filters. Otherwise, we are in default state.
111 if (page) 95 if (tab)
112 { 96 {
113 if (checkWhitelisted(page)) 97 isPageWhitelisted(whitelisted =>
114 document.body.classList.add("disabled"); 98 {
99 if (whitelisted)
100 document.body.classList.add("disabled");
101 });
115 102
116 chrome.tabs.sendMessage(page.id, { 103 chrome.tabs.sendMessage(tab.id, {
117 type: "composer.content.getState" 104 type: "composer.content.getState"
118 }, 105 },
119 response => 106 response =>
120 { 107 {
121 if (response && response.active) 108 if (response && response.active)
122 document.body.classList.add("clickhide-active"); 109 document.body.classList.add("clickhide-active");
123 }); 110 });
124 } 111 }
125 }); 112 });
126 113
127 document.getElementById("enabled").addEventListener( 114 document.getElementById("enabled").addEventListener(
128 "click", toggleEnabled, false 115 "click", toggleEnabled, false
129 ); 116 );
130 document.getElementById("clickhide").addEventListener( 117 document.getElementById("clickhide").addEventListener(
131 "click", activateClickHide, false 118 "click", activateClickHide, false
132 ); 119 );
133 document.getElementById("clickhide-cancel").addEventListener( 120 document.getElementById("clickhide-cancel").addEventListener(
134 "click", cancelClickHide, false 121 "click", cancelClickHide, false
135 ); 122 );
136 document.getElementById("options").addEventListener("click", () => 123 document.getElementById("options").addEventListener("click", () =>
137 { 124 {
138 ext.showOptions(); 125 ext.showOptions(window.close);
139 }, false); 126 }, false);
140 127
141 // Set up collapsing of menu items 128 // Set up collapsing of menu items
142 for (let collapser of document.getElementsByClassName("collapse")) 129 for (let collapser of document.getElementsByClassName("collapse"))
143 { 130 {
144 collapser.addEventListener("click", toggleCollapse, false); 131 collapser.addEventListener("click", toggleCollapse, false);
145 getPref(collapser.dataset.option, value => 132 getPref(collapser.dataset.option, value =>
146 { 133 {
147 if (value) 134 if (value)
148 { 135 {
149 document.getElementById( 136 document.getElementById(
150 collapser.dataset.collapsible 137 collapser.dataset.collapsible
151 ).classList.remove("collapsed"); 138 ).classList.remove("collapsed");
152 } 139 }
153 }); 140 });
154 } 141 }
155 } 142 }
156 143
157 function toggleEnabled() 144 function toggleEnabled()
158 { 145 {
159 let disabled = document.body.classList.toggle("disabled"); 146 let disabled = document.body.classList.toggle("disabled");
160 if (disabled) 147 chrome.runtime.sendMessage({
161 { 148 type: disabled ? "filters.whitelist" : "filters.unwhitelist",
162 let host = getDecodedHostname(page.url).replace(/^www\./, ""); 149 tab
163 let filter = Filter.fromText("@@||" + host + "^$document"); 150 });
164 if (filter.subscriptions.length && filter.disabled)
165 filter.disabled = false;
166 else
167 {
168 filter.disabled = false;
169 FilterStorage.addFilter(filter);
170 }
171 }
172 else
173 {
174 // Remove any exception rules applying to this URL
175 let filter = checkWhitelisted(page);
176 while (filter)
177 {
178 FilterStorage.removeFilter(filter);
179 if (filter.subscriptions.length)
180 filter.disabled = true;
181 filter = checkWhitelisted(page);
182 }
183 }
184 } 151 }
185 152
186 function activateClickHide() 153 function activateClickHide()
187 { 154 {
188 document.body.classList.add("clickhide-active"); 155 document.body.classList.add("clickhide-active");
189 page.sendMessage({type: "composer.content.startPickingElement"}); 156 chrome.tabs.sendMessage(tab.id, {
157 type: "composer.content.startPickingElement"
158 });
190 159
191 // 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
192 activateClickHide.timeout = window.setTimeout(ext.closePopup, 5000); 161 activateClickHide.timeout = window.setTimeout(window.close, 5000);
193 } 162 }
194 163
195 function cancelClickHide() 164 function cancelClickHide()
196 { 165 {
197 if (activateClickHide.timeout) 166 if (activateClickHide.timeout)
198 { 167 {
199 window.clearTimeout(activateClickHide.timeout); 168 window.clearTimeout(activateClickHide.timeout);
200 activateClickHide.timeout = null; 169 activateClickHide.timeout = null;
201 } 170 }
202 document.body.classList.remove("clickhide-active"); 171 document.body.classList.remove("clickhide-active");
203 page.sendMessage({type: "composer.content.finished"}); 172 chrome.tabs.sendMessage(tab.id, {type: "composer.content.finished"});
204 } 173 }
205 174
206 function toggleCollapse(event) 175 function toggleCollapse(event)
207 { 176 {
208 let collapser = event.currentTarget; 177 let collapser = event.currentTarget;
209 let collapsible = document.getElementById(collapser.dataset.collapsible); 178 let collapsible = document.getElementById(collapser.dataset.collapsible);
210 collapsible.classList.toggle("collapsed"); 179 collapsible.classList.toggle("collapsed");
211 togglePref(collapser.dataset.option); 180 togglePref(collapser.dataset.option);
212 } 181 }
213 182
214 document.addEventListener("DOMContentLoaded", onLoad, false); 183 document.addEventListener("DOMContentLoaded", onLoad, false);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld