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

Side by Side Diff: lib/filterComposer.js

Issue 29338764: Issue 3842 - Split up the logic updating the icon and context menu (Closed)
Patch Set: Rebased Created March 22, 2016, 8:26 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 | « background.js ('k') | lib/icon.js » ('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-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 /** @module filterComposer */ 18 /** @module filterComposer */
19 19
20 "use strict"; 20 "use strict";
21 21
22 let {defaultMatcher} = require("matcher");
23 let {RegExpFilter} = require("filterClasses");
24 let {FilterNotifier} = require("filterNotifier");
25 let {Prefs} = require("prefs");
22 let {extractHostFromFrame, stringifyURL, isThirdParty} = require("url"); 26 let {extractHostFromFrame, stringifyURL, isThirdParty} = require("url");
23 let {getKey, checkWhitelisted} = require("whitelisting"); 27 let {getKey, checkWhitelisted} = require("whitelisting");
24 let {defaultMatcher} = require("matcher");
25 let {RegExpFilter} = require("filterClasses");
26 let {port} = require("messaging"); 28 let {port} = require("messaging");
27 29
30 let readyPages = new ext.PageMap();
31
32 /**
33 * Checks whether the given page is ready to use the filter composer
34 *
35 * @param {Page} page
36 * @return {boolean}
37 */
38 exports.isPageReady = function(page)
39 {
40 return readyPages.has(page);
41 };
42
28 function isValidString(s) { 43 function isValidString(s) {
29 return s && s.indexOf("\0") == -1; 44 return s && s.indexOf("\0") == -1;
30 } 45 }
31 46
32 function escapeChar(chr) 47 function escapeChar(chr)
33 { 48 {
34 let code = chr.charCodeAt(0); 49 let code = chr.charCodeAt(0);
35 50
36 // Control characters and leading digits must be escaped based on 51 // Control characters and leading digits must be escaped based on
37 // their char code in CSS. Moreover, curly brackets aren't allowed 52 // their char code in CSS. Moreover, curly brackets aren't allowed
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 146
132 // Add an element hiding filter for each generated CSS selector 147 // Add an element hiding filter for each generated CSS selector
133 for (let selector of selectors) 148 for (let selector of selectors)
134 filters.push(docDomain.replace(/^www\./, "") + "##" + selector); 149 filters.push(docDomain.replace(/^www\./, "") + "##" + selector);
135 } 150 }
136 } 151 }
137 152
138 return {filters: filters, selectors: selectors}; 153 return {filters: filters, selectors: selectors};
139 } 154 }
140 155
156 let contextMenuItem = {
157 title: ext.i18n.getMessage("block_element"),
158 contexts: ["image", "video", "audio"],
159 onclick: page =>
160 {
161 page.sendMessage({type: "composer.content.contextMenuClicked"});
162 }
163 };
164
165 function updateContextMenu(page, filter)
166 {
167 page.contextMenus.remove(contextMenuItem);
168
169 if (typeof filter == "undefined")
170 filter = checkWhitelisted(page);
171 if (!filter && Prefs.shouldShowBlockElementMenu && readyPages.has(page))
172 page.contextMenus.create(contextMenuItem);
173 }
174
175 FilterNotifier.addListener((action, page, filter) =>
176 {
177 if (action == "page.WhitelistingStateRevalidate")
178 updateContextMenu(page, filter);
179 });
180
181 Prefs.on("shouldShowBlockElementMenu", () =>
182 {
183 ext.pages.query({}, pages =>
184 {
185 for (let page of pages)
186 updateContextMenu(page);
187 });
188 });
189
141 port.on("composer.ready", (message, sender) => 190 port.on("composer.ready", (message, sender) =>
142 { 191 {
143 htmlPages.set(sender.page, null); 192 readyPages.set(sender.page, null);
144 refreshIconAndContextMenu(sender.page); 193 updateContextMenu(sender.page);
145 }); 194 });
146 195
147 port.on("composer.openDialog", (message, sender) => 196 port.on("composer.openDialog", (message, sender) =>
148 { 197 {
149 return new Promise(resolve => 198 return new Promise(resolve =>
150 { 199 {
151 ext.windows.create({ 200 ext.windows.create({
152 url: ext.getURL("composer.html"), 201 url: ext.getURL("composer.html"),
153 left: 50, 202 left: 50,
154 top: 50, 203 top: 50,
(...skipping 30 matching lines...) Expand all
185 src: message.src, 234 src: message.src,
186 style: message.style, 235 style: message.style,
187 classes: message.classes, 236 classes: message.classes,
188 urls: message.urls, 237 urls: message.urls,
189 type: message.mediatype, 238 type: message.mediatype,
190 baseURL: message.baseURL, 239 baseURL: message.baseURL,
191 page: sender.page, 240 page: sender.page,
192 frame: sender.frame 241 frame: sender.frame
193 }); 242 });
194 }); 243 });
OLDNEW
« no previous file with comments | « background.js ('k') | lib/icon.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld