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