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

Powered by Google App Engine
This is Rietveld