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

Side by Side Diff: popup.js

Issue 29338491: Issue 3823 - Split up message responder code (Closed)
Patch Set: Implement Port.disconnect() Created March 21, 2016, 4:59 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 | « metadata.common ('k') | no next file » | 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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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 var backgroundPage = ext.backgroundPage.getWindow(); 18 var backgroundPage = ext.backgroundPage.getWindow();
19 var require = backgroundPage.require; 19 var require = backgroundPage.require;
20 20
21 var Filter = require("filterClasses").Filter; 21 var Filter = require("filterClasses").Filter;
22 var FilterStorage = require("filterStorage").FilterStorage; 22 var FilterStorage = require("filterStorage").FilterStorage;
23 var Prefs = require("prefs").Prefs; 23 var Prefs = require("prefs").Prefs;
24 var checkWhitelisted = require("whitelisting").checkWhitelisted; 24 var checkWhitelisted = require("whitelisting").checkWhitelisted;
25 var getDecodedHostname = require("url").getDecodedHostname; 25 var getDecodedHostname = require("url").getDecodedHostname;
26 var getPort = require("messaging").getPort;
26 27
27 var page = null; 28 var page = null;
28 29
29 function onLoad() 30 function onLoad()
30 { 31 {
31 ext.pages.query({active: true, lastFocusedWindow: true}, function(pages) 32 ext.pages.query({active: true, lastFocusedWindow: true}, function(pages)
32 { 33 {
33 page = pages[0]; 34 page = pages[0];
34 35
35 // Mark page as 'local' or 'nohtml' to hide non-relevant elements 36 // Mark page as 'local' or 'nohtml' to hide non-relevant elements
36 if (!page || (page.url.protocol != "http:" && 37 if (!page || (page.url.protocol != "http:" &&
37 page.url.protocol != "https:")) 38 page.url.protocol != "https:"))
38 document.body.classList.add("local"); 39 document.body.classList.add("local");
39 else if (!backgroundPage.htmlPages.has(page)) 40 else if (!backgroundPage.htmlPages.has(page))
41 {
40 document.body.classList.add("nohtml"); 42 document.body.classList.add("nohtml");
43 getPort(window).on("composer.ready", function(message, sender)
44 {
45 if (sender.page.id == page.id)
46 document.body.classList.remove("nohtml");
47 });
48 }
41 49
42 // Ask content script whether clickhide is active. If so, show cancel button . 50 // Ask content script whether clickhide is active. If so, show cancel button .
43 // If that isn't the case, ask background.html whether it has cached filters . If so, 51 // If that isn't the case, ask background.html whether it has cached filters . If so,
44 // ask the user whether she wants those filters. 52 // ask the user whether she wants those filters.
45 // Otherwise, we are in default state. 53 // Otherwise, we are in default state.
46 if (page) 54 if (page)
47 { 55 {
48 if (checkWhitelisted(page)) 56 if (checkWhitelisted(page))
49 document.body.classList.add("disabled"); 57 document.body.classList.add("disabled");
50 58
51 page.sendMessage({type: "composer.content.getState"}, function(response) 59 page.sendMessage({type: "composer.content.getState"}, function(response)
52 { 60 {
53 if (response && response.active) 61 if (response && response.active)
54 document.body.classList.add("clickhide-active"); 62 document.body.classList.add("clickhide-active");
55 }); 63 });
56 } 64 }
57 }); 65 });
58 66
59 // Attach event listeners
60 ext.onMessage.addListener(onMessage);
61 document.getElementById("enabled").addEventListener("click", toggleEnabled, fa lse); 67 document.getElementById("enabled").addEventListener("click", toggleEnabled, fa lse);
62 document.getElementById("clickhide").addEventListener("click", activateClickHi de, false); 68 document.getElementById("clickhide").addEventListener("click", activateClickHi de, false);
63 document.getElementById("clickhide-cancel").addEventListener("click", cancelCl ickHide, false); 69 document.getElementById("clickhide-cancel").addEventListener("click", cancelCl ickHide, false);
64 document.getElementById("options").addEventListener("click", function() 70 document.getElementById("options").addEventListener("click", function()
65 { 71 {
66 ext.showOptions(); 72 ext.showOptions();
67 }, false); 73 }, false);
68 74
69 // Set up collapsing of menu items 75 // Set up collapsing of menu items
70 var collapsers = document.getElementsByClassName("collapse"); 76 var collapsers = document.getElementsByClassName("collapse");
71 for (var i = 0; i < collapsers.length; i++) 77 for (var i = 0; i < collapsers.length; i++)
72 { 78 {
73 var collapser = collapsers[i]; 79 var collapser = collapsers[i];
74 collapser.addEventListener("click", toggleCollapse, false); 80 collapser.addEventListener("click", toggleCollapse, false);
75 if (!Prefs[collapser.dataset.option]) 81 if (!Prefs[collapser.dataset.option])
76 document.getElementById(collapser.dataset.collapsable).classList.add("coll apsed"); 82 document.getElementById(collapser.dataset.collapsable).classList.add("coll apsed");
77 } 83 }
78 } 84 }
79 85
80 function onUnload()
81 {
82 ext.onMessage.removeListener(onMessage);
83 }
84
85 function onMessage(message, sender, callback)
86 {
87 if (message.type == "composer.ready" && sender.page.id == page.id)
88 document.body.classList.remove("nohtml");
89 }
90
91 function toggleEnabled() 86 function toggleEnabled()
92 { 87 {
93 var disabled = document.body.classList.toggle("disabled"); 88 var disabled = document.body.classList.toggle("disabled");
94 if (disabled) 89 if (disabled)
95 { 90 {
96 var host = getDecodedHostname(page.url).replace(/^www\./, ""); 91 var host = getDecodedHostname(page.url).replace(/^www\./, "");
97 var filter = Filter.fromText("@@||" + host + "^$document"); 92 var filter = Filter.fromText("@@||" + host + "^$document");
98 if (filter.subscriptions.length && filter.disabled) 93 if (filter.subscriptions.length && filter.disabled)
99 filter.disabled = false; 94 filter.disabled = false;
100 else 95 else
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 } 133 }
139 134
140 function toggleCollapse(event) 135 function toggleCollapse(event)
141 { 136 {
142 var collapser = event.currentTarget; 137 var collapser = event.currentTarget;
143 Prefs[collapser.dataset.option] = !Prefs[collapser.dataset.option]; 138 Prefs[collapser.dataset.option] = !Prefs[collapser.dataset.option];
144 collapser.parentNode.classList.toggle("collapsed"); 139 collapser.parentNode.classList.toggle("collapsed");
145 } 140 }
146 141
147 document.addEventListener("DOMContentLoaded", onLoad, false); 142 document.addEventListener("DOMContentLoaded", onLoad, false);
148 window.addEventListener("unload", onUnload, false);
OLDNEW
« no previous file with comments | « metadata.common ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld