| 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-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 url */ | 18 /** @module url */ |
| 19 | 19 |
| 20 "use strict"; | 20 import {getDomain} from "tldjs"; |
| 21 import punycode from "punycode"; |
| 21 | 22 |
| 22 const {getDomain} = require("tldjs"); | |
| 23 const punycode = require("punycode"); | |
| 24 | |
| 25 let getDecodedHostname = | |
| 26 /** | 23 /** |
| 27 * Gets the IDN-decoded hostname from a URL object. | 24 * Gets the IDN-decoded hostname from a URL object. |
| 28 * | 25 * |
| 29 * @param {URL} url | 26 * @param {URL} url |
| 30 * @return {string} | 27 * @return {string} |
| 31 * @static | 28 * @static |
| 32 */ | 29 */ |
| 33 exports.getDecodedHostname = url => | 30 export const getDecodedHostname = url => |
| 34 { | 31 { |
| 35 let {hostname} = url; | 32 let {hostname} = url; |
| 36 | 33 |
| 37 if (hostname.indexOf("xn--") == -1) | 34 if (hostname.indexOf("xn--") == -1) |
| 38 return hostname; | 35 return hostname; |
| 39 | 36 |
| 40 return punycode.toUnicode(hostname); | 37 return punycode.toUnicode(hostname); |
| 41 }; | 38 }; |
| 42 | 39 |
| 43 /** | 40 /** |
| 44 * Gets the IDN-decoded hostname from the URL of a frame. | 41 * Gets the IDN-decoded hostname from the URL of a frame. |
| 45 * If the URL don't have host information (like "about:blank" | 42 * If the URL don't have host information (like "about:blank" |
| 46 * and "data:" URLs) it falls back to the parent frame. | 43 * and "data:" URLs) it falls back to the parent frame. |
| 47 * | 44 * |
| 48 * @param {Frame} frame | 45 * @param {Frame} frame |
| 49 * @return {string} | 46 * @return {string} |
| 50 */ | 47 */ |
| 51 exports.extractHostFromFrame = frame => | 48 export const extractHostFromFrame = frame => |
| 52 { | 49 { |
| 53 for (; frame; frame = frame.parent) | 50 for (; frame; frame = frame.parent) |
| 54 { | 51 { |
| 55 let hostname = getDecodedHostname(frame.url); | 52 let hostname = getDecodedHostname(frame.url); |
| 56 if (hostname) | 53 if (hostname) |
| 57 return hostname; | 54 return hostname; |
| 58 } | 55 } |
| 59 | 56 |
| 60 return ""; | 57 return ""; |
| 61 }; | 58 }; |
| 62 | 59 |
| 63 /** | 60 /** |
| 64 * Converts a URL object into a string. For HTTP(S) URLs | 61 * Converts a URL object into a string. For HTTP(S) URLs |
| 65 * the hostname gets IDN-decoded and the hash is stripped. | 62 * the hostname gets IDN-decoded and the hash is stripped. |
| 66 * | 63 * |
| 67 * @param {URL} url | 64 * @param {URL} url |
| 68 * @return {string} | 65 * @return {string} |
| 69 */ | 66 */ |
| 70 exports.stringifyURL = url => | 67 export const stringifyURL = url => |
| 71 { | 68 { |
| 72 let {protocol, href} = url; | 69 let {protocol, href} = url; |
| 73 | 70 |
| 74 if (protocol == "http:" || protocol == "https:") | 71 if (protocol == "http:" || protocol == "https:") |
| 75 { | 72 { |
| 76 let {hostname} = url; | 73 let {hostname} = url; |
| 77 if (hostname.indexOf("xn--") != -1) | 74 if (hostname.indexOf("xn--") != -1) |
| 78 href = href.replace(hostname, punycode.toUnicode(hostname)); | 75 href = href.replace(hostname, punycode.toUnicode(hostname)); |
| 79 | 76 |
| 80 let hash = href.indexOf("#"); | 77 let hash = href.indexOf("#"); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 96 return hostname.indexOf(":") == -1; | 93 return hostname.indexOf(":") == -1; |
| 97 } | 94 } |
| 98 | 95 |
| 99 /** | 96 /** |
| 100 * Checks whether the request's origin is different from the document's origin. | 97 * Checks whether the request's origin is different from the document's origin. |
| 101 * | 98 * |
| 102 * @param {URL} url The request URL | 99 * @param {URL} url The request URL |
| 103 * @param {string} documentHost The IDN-decoded hostname of the document | 100 * @param {string} documentHost The IDN-decoded hostname of the document |
| 104 * @return {Boolean} | 101 * @return {Boolean} |
| 105 */ | 102 */ |
| 106 exports.isThirdParty = (url, documentHost) => | 103 export const isThirdParty = (url, documentHost) => |
| 107 { | 104 { |
| 108 let requestHost = getDecodedHostname(url).replace(/\.+$/, ""); | 105 let requestHost = getDecodedHostname(url).replace(/\.+$/, ""); |
| 109 documentHost = documentHost.replace(/\.+$/, ""); | 106 documentHost = documentHost.replace(/\.+$/, ""); |
| 110 | 107 |
| 111 if (requestHost == documentHost) | 108 if (requestHost == documentHost) |
| 112 return false; | 109 return false; |
| 113 | 110 |
| 114 if (!isDomain(requestHost) || !isDomain(documentHost)) | 111 if (!isDomain(requestHost) || !isDomain(documentHost)) |
| 115 return true; | 112 return true; |
| 116 | 113 |
| 117 return getDomain(requestHost) != getDomain(documentHost); | 114 return getDomain(requestHost) != getDomain(documentHost); |
| 118 }; | 115 }; |
| OLD | NEW |