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

Delta Between Two Patch Sets: lib/filterComposer.js

Issue 29370947: Issue 3138 - Improve how context menu "block element" handles iframes (Closed)
Left Patch Set: Rebased Created Aug. 9, 2017, 3:27 p.m.
Right Patch Set: Use messaging instead of requiring the info module Created Oct. 19, 2017, 10:52 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « ext/background.js ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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-2017 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 /** @module filterComposer */ 18 /** @module filterComposer */
19 19
20 "use strict"; 20 "use strict";
21 21
22 const {defaultMatcher} = require("matcher"); 22 const {defaultMatcher} = require("matcher");
23 const {RegExpFilter} = require("filterClasses"); 23 const {RegExpFilter} = require("filterClasses");
24 const {FilterNotifier} = require("filterNotifier"); 24 const {FilterNotifier} = require("filterNotifier");
25 const {Prefs} = require("prefs"); 25 const {Prefs} = require("prefs");
26 const {extractHostFromFrame, stringifyURL, isThirdParty} = require("url"); 26 const {extractHostFromFrame, stringifyURL, isThirdParty} = require("url");
27 const {getKey, checkWhitelisted} = require("whitelisting"); 27 const {getKey, checkWhitelisted} = require("whitelisting");
28 const {port} = require("messaging"); 28 const {port} = require("messaging");
29 const info = require("info");
29 30
30 let readyPages = new ext.PageMap(); 31 let readyPages = new ext.PageMap();
31 32
32 /** 33 /**
33 * Checks whether the given page is ready to use the filter composer 34 * Checks whether the given page is ready to use the filter composer
34 * 35 *
35 * @param {Page} page 36 * @param {Page} page
36 * @return {boolean} 37 * @return {boolean}
37 */ 38 */
38 exports.isPageReady = page => 39 exports.isPageReady = page =>
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 getKey(page, frame), specificOnly 112 getKey(page, frame), specificOnly
112 ); 113 );
113 114
114 if (!filter) 115 if (!filter)
115 { 116 {
116 let filterText = url.replace(/^[\w-]+:\/+(?:www\.)?/, "||"); 117 let filterText = url.replace(/^[\w-]+:\/+(?:www\.)?/, "||");
117 118
118 if (specificOnly) 119 if (specificOnly)
119 filterText += "$domain=" + docDomain; 120 filterText += "$domain=" + docDomain;
120 121
121 if (filters.indexOf(filterText) == -1) 122 if (!filters.includes(filterText))
122 filters.push(filterText); 123 filters.push(filterText);
123 } 124 }
124 } 125 }
125 126
126 // If we couldn't generate any blocking filters, fallback to element hiding 127 // If we couldn't generate any blocking filters, fallback to element hiding
127 if (filters.length == 0 && !checkWhitelisted(page, frame, 128 if (filters.length == 0 && !checkWhitelisted(page, frame,
128 RegExpFilter.typeMap.ELEMHIDE)) 129 RegExpFilter.typeMap.ELEMHIDE))
129 { 130 {
130 // Generate CSS selectors based on the element's "id" and 131 // Generate CSS selectors based on the element's "id" and
131 // "class" attribute. 132 // "class" attribute.
(...skipping 27 matching lines...) Expand all
159 // Add an element hiding filter for each generated CSS selector 160 // Add an element hiding filter for each generated CSS selector
160 for (let selector of selectors) 161 for (let selector of selectors)
161 filters.push(docDomain.replace(/^www\./, "") + "##" + selector); 162 filters.push(docDomain.replace(/^www\./, "") + "##" + selector);
162 } 163 }
163 } 164 }
164 165
165 return {filters, selectors}; 166 return {filters, selectors};
166 } 167 }
167 168
168 let contextMenuItem = { 169 let contextMenuItem = {
169 title: ext.i18n.getMessage("block_element"), 170 title: browser.i18n.getMessage("block_element"),
170 contexts: ["image", "video", "audio"], 171 contexts: ["image", "video", "audio"],
171 onclick(page, info) 172 onclick(page, info)
172 { 173 {
173 page.sendMessage( 174 page.sendMessage(
174 {type: "composer.content.contextMenuClicked"}, undefined, info.frameId 175 {type: "composer.content.contextMenuClicked"}, undefined, info.frameId
175 ); 176 );
176 } 177 }
177 }; 178 };
178 179
179 function updateContextMenu(page, filter) 180 function updateContextMenu(page, filter)
180 { 181 {
181 page.contextMenus.remove(contextMenuItem); 182 page.contextMenus.remove(contextMenuItem);
182 183
183 if (typeof filter == "undefined") 184 if (typeof filter == "undefined")
184 filter = checkWhitelisted(page); 185 filter = checkWhitelisted(page);
185 if (!filter && Prefs.shouldShowBlockElementMenu && readyPages.has(page)) 186
187 // We don't support the filter composer on Firefox for Android, because the
188 // user experience on mobile is quite different.
189 if (info.application != "fennec" &&
190 !filter && Prefs.shouldShowBlockElementMenu && readyPages.has(page))
191 {
186 page.contextMenus.create(contextMenuItem); 192 page.contextMenus.create(contextMenuItem);
193 }
187 } 194 }
188 195
189 FilterNotifier.on("page.WhitelistingStateRevalidate", updateContextMenu); 196 FilterNotifier.on("page.WhitelistingStateRevalidate", updateContextMenu);
190 197
191 Prefs.on("shouldShowBlockElementMenu", () => 198 Prefs.on("shouldShowBlockElementMenu", () =>
192 { 199 {
193 ext.pages.query({}, pages => 200 browser.tabs.query({}, tabs =>
194 { 201 {
195 for (let page of pages) 202 for (let tab of tabs)
196 updateContextMenu(page); 203 updateContextMenu(new ext.Page(tab));
197 }); 204 });
205 });
206
207 port.on("composer.isPageReady", (message, sender) =>
208 {
209 return readyPages.has(new ext.Page({id: message.pageId}));
198 }); 210 });
199 211
200 port.on("composer.ready", (message, sender) => 212 port.on("composer.ready", (message, sender) =>
201 { 213 {
202 readyPages.set(sender.page, null); 214 readyPages.set(sender.page, null);
203 updateContextMenu(sender.page); 215 updateContextMenu(sender.page);
204 }); 216 });
205 217
206 port.on("composer.openDialog", (message, sender) => 218 port.on("composer.openDialog", (message, sender) =>
207 { 219 {
208 return new Promise(resolve => 220 return new Promise(resolve =>
209 { 221 {
210 ext.windows.create({ 222 ext.windows.create({
211 url: ext.getURL("composer.html"), 223 url: browser.extension.getURL("composer.html"),
212 left: 50, 224 left: 50,
213 top: 50, 225 top: 50,
214 width: 420, 226 width: 420,
215 height: 200, 227 height: 200,
216 type: "popup" 228 type: "popup"
217 }, 229 },
218 popupPage => 230 popupPage =>
219 { 231 {
220 let popupPageId = popupPage.id; 232 let popupPageId = popupPage.id;
221 function onRemoved(removedPageId) 233 function onRemoved(removedPageId)
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
253 265
254 port.on("composer.quoteCSS", (message, sender) => 266 port.on("composer.quoteCSS", (message, sender) =>
255 { 267 {
256 return quoteCSS(message.CSS); 268 return quoteCSS(message.CSS);
257 }); 269 });
258 270
259 ext.pages.onLoading.addListener(page => 271 ext.pages.onLoading.addListener(page =>
260 { 272 {
261 page.sendMessage({type: "composer.content.finished"}); 273 page.sendMessage({type: "composer.content.finished"});
262 }); 274 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld