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

Side by Side Diff: popup.js

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

Powered by Google App Engine
This is Rietveld