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: Put the actual revision Created Aug. 27, 2018, 2:22 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 and simply reject the promise.
Manish Jethani 2018/08/27 15:40:49 I think this would read better as "... why we catc
hub 2018/08/27 16:42:04 Done.
215 return Promise.reject(error);
214 } 216 }
215 } 217 }
216 218
217 port.on("content.applyFilters", (message, sender) => 219 port.on("content.applyFilters", (message, sender) =>
218 { 220 {
219 let selectors = []; 221 let selectors = [];
220 let emulatedPatterns = []; 222 let emulatedPatterns = [];
221 let trace = HitLogger.hasListener(sender.page.id); 223 let trace = HitLogger.hasListener(sender.page.id);
222 let inline = !userStyleSheetsSupported; 224 let inline = !userStyleSheetsSupported;
223 225
224 let {elemhide, snippets} = message.filterTypes || 226 let {elemhide, snippets} = message.filterTypes ||
225 {elemhide: true, snippets: true}; 227 {elemhide: true, snippets: true};
226 228
227 if (!checkWhitelisted(sender.page, sender.frame, null, 229 if (!checkWhitelisted(sender.page, sender.frame, null,
228 RegExpFilter.typeMap.DOCUMENT)) 230 RegExpFilter.typeMap.DOCUMENT))
229 { 231 {
230 let hostname = extractHostFromFrame(sender.frame); 232 let docDomain = extractHostFromFrame(sender.frame);
231 233
232 if (snippets) 234 if (snippets)
233 { 235 {
234 for (let script of Snippets.getScriptsForDomain(hostname)) 236 for (let filter of Snippets.getFiltersForDomain(docDomain))
Manish Jethani 2018/08/27 15:40:49 Nit: As a convention we have been adding braces ar
hub 2018/08/27 16:42:04 Done.
235 executeScript(script, sender.page.id, sender.frame.id); 237 executeScript(filter.script, sender.page.id, sender.frame.id)
238 .then(() =>
239 {
240 let tabIds = [sender.page.id];
241 if (filter)
242 FilterNotifier.emit("filter.hitCount", filter, 0, 0, tabIds);
243
244 logRequest(tabIds, {
245 url: sender.frame.url.href, type: "SNIPPET",
Manish Jethani 2018/08/27 15:40:49 Nit: When an object literal spans multiple lines,
hub 2018/08/27 16:42:04 Done.
246 docDomain
247 }, filter);
248 });
236 } 249 }
237 250
238 if (elemhide && !checkWhitelisted(sender.page, sender.frame, null, 251 if (elemhide && !checkWhitelisted(sender.page, sender.frame, null,
239 RegExpFilter.typeMap.ELEMHIDE)) 252 RegExpFilter.typeMap.ELEMHIDE))
240 { 253 {
241 let specificOnly = checkWhitelisted(sender.page, sender.frame, null, 254 let specificOnly = checkWhitelisted(sender.page, sender.frame, null,
242 RegExpFilter.typeMap.GENERICHIDE); 255 RegExpFilter.typeMap.GENERICHIDE);
243 selectors = ElemHide.getSelectorsForDomain(hostname, specificOnly); 256 selectors = ElemHide.getSelectorsForDomain(docDomain, specificOnly);
244 257
245 for (let filter of ElemHideEmulation.getRulesForDomain(hostname)) 258 for (let filter of ElemHideEmulation.getRulesForDomain(docDomain))
246 emulatedPatterns.push({selector: filter.selector, text: filter.text}); 259 emulatedPatterns.push({selector: filter.selector, text: filter.text});
247 } 260 }
248 } 261 }
249 262
250 if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id, 263 if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id,
251 selectors, "standard")) 264 selectors, "standard"))
252 { 265 {
253 inline = true; 266 inline = true;
254 } 267 }
255 268
(...skipping 16 matching lines...) Expand all
272 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, 285 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors,
273 message.groupName, message.appendOnly); 286 message.groupName, message.appendOnly);
274 }); 287 });
275 288
276 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"}) 289 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"})
277 .then(response => response.ok ? response.text() : "") 290 .then(response => response.ok ? response.text() : "")
278 .then(text => 291 .then(text =>
279 { 292 {
280 snippetsLibrarySource = text; 293 snippetsLibrarySource = text;
281 }); 294 });
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