| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 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 |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 170 let code = executableCode.get(script); | 170 let code = executableCode.get(script); |
| 171 if (code) | 171 if (code) |
| 172 return code; | 172 return code; |
| 173 | 173 |
| 174 code = compileScript(script, [snippetsLibrarySource]); | 174 code = compileScript(script, [snippetsLibrarySource]); |
| 175 | 175 |
| 176 executableCode.set(script, code); | 176 executableCode.set(script, code); |
| 177 return code; | 177 return code; |
| 178 } | 178 } |
| 179 | 179 |
| 180 function executeScript(script, tabId, frameId, request) | 180 function executeScript(script, tabId, frameId) |
| 181 { | 181 { |
| 182 try | 182 try |
| 183 { | 183 { |
| 184 let details = { | 184 let details = { |
| 185 code: getExecutableCode(script), | 185 code: getExecutableCode(script), |
| 186 matchAboutBlank: true, | 186 matchAboutBlank: true, |
| 187 runAt: "document_start" | 187 runAt: "document_start" |
| 188 }; | 188 }; |
| 189 | 189 |
| 190 // Chrome <50 throws an exception if chrome.tabs.executeScript is called | 190 // Chrome <50 throws an exception if chrome.tabs.executeScript is called |
| 191 // with a frameId of 0. | 191 // with a frameId of 0. |
| 192 if (frameId != 0) | 192 if (frameId != 0) |
| 193 details.frameId = frameId; | 193 details.frameId = frameId; |
| 194 | 194 |
| 195 browser.tabs.executeScript(tabId, details) | 195 return browser.tabs.executeScript(tabId, details) |
| 196 .then(() => | |
|
Manish Jethani
2018/08/27 05:47:36
Ideally this would take the form `.then(() => {},
hub
2018/08/27 12:56:47
this is obsolete now.
| |
| 197 { | |
| 198 let {filter} = request; | |
|
Manish Jethani
2018/08/27 05:47:36
We could read out `request.url` and `request.docDo
hub
2018/08/27 12:56:47
this is obsolete now.
| |
| 199 | |
| 200 let tabIds = [tabId]; | |
| 201 if (filter) | |
| 202 FilterNotifier.emit("filter.hitCount", filter, 0, 0, tabIds); | |
| 203 | |
| 204 logRequest(tabIds, { | |
| 205 url: request.url, type: "SNIPPET", docDomain: request.docDomain | |
| 206 }, filter); | |
| 207 }) | |
| 208 .catch(error => | 196 .catch(error => |
| 209 { | 197 { |
| 210 // 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 |
| 211 // simply ignore the error. | 199 // simply ignore the error. |
| 212 if (error.message == "The frame was removed.") | 200 if (error.message == "The frame was removed.") |
| 213 return; | 201 return; |
| 214 | 202 |
| 215 // 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 |
| 216 // this is exactly, but we simply ignore the error. | 204 // this is exactly, but we simply ignore the error. |
| 217 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)) |
| 218 return; | 206 return; |
| 219 | 207 |
| 220 throw error; | 208 throw error; |
| 221 }); | 209 }); |
| 222 } | 210 } |
| 223 catch (error) | 211 catch (error) |
| 224 { | 212 { |
| 225 // 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 |
| 226 // 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); | |
| 227 } | 217 } |
|
Manish Jethani
2018/08/27 05:47:36
I think it would be cleaner to keep executeScript
hub
2018/08/27 12:56:47
Done.
| |
| 228 } | 218 } |
| 229 | 219 |
| 230 port.on("content.applyFilters", (message, sender) => | 220 port.on("content.applyFilters", (message, sender) => |
| 231 { | 221 { |
| 232 let selectors = []; | 222 let selectors = []; |
| 233 let emulatedPatterns = []; | 223 let emulatedPatterns = []; |
| 234 let trace = HitLogger.hasListener(sender.page.id); | 224 let trace = HitLogger.hasListener(sender.page.id); |
| 235 let inline = !userStyleSheetsSupported; | 225 let inline = !userStyleSheetsSupported; |
| 236 | 226 |
| 237 let {elemhide, snippets} = message.filterTypes || | 227 let {elemhide, snippets} = message.filterTypes || |
| 238 {elemhide: true, snippets: true}; | 228 {elemhide: true, snippets: true}; |
| 239 | 229 |
| 240 if (!checkWhitelisted(sender.page, sender.frame, null, | 230 if (!checkWhitelisted(sender.page, sender.frame, null, |
| 241 RegExpFilter.typeMap.DOCUMENT)) | 231 RegExpFilter.typeMap.DOCUMENT)) |
| 242 { | 232 { |
| 243 let hostname = extractHostFromFrame(sender.frame); | 233 let docDomain = extractHostFromFrame(sender.frame); |
| 244 | 234 |
| 245 if (snippets) | 235 if (snippets) |
| 246 { | 236 { |
| 247 for (let filter of Snippets.getScriptsForDomain(hostname)) | 237 for (let filter of Snippets.getFiltersForDomain(docDomain)) |
| 248 executeScript(filter.script, sender.page.id, sender.frame.id, | 238 { |
| 249 { | 239 executeScript(filter.script, sender.page.id, sender.frame.id).then(() => |
| 250 docDomain: hostname, | 240 { |
|
Sebastian Noack
2018/08/27 04:51:35
Nit: If you rename the variable "hostname" to "doc
hub
2018/08/27 12:56:47
Done.
| |
| 241 let tabIds = [sender.page.id]; | |
| 242 if (filter) | |
| 243 FilterNotifier.emit("filter.hitCount", filter, 0, 0, tabIds); | |
| 244 | |
| 245 logRequest(tabIds, { | |
| 251 url: sender.frame.url.href, | 246 url: sender.frame.url.href, |
| 252 filter | 247 type: "SNIPPET", |
| 253 }); | 248 docDomain |
| 249 }, filter); | |
| 250 }); | |
| 251 } | |
| 254 } | 252 } |
| 255 | 253 |
| 256 if (elemhide && !checkWhitelisted(sender.page, sender.frame, null, | 254 if (elemhide && !checkWhitelisted(sender.page, sender.frame, null, |
| 257 RegExpFilter.typeMap.ELEMHIDE)) | 255 RegExpFilter.typeMap.ELEMHIDE)) |
| 258 { | 256 { |
| 259 let specificOnly = checkWhitelisted(sender.page, sender.frame, null, | 257 let specificOnly = checkWhitelisted(sender.page, sender.frame, null, |
| 260 RegExpFilter.typeMap.GENERICHIDE); | 258 RegExpFilter.typeMap.GENERICHIDE); |
| 261 selectors = ElemHide.getSelectorsForDomain(hostname, specificOnly); | 259 selectors = ElemHide.getSelectorsForDomain(docDomain, specificOnly); |
| 262 | 260 |
| 263 for (let filter of ElemHideEmulation.getRulesForDomain(hostname)) | 261 for (let filter of ElemHideEmulation.getRulesForDomain(docDomain)) |
| 264 emulatedPatterns.push({selector: filter.selector, text: filter.text}); | 262 emulatedPatterns.push({selector: filter.selector, text: filter.text}); |
| 265 } | 263 } |
| 266 } | 264 } |
| 267 | 265 |
| 268 if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id, | 266 if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id, |
| 269 selectors, "standard")) | 267 selectors, "standard")) |
| 270 { | 268 { |
| 271 inline = true; | 269 inline = true; |
| 272 } | 270 } |
| 273 | 271 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 290 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, | 288 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, |
| 291 message.groupName, message.appendOnly); | 289 message.groupName, message.appendOnly); |
| 292 }); | 290 }); |
| 293 | 291 |
| 294 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"}) | 292 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"}) |
| 295 .then(response => response.ok ? response.text() : "") | 293 .then(response => response.ok ? response.text() : "") |
| 296 .then(text => | 294 .then(text => |
| 297 { | 295 { |
| 298 snippetsLibrarySource = text; | 296 snippetsLibrarySource = text; |
| 299 }); | 297 }); |
| LEFT | RIGHT |