OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2013 Eyeo GmbH | 3 * Copyright (C) 2006-2014 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 imports = ["require", "isWhitelisted", "extractHostFromURL", "refreshIconAnd
ContextMenu", "openOptions"]; | 19 var imports = ["require", "extractHostFromURL", "openOptions"]; |
20 for (var i = 0; i < imports.length; i++) | 20 for (var i = 0; i < imports.length; i++) |
21 window[imports[i]] = backgroundPage[imports[i]]; | 21 window[imports[i]] = backgroundPage[imports[i]]; |
22 | 22 |
23 var Filter = require("filterClasses").Filter; | 23 var Filter = require("filterClasses").Filter; |
24 var FilterStorage = require("filterStorage").FilterStorage; | 24 var FilterStorage = require("filterStorage").FilterStorage; |
25 var Prefs = require("prefs").Prefs; | 25 var Prefs = require("prefs").Prefs; |
| 26 var isWhitelisted = require("whitelisting").isWhitelisted; |
26 | 27 |
27 var tab = null; | 28 var page = null; |
28 | 29 |
29 function init() | 30 function init() |
30 { | 31 { |
31 // Mark page as local to hide non-relevant elements | 32 ext.pages.query({active: true, lastFocusedWindow: true}, function(pages) |
32 ext.windows.getLastFocused(function(win) | |
33 { | 33 { |
34 win.getActiveTab(function(tab) | 34 page = pages[0]; |
| 35 |
| 36 // Mark page as local to hide non-relevant elements |
| 37 if (!page || !/^https?:\/\//.test(page.url)) |
| 38 document.body.classList.add("local"); |
| 39 |
| 40 // Ask content script whether clickhide is active. If so, show cancel button
. |
| 41 // If that isn't the case, ask background.html whether it has cached filters
. If so, |
| 42 // ask the user whether she wants those filters. |
| 43 // Otherwise, we are in default state. |
| 44 if (page) |
35 { | 45 { |
36 if (!/^https?:\/\//.exec(tab.url)) | 46 if (isWhitelisted(page.url)) |
37 document.body.classList.add("local"); | 47 document.getElementById("enabled").classList.add("off"); |
38 }); | 48 |
| 49 page.sendMessage({type: "get-clickhide-state"}, function(response) |
| 50 { |
| 51 if (response && response.active) |
| 52 document.body.classList.add("clickhide-active"); |
| 53 }); |
| 54 } |
39 }); | 55 }); |
40 | 56 |
41 // Attach event listeners | 57 // Attach event listeners |
42 document.getElementById("enabled").addEventListener("click", toggleEnabled, fa
lse); | 58 document.getElementById("enabled").addEventListener("click", toggleEnabled, fa
lse); |
43 document.getElementById("clickhide").addEventListener("click", activateClickHi
de, false); | 59 document.getElementById("clickhide").addEventListener("click", activateClickHi
de, false); |
44 document.getElementById("clickhide-cancel").addEventListener("click", cancelCl
ickHide, false); | 60 document.getElementById("clickhide-cancel").addEventListener("click", cancelCl
ickHide, false); |
45 document.getElementById("options").addEventListener("click", function() | 61 document.getElementById("options").addEventListener("click", function() |
46 { | 62 { |
47 openOptions(); | 63 openOptions(); |
48 }, false); | 64 }, false); |
49 | 65 |
50 // Set up collapsing of menu items | 66 // Set up collapsing of menu items |
51 var collapsers = document.getElementsByClassName("collapse"); | 67 var collapsers = document.getElementsByClassName("collapse"); |
52 for (var i = 0; i < collapsers.length; i++) | 68 for (var i = 0; i < collapsers.length; i++) |
53 { | 69 { |
54 var collapser = collapsers[i]; | 70 var collapser = collapsers[i]; |
55 collapser.addEventListener("click", toggleCollapse, false); | 71 collapser.addEventListener("click", toggleCollapse, false); |
56 if (!Prefs[collapser.dataset.option]) | 72 if (!Prefs[collapser.dataset.option]) |
57 document.getElementById(collapser.dataset.collapsable).classList.add("coll
apsed"); | 73 document.getElementById(collapser.dataset.collapsable).classList.add("coll
apsed"); |
58 } | 74 } |
59 | |
60 // Ask content script whether clickhide is active. If so, show cancel button. | |
61 // If that isn't the case, ask background.html whether it has cached filters.
If so, | |
62 // ask the user whether she wants those filters. | |
63 // Otherwise, we are in default state. | |
64 ext.windows.getLastFocused(function(win) | |
65 { | |
66 win.getActiveTab(function(t) | |
67 { | |
68 tab = t; | |
69 if (isWhitelisted(tab.url)) | |
70 document.getElementById("enabled").classList.add("off"); | |
71 | |
72 tab.sendMessage({type: "get-clickhide-state"}, function(response) | |
73 { | |
74 if (response && response.active) | |
75 document.body.classList.add("clickhide-active"); | |
76 }); | |
77 }); | |
78 }); | |
79 } | 75 } |
80 window.addEventListener("DOMContentLoaded", init, false); | 76 window.addEventListener("DOMContentLoaded", init, false); |
81 | 77 |
82 function toggleEnabled() | 78 function toggleEnabled() |
83 { | 79 { |
84 var enabledButton = document.getElementById("enabled") | 80 var enabledButton = document.getElementById("enabled") |
85 var disabled = enabledButton.classList.toggle("off"); | 81 var disabled = enabledButton.classList.toggle("off"); |
86 if (disabled) | 82 if (disabled) |
87 { | 83 { |
88 var host = extractHostFromURL(tab.url).replace(/^www\./, ""); | 84 var host = extractHostFromURL(page.url).replace(/^www\./, ""); |
89 var filter = Filter.fromText("@@||" + host + "^$document"); | 85 var filter = Filter.fromText("@@||" + host + "^$document"); |
90 if (filter.subscriptions.length && filter.disabled) | 86 if (filter.subscriptions.length && filter.disabled) |
91 filter.disabled = false; | 87 filter.disabled = false; |
92 else | 88 else |
93 { | 89 { |
94 filter.disabled = false; | 90 filter.disabled = false; |
95 FilterStorage.addFilter(filter); | 91 FilterStorage.addFilter(filter); |
96 } | 92 } |
97 } | 93 } |
98 else | 94 else |
99 { | 95 { |
100 // Remove any exception rules applying to this URL | 96 // Remove any exception rules applying to this URL |
101 var filter = isWhitelisted(tab.url); | 97 var filter = isWhitelisted(page.url); |
102 while (filter) | 98 while (filter) |
103 { | 99 { |
104 FilterStorage.removeFilter(filter); | 100 FilterStorage.removeFilter(filter); |
105 if (filter.subscriptions.length) | 101 if (filter.subscriptions.length) |
106 filter.disabled = true; | 102 filter.disabled = true; |
107 filter = isWhitelisted(tab.url); | 103 filter = isWhitelisted(page.url); |
108 } | 104 } |
109 } | 105 } |
110 | |
111 refreshIconAndContextMenu(tab); | |
112 } | 106 } |
113 | 107 |
114 function activateClickHide() | 108 function activateClickHide() |
115 { | 109 { |
116 document.body.classList.add("clickhide-active"); | 110 document.body.classList.add("clickhide-active"); |
117 tab.sendMessage({type: "clickhide-activate"}); | 111 page.sendMessage({type: "clickhide-activate"}); |
118 | 112 |
119 // Close the popup after a few seconds, so user doesn't have to | 113 // Close the popup after a few seconds, so user doesn't have to |
120 activateClickHide.timeout = window.setTimeout(window.close, 5000); | 114 activateClickHide.timeout = window.setTimeout(ext.closePopup, 5000); |
121 } | 115 } |
122 | 116 |
123 function cancelClickHide() | 117 function cancelClickHide() |
124 { | 118 { |
125 if (activateClickHide.timeout) | 119 if (activateClickHide.timeout) |
126 { | 120 { |
127 window.clearTimeout(activateClickHide.timeout); | 121 window.clearTimeout(activateClickHide.timeout); |
128 activateClickHide.timeout = null; | 122 activateClickHide.timeout = null; |
129 } | 123 } |
130 document.body.classList.remove("clickhide-active"); | 124 document.body.classList.remove("clickhide-active"); |
131 tab.sendMessage({type: "clickhide-deactivate"}); | 125 page.sendMessage({type: "clickhide-deactivate"}); |
132 } | 126 } |
133 | 127 |
134 function toggleCollapse(event) | 128 function toggleCollapse(event) |
135 { | 129 { |
136 var collapser = event.currentTarget; | 130 var collapser = event.currentTarget; |
137 Prefs[collapser.dataset.option] = !Prefs[collapser.dataset.option]; | 131 Prefs[collapser.dataset.option] = !Prefs[collapser.dataset.option]; |
138 collapser.parentNode.classList.toggle("collapsed"); | 132 collapser.parentNode.classList.toggle("collapsed"); |
139 } | 133 } |
OLD | NEW |