| OLD | NEW |
| 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 let {extractHostFromFrame, stringifyURL, isThirdParty} = require("url"); | 20 let {extractHostFromFrame, stringifyURL, isThirdParty} = require("url"); |
| 21 let {getKey, isFrameWhitelisted} = require("whitelisting"); | 21 let {getKey, checkWhitelisted} = require("whitelisting"); |
| 22 let {defaultMatcher} = require("matcher"); | 22 let {defaultMatcher} = require("matcher"); |
| 23 let {RegExpFilter} = require("filterClasses"); | 23 let {RegExpFilter} = require("filterClasses"); |
| 24 | 24 |
| 25 function isValidString(s) { | 25 function isValidString(s) { |
| 26 return s && s.indexOf("\0") == -1; | 26 return s && s.indexOf("\0") == -1; |
| 27 } | 27 } |
| 28 | 28 |
| 29 function escapeChar(chr) | 29 function escapeChar(chr) |
| 30 { | 30 { |
| 31 let code = chr.charCodeAt(0); | 31 let code = chr.charCodeAt(0); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 * hiding filters: {filters: [...], selectors: [...]} | 85 * hiding filters: {filters: [...], selectors: [...]} |
| 86 */ | 86 */ |
| 87 exports.composeFilters = function(details) | 87 exports.composeFilters = function(details) |
| 88 { | 88 { |
| 89 let filters = []; | 89 let filters = []; |
| 90 let selectors = []; | 90 let selectors = []; |
| 91 | 91 |
| 92 let page = details.page; | 92 let page = details.page; |
| 93 let frame = details.frame; | 93 let frame = details.frame; |
| 94 | 94 |
| 95 if (!isFrameWhitelisted(page, frame, RegExpFilter.typeMap.DOCUMENT)) | 95 if (!checkWhitelisted(page, frame)) |
| 96 { | 96 { |
| 97 let typeMask = RegExpFilter.typeMap[details.type]; | 97 let typeMask = RegExpFilter.typeMap[details.type]; |
| 98 let docDomain = extractHostFromFrame(frame); | 98 let docDomain = extractHostFromFrame(frame); |
| 99 | 99 |
| 100 // Add a blocking filter for each URL of the element that can be blocked | 100 // Add a blocking filter for each URL of the element that can be blocked |
| 101 for (let url of details.urls) | 101 for (let url of details.urls) |
| 102 { | 102 { |
| 103 let urlObj = new URL(url, details.baseURL); | 103 let urlObj = new URL(url, details.baseURL); |
| 104 url = stringifyURL(urlObj); | 104 url = stringifyURL(urlObj); |
| 105 | 105 |
| 106 let filter = defaultMatcher.whitelist.matchesAny( | 106 let filter = defaultMatcher.whitelist.matchesAny( |
| 107 url, typeMask, docDomain, | 107 url, typeMask, docDomain, |
| 108 isThirdParty(urlObj, docDomain), | 108 isThirdParty(urlObj, docDomain), |
| 109 getKey(page, frame) | 109 getKey(page, frame) |
| 110 ); | 110 ); |
| 111 | 111 |
| 112 if (!filter) | 112 if (!filter) |
| 113 { | 113 { |
| 114 let filterText = url.replace(/^[\w\-]+:\/+(?:www\.)?/, "||"); | 114 let filterText = url.replace(/^[\w\-]+:\/+(?:www\.)?/, "||"); |
| 115 | 115 |
| 116 if (filters.indexOf(filterText) == -1) | 116 if (filters.indexOf(filterText) == -1) |
| 117 filters.push(filterText); | 117 filters.push(filterText); |
| 118 } | 118 } |
| 119 } | 119 } |
| 120 | 120 |
| 121 // If we couldn't generate any blocking filters, fallback to element hiding | 121 // If we couldn't generate any blocking filters, fallback to element hiding |
| 122 let selectors = []; | 122 let selectors = []; |
| 123 if (filters.length == 0 && !isFrameWhitelisted(page, frame, RegExpFilter.typ
eMap.ELEMHIDE)) | 123 if (filters.length == 0 && !checkWhitelisted(page, frame, RegExpFilter.typeM
ap.ELEMHIDE)) |
| 124 { | 124 { |
| 125 // Generate CSS selectors based on the element's "id" and "class" attribut
e | 125 // Generate CSS selectors based on the element's "id" and "class" attribut
e |
| 126 if (isValidString(details.id)) | 126 if (isValidString(details.id)) |
| 127 selectors.push("#" + escapeCSS(details.id)); | 127 selectors.push("#" + escapeCSS(details.id)); |
| 128 | 128 |
| 129 let classes = details.classes.filter(isValidString); | 129 let classes = details.classes.filter(isValidString); |
| 130 if (classes.length > 0) | 130 if (classes.length > 0) |
| 131 selectors.push(classes.map(c => "." + escapeCSS(c)).join("")); | 131 selectors.push(classes.map(c => "." + escapeCSS(c)).join("")); |
| 132 | 132 |
| 133 // If there is a "src" attribute, specifiying a URL that we can't block, | 133 // If there is a "src" attribute, specifiying a URL that we can't block, |
| 134 // generate a CSS selector matching the "src" attribute | 134 // generate a CSS selector matching the "src" attribute |
| 135 if (isValidString(details.src)) | 135 if (isValidString(details.src)) |
| 136 selectors.push(escapeCSS(details.tagName) + "[src=" + quoteCSS(details.s
rc) + "]"); | 136 selectors.push(escapeCSS(details.tagName) + "[src=" + quoteCSS(details.s
rc) + "]"); |
| 137 | 137 |
| 138 // As last resort, if there is a "style" attribute, and we couldn't genera
te | 138 // As last resort, if there is a "style" attribute, and we couldn't genera
te |
| 139 // any filters so far, generate a CSS selector matching the "style" attrib
ute | 139 // any filters so far, generate a CSS selector matching the "style" attrib
ute |
| 140 if (isValidString(details.style) && selectors.length == 0 && filters.lengt
h == 0) | 140 if (isValidString(details.style) && selectors.length == 0 && filters.lengt
h == 0) |
| 141 selectors.push(escapeCSS(details.tagName) + "[style=" + quoteCSS(details
.style) + "]"); | 141 selectors.push(escapeCSS(details.tagName) + "[style=" + quoteCSS(details
.style) + "]"); |
| 142 | 142 |
| 143 // Add an element hiding filter for each generated CSS selector | 143 // Add an element hiding filter for each generated CSS selector |
| 144 for (let selector of selectors) | 144 for (let selector of selectors) |
| 145 filters.push(docDomain.replace(/^www\./, "") + "##" + selector); | 145 filters.push(docDomain.replace(/^www\./, "") + "##" + selector); |
| 146 } | 146 } |
| 147 } | 147 } |
| 148 | 148 |
| 149 return {filters: filters, selectors: selectors}; | 149 return {filters: filters, selectors: selectors}; |
| 150 }; | 150 }; |
| OLD | NEW |