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

Powered by Google App Engine
This is Rietveld