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