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 (function() | 18 let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); |
| 19 |
| 20 let {port} = require("messaging"); |
| 21 |
| 22 Services.obs.addObserver(onContentWindow, "content-document-global-created", |
| 23 false); |
| 24 onShutdown.add(() => |
19 { | 25 { |
20 addEventListener("click", onClick, false); | 26 Services.obs.removeObserver(onContentWindow, |
21 addMessageListener("AdblockPlus:Shutdown", onShutdown); | 27 "content-document-global-created"); |
| 28 }); |
22 | 29 |
23 function onShutdown(message) | 30 function onContentWindow(subject, topic, data) |
| 31 { |
| 32 if (subject instanceof Ci.nsIDOMWindow && subject.top == subject) |
24 { | 33 { |
25 if (message.data == Components.stack.filename) | 34 let eventTarget = subject.QueryInterface(Ci.nsIInterfaceRequestor) |
| 35 .getInterface(Ci.nsIWebNavigation) |
| 36 .QueryInterface(Ci.nsIDocShell) |
| 37 .chromeEventHandler; |
| 38 if (eventTarget) |
| 39 eventTarget.addEventListener("click", onClick, true); |
| 40 } |
| 41 } |
| 42 |
| 43 function onClick(event) |
| 44 { |
| 45 if (onShutdown.done) |
| 46 return; |
| 47 |
| 48 // Ignore right-clicks |
| 49 if (event.button == 2) |
| 50 return; |
| 51 |
| 52 // Search the link associated with the click |
| 53 let link = event.target; |
| 54 while (!(link instanceof Ci.nsIDOMHTMLAnchorElement)) |
| 55 { |
| 56 link = link.parentNode; |
| 57 |
| 58 if (!link) |
| 59 return; |
| 60 } |
| 61 |
| 62 let queryString = null; |
| 63 if (link.protocol == "http:" || link.protocol == "https:") |
| 64 { |
| 65 if (link.host == "subscribe.adblockplus.org" && link.pathname == "/") |
| 66 queryString = link.search.substr(1); |
| 67 } |
| 68 else |
| 69 { |
| 70 // Firefox doesn't populate the "search" property for links with |
| 71 // non-standard URL schemes so we need to extract the query string |
| 72 // manually |
| 73 let match = /^abp:\/*subscribe\/*\?(.*)/i.exec(link.href); |
| 74 if (match) |
| 75 queryString = match[1]; |
| 76 } |
| 77 |
| 78 if (!queryString) |
| 79 return; |
| 80 |
| 81 // This is our link - make sure the browser doesn't handle it |
| 82 event.preventDefault(); |
| 83 event.stopPropagation(); |
| 84 |
| 85 // Decode URL parameters |
| 86 let title = null; |
| 87 let url = null; |
| 88 let mainSubscriptionTitle = null; |
| 89 let mainSubscriptionURL = null; |
| 90 for (let param of queryString.split("&")) |
| 91 { |
| 92 let parts = param.split("=", 2); |
| 93 if (parts.length != 2 || !/\S/.test(parts[1])) |
| 94 continue; |
| 95 switch (parts[0]) |
26 { | 96 { |
27 removeEventListener("click", onClick, false); | 97 case "title": |
28 removeMessageListener("AdblockPlus:Shutdown", onShutdown); | 98 title = decodeURIComponent(parts[1]); |
| 99 break; |
| 100 case "location": |
| 101 url = decodeURIComponent(parts[1]); |
| 102 break; |
| 103 case "requiresTitle": |
| 104 mainSubscriptionTitle = decodeURIComponent(parts[1]); |
| 105 break; |
| 106 case "requiresLocation": |
| 107 mainSubscriptionURL = decodeURIComponent(parts[1]); |
| 108 break; |
29 } | 109 } |
30 } | 110 } |
31 | 111 |
32 function onClick(event) | 112 port.emit("subscribeLinkClick", { |
33 { | 113 title: title, |
34 // Ignore right-clicks | 114 url: url, |
35 if (event.button == 2) | 115 mainSubscriptionTitle: mainSubscriptionTitle, |
36 return; | 116 mainSubscriptionURL: mainSubscriptionURL |
37 | 117 }); |
38 // Search the link associated with the click | 118 } |
39 let link = event.target; | |
40 while (!(link instanceof content.HTMLAnchorElement)) | |
41 { | |
42 link = link.parentNode; | |
43 | |
44 if (!link) | |
45 return; | |
46 } | |
47 | |
48 let queryString = null; | |
49 if (link.protocol == "http:" || link.protocol == "https:") | |
50 { | |
51 if (link.host == "subscribe.adblockplus.org" && link.pathname == "/") | |
52 queryString = link.search.substr(1); | |
53 } | |
54 else | |
55 { | |
56 // Firefox doesn't populate the "search" property for links with | |
57 // non-standard URL schemes so we need to extract the query string | |
58 // manually | |
59 let match = /^abp:\/*subscribe\/*\?(.*)/i.exec(link.href); | |
60 if (match) | |
61 queryString = match[1]; | |
62 } | |
63 | |
64 if (!queryString) | |
65 return; | |
66 | |
67 // This is our link - make sure the browser doesn't handle it | |
68 event.preventDefault(); | |
69 event.stopPropagation(); | |
70 | |
71 // Decode URL parameters | |
72 let title = null; | |
73 let url = null; | |
74 let mainSubscriptionTitle = null; | |
75 let mainSubscriptionURL = null; | |
76 for (let param of queryString.split("&")) | |
77 { | |
78 let parts = param.split("=", 2); | |
79 if (parts.length != 2 || !/\S/.test(parts[1])) | |
80 continue; | |
81 switch (parts[0]) | |
82 { | |
83 case "title": | |
84 title = decodeURIComponent(parts[1]); | |
85 break; | |
86 case "location": | |
87 url = decodeURIComponent(parts[1]); | |
88 break; | |
89 case "requiresTitle": | |
90 mainSubscriptionTitle = decodeURIComponent(parts[1]); | |
91 break; | |
92 case "requiresLocation": | |
93 mainSubscriptionURL = decodeURIComponent(parts[1]); | |
94 break; | |
95 } | |
96 } | |
97 | |
98 sendAsyncMessage("AdblockPlus:SubscribeLink", { | |
99 title: title, | |
100 url: url, | |
101 mainSubscriptionTitle: mainSubscriptionTitle, | |
102 mainSubscriptionURL: mainSubscriptionURL | |
103 }); | |
104 } | |
105 })(); | |
106 | |
OLD | NEW |