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