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: Check for page ready again after adding listener Created Sept. 19, 2017, 8:54 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') | 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 const {Filter} = require("filterClasses");
23 const {FilterStorage} = require("filterStorage"); 23 const {FilterStorage} = require("filterStorage");
24 const {Prefs} = require("prefs");
25 const {checkWhitelisted} = require("whitelisting"); 24 const {checkWhitelisted} = require("whitelisting");
26 const {getDecodedHostname} = require("url"); 25 const {getDecodedHostname} = require("url");
27 26
28 let page = null; 27 let page = null;
29 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.
36 let page = {id: tab.id};
37
38 if (tab.url)
39 page.url = new URL(tab.url);
40
41 return page;
42 }
43
44 function getPref(key, callback)
45 {
46 chrome.runtime.sendMessage({type: "prefs.get", key}, callback);
47 }
48
49 function togglePref(key, callback)
50 {
51 chrome.runtime.sendMessage({type: "prefs.toggle", key}, callback);
52 }
53
54 function checkPageReady(pageId, callback)
55 {
56 chrome.runtime.sendMessage({type: "composer.isPageReady", pageId}, callback);
57 }
58
59 function whenPageReady(pageId, callback)
60 {
61 let handlePageReady = () =>
62 {
63 handlePageReady = null;
Manish Jethani 2017/09/19 08:59:13 Set this to null so it's never called again.
64 ext.onMessage.removeListener(listener);
65 callback();
66 };
67
68 let listener = (message, sender) =>
69 {
70 if (handlePageReady && message.type == "composer.ready" &&
71 sender.page.id == pageId)
72 {
73 handlePageReady();
74 }
75 };
76
77 ext.onMessage.addListener(listener);
78
79 // Check if the page is ready after adding the listener just in case we've
80 // already missed it.
81 checkPageReady(pageId, ready =>
82 {
83 if (ready && handlePageReady)
84 handlePageReady();
85 });
86 }
87
30 function onLoad() 88 function onLoad()
31 { 89 {
32 ext.pages.query({active: true, lastFocusedWindow: true}, pages => 90 chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs =>
33 { 91 {
34 page = pages[0]; 92 page = createPageObject(tabs[0]);
35 93
36 // Mark page as 'local' or 'nohtml' to hide non-relevant elements 94 // Mark page as 'local' or 'nohtml' to hide non-relevant elements
37 if (!page || (page.url.protocol != "http:" && 95 if (!page || (page.url.protocol != "http:" &&
38 page.url.protocol != "https:")) 96 page.url.protocol != "https:"))
97 {
39 document.body.classList.add("local"); 98 document.body.classList.add("local");
40 else if (!require("filterComposer").isPageReady(page)) 99 }
100 else
41 { 101 {
42 document.body.classList.add("nohtml"); 102 checkPageReady(page.id, ready =>
Manish Jethani 2017/09/19 08:59:13 We still do this check since it's going to return
43 require("messaging").getPort(window).on( 103 {
44 "composer.ready", (message, sender) => 104 if (!ready)
45 { 105 {
46 if (sender.page.id == page.id) 106 document.body.classList.add("nohtml");
107 whenPageReady(page.id, () =>
108 {
47 document.body.classList.remove("nohtml"); 109 document.body.classList.remove("nohtml");
110 });
48 } 111 }
49 ); 112 });
50 } 113 }
51 114
52 // Ask content script whether clickhide is active. If so, show 115 // Ask content script whether clickhide is active. If so, show
53 // cancel button. If that isn't the case, ask background.html 116 // cancel button. If that isn't the case, ask background.html
54 // whether it has cached filters. If so, ask the user whether she 117 // whether it has cached filters. If so, ask the user whether she
55 // wants those filters. Otherwise, we are in default state. 118 // wants those filters. Otherwise, we are in default state.
56 if (page) 119 if (page)
57 { 120 {
58 if (checkWhitelisted(page)) 121 if (checkWhitelisted(page))
59 document.body.classList.add("disabled"); 122 document.body.classList.add("disabled");
60 123
61 page.sendMessage({type: "composer.content.getState"}, response => 124 chrome.tabs.sendMessage(page.id, {
125 type: "composer.content.getState"
126 },
127 response =>
62 { 128 {
63 if (response && response.active) 129 if (response && response.active)
64 document.body.classList.add("clickhide-active"); 130 document.body.classList.add("clickhide-active");
65 }); 131 });
66 } 132 }
67 }); 133 });
68 134
69 document.getElementById("enabled").addEventListener( 135 document.getElementById("enabled").addEventListener(
70 "click", toggleEnabled, false 136 "click", toggleEnabled, false
71 ); 137 );
72 document.getElementById("clickhide").addEventListener( 138 document.getElementById("clickhide").addEventListener(
73 "click", activateClickHide, false 139 "click", activateClickHide, false
74 ); 140 );
75 document.getElementById("clickhide-cancel").addEventListener( 141 document.getElementById("clickhide-cancel").addEventListener(
76 "click", cancelClickHide, false 142 "click", cancelClickHide, false
77 ); 143 );
78 document.getElementById("options").addEventListener("click", () => 144 document.getElementById("options").addEventListener("click", () =>
79 { 145 {
80 ext.showOptions(); 146 ext.showOptions();
81 }, false); 147 }, false);
82 148
83 // Set up collapsing of menu items 149 // Set up collapsing of menu items
84 for (let collapser of document.getElementsByClassName("collapse")) 150 for (let collapser of document.getElementsByClassName("collapse"))
85 { 151 {
86 collapser.addEventListener("click", toggleCollapse, false); 152 collapser.addEventListener("click", toggleCollapse, false);
87 if (!Prefs[collapser.dataset.option]) 153 getPref(collapser.dataset.option, value =>
88 { 154 {
89 document.getElementById( 155 if (value)
90 collapser.dataset.collapsable 156 {
91 ).classList.add("collapsed"); 157 document.getElementById(
92 } 158 collapser.dataset.collapsible
159 ).classList.remove("collapsed");
160 }
161 });
93 } 162 }
94 } 163 }
95 164
96 function toggleEnabled() 165 function toggleEnabled()
97 { 166 {
98 let disabled = document.body.classList.toggle("disabled"); 167 let disabled = document.body.classList.toggle("disabled");
99 if (disabled) 168 if (disabled)
100 { 169 {
101 let host = getDecodedHostname(page.url).replace(/^www\./, ""); 170 let host = getDecodedHostname(page.url).replace(/^www\./, "");
102 let filter = Filter.fromText("@@||" + host + "^$document"); 171 let filter = Filter.fromText("@@||" + host + "^$document");
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 window.clearTimeout(activateClickHide.timeout); 207 window.clearTimeout(activateClickHide.timeout);
139 activateClickHide.timeout = null; 208 activateClickHide.timeout = null;
140 } 209 }
141 document.body.classList.remove("clickhide-active"); 210 document.body.classList.remove("clickhide-active");
142 page.sendMessage({type: "composer.content.finished"}); 211 page.sendMessage({type: "composer.content.finished"});
143 } 212 }
144 213
145 function toggleCollapse(event) 214 function toggleCollapse(event)
146 { 215 {
147 let collapser = event.currentTarget; 216 let collapser = event.currentTarget;
148 Prefs[collapser.dataset.option] = !Prefs[collapser.dataset.option]; 217 let collapsible = document.getElementById(collapser.dataset.collapsible);
149 collapser.parentNode.classList.toggle("collapsed"); 218 collapsible.classList.toggle("collapsed");
219 togglePref(collapser.dataset.option);
150 } 220 }
151 221
152 document.addEventListener("DOMContentLoaded", onLoad, false); 222 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