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

Side by Side Diff: lib/contentFiltering.js

Issue 29865587: Issue 6843 - Log snippet filter hits (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Fixed nits Created Aug. 27, 2018, 4:41 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 | « dependencies ('k') | lib/hitLogger.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-present 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 contentFiltering */ 18 /** @module contentFiltering */
19 19
20 "use strict"; 20 "use strict";
21 21
22 const {RegExpFilter} = require("../adblockpluscore/lib/filterClasses"); 22 const {RegExpFilter} = require("../adblockpluscore/lib/filterClasses");
23 const {ElemHide} = require("../adblockpluscore/lib/elemHide"); 23 const {ElemHide} = require("../adblockpluscore/lib/elemHide");
24 const {ElemHideEmulation} = require("../adblockpluscore/lib/elemHideEmulation"); 24 const {ElemHideEmulation} = require("../adblockpluscore/lib/elemHideEmulation");
25 const {FilterNotifier} = require("../adblockpluscore/lib/filterNotifier");
25 const {Snippets, compileScript} = require("../adblockpluscore/lib/snippets"); 26 const {Snippets, compileScript} = require("../adblockpluscore/lib/snippets");
26 const {checkWhitelisted} = require("./whitelisting"); 27 const {checkWhitelisted} = require("./whitelisting");
27 const {extractHostFromFrame} = require("./url"); 28 const {extractHostFromFrame} = require("./url");
28 const {port} = require("./messaging"); 29 const {port} = require("./messaging");
29 const {HitLogger} = require("./hitLogger"); 30 const {HitLogger, logRequest} = require("./hitLogger");
30 const info = require("info"); 31 const info = require("info");
31 32
32 // Chromium's support for tabs.removeCSS is still a work in progress and the 33 // Chromium's support for tabs.removeCSS is still a work in progress and the
33 // API is likely to be different from Firefox's; for now we just don't use it 34 // API is likely to be different from Firefox's; for now we just don't use it
34 // at all, even if it's available. 35 // at all, even if it's available.
35 // See https://crbug.com/608854 36 // See https://crbug.com/608854
36 const styleSheetRemovalSupported = info.platform == "gecko"; 37 const styleSheetRemovalSupported = info.platform == "gecko";
37 38
38 const selectorGroupSize = 1024; 39 const selectorGroupSize = 1024;
39 40
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 code: getExecutableCode(script), 185 code: getExecutableCode(script),
185 matchAboutBlank: true, 186 matchAboutBlank: true,
186 runAt: "document_start" 187 runAt: "document_start"
187 }; 188 };
188 189
189 // Chrome <50 throws an exception if chrome.tabs.executeScript is called 190 // Chrome <50 throws an exception if chrome.tabs.executeScript is called
190 // with a frameId of 0. 191 // with a frameId of 0.
191 if (frameId != 0) 192 if (frameId != 0)
192 details.frameId = frameId; 193 details.frameId = frameId;
193 194
194 browser.tabs.executeScript(tabId, details) 195 return browser.tabs.executeScript(tabId, details)
195 .catch(error => 196 .catch(error =>
196 { 197 {
197 // Sometimes a frame is added and removed very quickly, in such cases we 198 // Sometimes a frame is added and removed very quickly, in such cases we
198 // simply ignore the error. 199 // simply ignore the error.
199 if (error.message == "The frame was removed.") 200 if (error.message == "The frame was removed.")
200 return; 201 return;
201 202
202 // Sometimes the frame in question is just not found. We don't know why 203 // Sometimes the frame in question is just not found. We don't know why
203 // this is exactly, but we simply ignore the error. 204 // this is exactly, but we simply ignore the error.
204 if (/^No frame with id \d+ in tab \d+\.$/.test(error.message)) 205 if (/^No frame with id \d+ in tab \d+\.$/.test(error.message))
205 return; 206 return;
206 207
207 throw error; 208 throw error;
208 }); 209 });
209 } 210 }
210 catch (error) 211 catch (error)
211 { 212 {
212 // See the comment in the catch block associated with the call to 213 // See the comment in the catch block associated with the call to
213 // tabs.insertCSS for why we catch and ignore any errors here. 214 // tabs.insertCSS for why we catch any error here and simply
215 // return a rejected promise.
216 return Promise.reject(error);
Sebastian Noack 2018/08/27 17:37:34 It appears I'm missing something here. But what is
hub 2018/08/27 19:45:24 Promise.catch(f) with call Promise.then(undefined,
Manish Jethani 2018/08/27 20:07:54 If the call to `tabs.executeScript` throws, the fu
Sebastian Noack 2018/08/27 20:15:11 Acknowledged. I misread the code and thought we ar
214 } 217 }
215 } 218 }
216 219
217 port.on("content.applyFilters", (message, sender) => 220 port.on("content.applyFilters", (message, sender) =>
218 { 221 {
219 let selectors = []; 222 let selectors = [];
220 let emulatedPatterns = []; 223 let emulatedPatterns = [];
221 let trace = HitLogger.hasListener(sender.page.id); 224 let trace = HitLogger.hasListener(sender.page.id);
222 let inline = !userStyleSheetsSupported; 225 let inline = !userStyleSheetsSupported;
223 226
224 let {elemhide, snippets} = message.filterTypes || 227 let {elemhide, snippets} = message.filterTypes ||
225 {elemhide: true, snippets: true}; 228 {elemhide: true, snippets: true};
226 229
227 if (!checkWhitelisted(sender.page, sender.frame, null, 230 if (!checkWhitelisted(sender.page, sender.frame, null,
228 RegExpFilter.typeMap.DOCUMENT)) 231 RegExpFilter.typeMap.DOCUMENT))
229 { 232 {
230 let hostname = extractHostFromFrame(sender.frame); 233 let docDomain = extractHostFromFrame(sender.frame);
231 234
232 if (snippets) 235 if (snippets)
233 { 236 {
234 for (let script of Snippets.getScriptsForDomain(hostname)) 237 for (let filter of Snippets.getFiltersForDomain(docDomain))
235 executeScript(script, sender.page.id, sender.frame.id); 238 {
239 executeScript(filter.script, sender.page.id, sender.frame.id)
240 .then(() =>
Sebastian Noack 2018/08/27 17:37:34 Nit: Wrapping here appears unnecessary.
hub 2018/08/27 19:45:24 without wrapping the `>` reach column 80 so I felt
Sebastian Noack 2018/08/27 20:15:11 Yes, the line may have up to 80 characters, not mo
241 {
242 let tabIds = [sender.page.id];
243 if (filter)
244 FilterNotifier.emit("filter.hitCount", filter, 0, 0, tabIds);
245
246 logRequest(tabIds, {
247 url: sender.frame.url.href,
248 type: "SNIPPET",
249 docDomain
250 }, filter);
251 });
252 }
236 } 253 }
237 254
238 if (elemhide && !checkWhitelisted(sender.page, sender.frame, null, 255 if (elemhide && !checkWhitelisted(sender.page, sender.frame, null,
239 RegExpFilter.typeMap.ELEMHIDE)) 256 RegExpFilter.typeMap.ELEMHIDE))
240 { 257 {
241 let specificOnly = checkWhitelisted(sender.page, sender.frame, null, 258 let specificOnly = checkWhitelisted(sender.page, sender.frame, null,
242 RegExpFilter.typeMap.GENERICHIDE); 259 RegExpFilter.typeMap.GENERICHIDE);
243 selectors = ElemHide.getSelectorsForDomain(hostname, specificOnly); 260 selectors = ElemHide.getSelectorsForDomain(docDomain, specificOnly);
244 261
245 for (let filter of ElemHideEmulation.getRulesForDomain(hostname)) 262 for (let filter of ElemHideEmulation.getRulesForDomain(docDomain))
246 emulatedPatterns.push({selector: filter.selector, text: filter.text}); 263 emulatedPatterns.push({selector: filter.selector, text: filter.text});
247 } 264 }
248 } 265 }
249 266
250 if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id, 267 if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id,
251 selectors, "standard")) 268 selectors, "standard"))
252 { 269 {
253 inline = true; 270 inline = true;
254 } 271 }
255 272
(...skipping 16 matching lines...) Expand all
272 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, 289 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors,
273 message.groupName, message.appendOnly); 290 message.groupName, message.appendOnly);
274 }); 291 });
275 292
276 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"}) 293 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"})
277 .then(response => response.ok ? response.text() : "") 294 .then(response => response.ok ? response.text() : "")
278 .then(text => 295 .then(text =>
279 { 296 {
280 snippetsLibrarySource = text; 297 snippetsLibrarySource = text;
281 }); 298 });
OLDNEW
« no previous file with comments | « dependencies ('k') | lib/hitLogger.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld