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 "use strict"; | 18 "use strict"; |
19 | 19 |
20 if (document instanceof HTMLDocument) | 20 function setupSubscriptionLink() |
21 { | 21 { |
22 document.addEventListener("click", event => | 22 if (document instanceof HTMLDocument) |
23 { | 23 { |
24 // Ignore right-clicks | 24 document.addEventListener("click", event => |
25 if (event.button == 2) | 25 { |
26 return; | 26 // Ignore right-clicks |
| 27 if (event.button == 2) |
| 28 return; |
27 | 29 |
28 // Ignore simulated clicks. | 30 // Ignore simulated clicks. |
29 if (event.isTrusted == false) | 31 if (event.isTrusted == false) |
30 return; | 32 return; |
31 | 33 |
32 // Search the link associated with the click | 34 // Search the link associated with the click |
33 let link = event.target; | 35 let link = event.target; |
34 while (!(link instanceof HTMLAnchorElement)) | 36 while (!(link instanceof HTMLAnchorElement)) |
35 { | 37 { |
36 link = link.parentNode; | 38 link = link.parentNode; |
37 | 39 |
38 if (!link) | 40 if (!link) |
| 41 return; |
| 42 } |
| 43 |
| 44 let queryString = null; |
| 45 if (link.protocol == "http:" || link.protocol == "https:") |
| 46 { |
| 47 if (link.host == "subscribe.adblockplus.org" && link.pathname == "/") |
| 48 queryString = link.search.substr(1); |
| 49 } |
| 50 else |
| 51 { |
| 52 // Firefox 51 doesn't seem to populate the "search" property for |
| 53 // links with non-standard URL schemes so we need to extract the query |
| 54 // string manually. |
| 55 let match = /^abp:\/*subscribe\/*\?(.*)/i.exec(link.href); |
| 56 if (match) |
| 57 queryString = match[1]; |
| 58 } |
| 59 |
| 60 if (!queryString) |
39 return; | 61 return; |
40 } | |
41 | 62 |
42 let queryString = null; | 63 // This is our link - make sure the browser doesn't handle it |
43 if (link.protocol == "http:" || link.protocol == "https:") | 64 event.preventDefault(); |
44 { | 65 event.stopPropagation(); |
45 if (link.host == "subscribe.adblockplus.org" && link.pathname == "/") | |
46 queryString = link.search.substr(1); | |
47 } | |
48 else | |
49 { | |
50 // Firefox 51 doesn't seem to populate the "search" property for | |
51 // links with non-standard URL schemes so we need to extract the query | |
52 // string manually. | |
53 let match = /^abp:\/*subscribe\/*\?(.*)/i.exec(link.href); | |
54 if (match) | |
55 queryString = match[1]; | |
56 } | |
57 | 66 |
58 if (!queryString) | 67 // Decode URL parameters |
59 return; | 68 let title = null; |
| 69 let url = null; |
| 70 for (let param of queryString.split("&")) |
| 71 { |
| 72 let parts = param.split("=", 2); |
| 73 if (parts.length != 2 || !/\S/.test(parts[1])) |
| 74 continue; |
| 75 switch (parts[0]) |
| 76 { |
| 77 case "title": |
| 78 title = decodeURIComponent(parts[1]); |
| 79 break; |
| 80 case "location": |
| 81 url = decodeURIComponent(parts[1]); |
| 82 break; |
| 83 } |
| 84 } |
| 85 if (!url) |
| 86 return; |
60 | 87 |
61 // This is our link - make sure the browser doesn't handle it | 88 // Default title to the URL |
62 event.preventDefault(); | 89 if (!title) |
63 event.stopPropagation(); | 90 title = url; |
64 | 91 |
65 // Decode URL parameters | 92 // Trim spaces in title and URL |
66 let title = null; | 93 title = title.trim(); |
67 let url = null; | 94 url = url.trim(); |
68 for (let param of queryString.split("&")) | 95 if (!/^(https?|ftp):/.test(url)) |
69 { | 96 return; |
70 let parts = param.split("=", 2); | |
71 if (parts.length != 2 || !/\S/.test(parts[1])) | |
72 continue; | |
73 switch (parts[0]) | |
74 { | |
75 case "title": | |
76 title = decodeURIComponent(parts[1]); | |
77 break; | |
78 case "location": | |
79 url = decodeURIComponent(parts[1]); | |
80 break; | |
81 } | |
82 } | |
83 if (!url) | |
84 return; | |
85 | 97 |
86 // Default title to the URL | 98 ext.backgroundPage.sendMessage({ |
87 if (!title) | 99 type: "subscriptions.add", |
88 title = url; | 100 title, |
| 101 url, |
| 102 confirm: true |
| 103 }); |
| 104 }, true); |
| 105 } |
| 106 } |
89 | 107 |
90 // Trim spaces in title and URL | 108 exports.setupSubscriptionLink = setupSubscriptionLink; |
91 title = title.trim(); | |
92 url = url.trim(); | |
93 if (!/^(https?|ftp):/.test(url)) | |
94 return; | |
95 | |
96 ext.backgroundPage.sendMessage({ | |
97 type: "subscriptions.add", | |
98 title, | |
99 url, | |
100 confirm: true | |
101 }); | |
102 }, true); | |
103 } | |
OLD | NEW |