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: Use chrome APIs instead of ext.pages Created Sept. 1, 2017, 6:40 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
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 checkPageReady(pageId, callback)
30 {
31 chrome.runtime.sendMessage({type: "composer.isPageReady", pageId}, callback);
32 }
33
34 function waitForPageReady(pageId, callback)
35 {
36 let listener = (message, sender) =>
37 {
38 if (message.type == "composer.ready" && sender.page.id == pageId)
39 {
40 ext.onMessage.removeListener(listener);
41 callback();
42 }
43 };
44
45 ext.onMessage.addListener(listener);
46 }
47
30 function onLoad() 48 function onLoad()
31 { 49 {
32 ext.pages.query({active: true, lastFocusedWindow: true}, pages => 50 chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs =>
33 { 51 {
34 page = pages[0]; 52 page = ext.createPageObject(tabs[0]);
Sebastian Noack 2017/09/13 02:49:39 Why do you need to wrap this object here?
Manish Jethani 2017/09/18 02:25:50 The checkWhilelisted call further down requires a
35 53
36 // Mark page as 'local' or 'nohtml' to hide non-relevant elements 54 // Mark page as 'local' or 'nohtml' to hide non-relevant elements
37 if (!page || (page.url.protocol != "http:" && 55 if (!page || (page.url.protocol != "http:" &&
38 page.url.protocol != "https:")) 56 page.url.protocol != "https:"))
57 {
39 document.body.classList.add("local"); 58 document.body.classList.add("local");
40 else if (!require("filterComposer").isPageReady(page)) 59 }
60 else
41 { 61 {
42 document.body.classList.add("nohtml"); 62 checkPageReady(page.id, ready =>
43 require("messaging").getPort(window).on( 63 {
44 "composer.ready", (message, sender) => 64 if (!ready)
45 { 65 {
46 if (sender.page.id == page.id) 66 document.body.classList.add("nohtml");
67 waitForPageReady(page.id, () =>
68 {
47 document.body.classList.remove("nohtml"); 69 document.body.classList.remove("nohtml");
70 });
48 } 71 }
49 ); 72 });
50 } 73 }
51 74
52 // Ask content script whether clickhide is active. If so, show 75 // Ask content script whether clickhide is active. If so, show
53 // cancel button. If that isn't the case, ask background.html 76 // cancel button. If that isn't the case, ask background.html
54 // whether it has cached filters. If so, ask the user whether she 77 // whether it has cached filters. If so, ask the user whether she
55 // wants those filters. Otherwise, we are in default state. 78 // wants those filters. Otherwise, we are in default state.
56 if (page) 79 if (page)
57 { 80 {
58 if (checkWhitelisted(page)) 81 if (checkWhitelisted(page))
59 document.body.classList.add("disabled"); 82 document.body.classList.add("disabled");
60 83
61 page.sendMessage({type: "composer.content.getState"}, response => 84 chrome.tabs.sendMessage(page.id, {
85 type: "composer.content.getState"
86 },
87 response =>
62 { 88 {
63 if (response && response.active) 89 if (response && response.active)
64 document.body.classList.add("clickhide-active"); 90 document.body.classList.add("clickhide-active");
65 }); 91 });
66 } 92 }
67 }); 93 });
68 94
69 document.getElementById("enabled").addEventListener( 95 document.getElementById("enabled").addEventListener(
70 "click", toggleEnabled, false 96 "click", toggleEnabled, false
71 ); 97 );
72 document.getElementById("clickhide").addEventListener( 98 document.getElementById("clickhide").addEventListener(
73 "click", activateClickHide, false 99 "click", activateClickHide, false
74 ); 100 );
75 document.getElementById("clickhide-cancel").addEventListener( 101 document.getElementById("clickhide-cancel").addEventListener(
76 "click", cancelClickHide, false 102 "click", cancelClickHide, false
77 ); 103 );
78 document.getElementById("options").addEventListener("click", () => 104 document.getElementById("options").addEventListener("click", () =>
79 { 105 {
80 ext.showOptions(); 106 ext.showOptions();
81 }, false); 107 }, false);
82 108
83 // Set up collapsing of menu items 109 // Set up collapsing of menu items
84 for (let collapser of document.getElementsByClassName("collapse")) 110 for (let collapser of document.getElementsByClassName("collapse"))
85 { 111 {
86 collapser.addEventListener("click", toggleCollapse, false); 112 collapser.addEventListener("click", toggleCollapse, false);
87 if (!Prefs[collapser.dataset.option]) 113 ext.prefs.get(collapser.dataset.option, value =>
88 { 114 {
89 document.getElementById( 115 if (value)
90 collapser.dataset.collapsable 116 {
91 ).classList.add("collapsed"); 117 document.getElementById(
92 } 118 collapser.dataset.collapsible
119 ).classList.remove("collapsed");
120 }
121 });
93 } 122 }
94 } 123 }
95 124
96 function toggleEnabled() 125 function toggleEnabled()
97 { 126 {
98 let disabled = document.body.classList.toggle("disabled"); 127 let disabled = document.body.classList.toggle("disabled");
99 if (disabled) 128 if (disabled)
100 { 129 {
101 let host = getDecodedHostname(page.url).replace(/^www\./, ""); 130 let host = getDecodedHostname(page.url).replace(/^www\./, "");
102 let filter = Filter.fromText("@@||" + host + "^$document"); 131 let filter = Filter.fromText("@@||" + host + "^$document");
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 window.clearTimeout(activateClickHide.timeout); 167 window.clearTimeout(activateClickHide.timeout);
139 activateClickHide.timeout = null; 168 activateClickHide.timeout = null;
140 } 169 }
141 document.body.classList.remove("clickhide-active"); 170 document.body.classList.remove("clickhide-active");
142 page.sendMessage({type: "composer.content.finished"}); 171 page.sendMessage({type: "composer.content.finished"});
143 } 172 }
144 173
145 function toggleCollapse(event) 174 function toggleCollapse(event)
146 { 175 {
147 let collapser = event.currentTarget; 176 let collapser = event.currentTarget;
148 Prefs[collapser.dataset.option] = !Prefs[collapser.dataset.option]; 177 let collapsible = document.getElementById(collapser.dataset.collapsible);
149 collapser.parentNode.classList.toggle("collapsed"); 178 collapsible.classList.toggle("collapsed");
179 ext.prefs.toggle(collapser.dataset.option);
150 } 180 }
151 181
152 document.addEventListener("DOMContentLoaded", onLoad, false); 182 document.addEventListener("DOMContentLoaded", onLoad, false);
OLDNEW

Powered by Google App Engine
This is Rietveld