Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/child/subscribeLinks.js

Issue 29338861: Issue 3851 - Implement subscribe link handling via process scripts (Closed)
Patch Set: Created March 21, 2016, 7:25 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/child/main.js ('k') | lib/ui.js » ('j') | lib/ui.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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)
Thomas Greiner 2016/04/18 15:16:25 So we're not listening to clicks in subdocuments?
Wladimir Palant 2016/04/18 15:38:40 We do, clicks will bubble up from frames. I verifi
Thomas Greiner 2016/04/18 17:49:43 Thanks for verifying!
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 eventTarget.addEventListener("click", onClick, true);
39 }
40 }
41
42 function onClick(event)
Wladimir Palant 2016/03/21 19:27:20 It's mostly an indentation change for this functio
Wladimir Palant 2016/03/21 19:29:20 Heh, forgot the sendAsyncMessage call at the botto
43 {
44 if (onShutdown.done)
45 return;
Erik 2016/04/06 03:11:41 This will stop the function from running after shu
Wladimir Palant 2016/04/18 15:38:40 Yes, the function itself is leaked - getting the e
46
47 // Ignore right-clicks
48 if (event.button == 2)
49 return;
50
51 // Search the link associated with the click
52 let link = event.target;
53 while (!(link instanceof Ci.nsIDOMHTMLAnchorElement))
54 {
55 link = link.parentNode;
56
57 if (!link)
58 return;
59 }
60
61 let queryString = null;
62 if (link.protocol == "http:" || link.protocol == "https:")
63 {
64 if (link.host == "subscribe.adblockplus.org" && link.pathname == "/")
65 queryString = link.search.substr(1);
66 }
67 else
68 {
69 // Firefox doesn't populate the "search" property for links with
70 // non-standard URL schemes so we need to extract the query string
71 // manually
72 let match = /^abp:\/*subscribe\/*\?(.*)/i.exec(link.href);
73 if (match)
74 queryString = match[1];
75 }
76
77 if (!queryString)
78 return;
79
80 // This is our link - make sure the browser doesn't handle it
81 event.preventDefault();
82 event.stopPropagation();
83
84 // Decode URL parameters
85 let title = null;
86 let url = null;
87 let mainSubscriptionTitle = null;
88 let mainSubscriptionURL = null;
89 for (let param of queryString.split("&"))
90 {
91 let parts = param.split("=", 2);
92 if (parts.length != 2 || !/\S/.test(parts[1]))
93 continue;
94 switch (parts[0])
26 { 95 {
27 removeEventListener("click", onClick, false); 96 case "title":
28 removeMessageListener("AdblockPlus:Shutdown", onShutdown); 97 title = decodeURIComponent(parts[1]);
98 break;
99 case "location":
100 url = decodeURIComponent(parts[1]);
101 break;
102 case "requiresTitle":
103 mainSubscriptionTitle = decodeURIComponent(parts[1]);
104 break;
105 case "requiresLocation":
106 mainSubscriptionURL = decodeURIComponent(parts[1]);
107 break;
29 } 108 }
30 } 109 }
31 110
32 function onClick(event) 111 port.emit("subscribeLinkClick", {
33 { 112 title: title,
34 // Ignore right-clicks 113 url: url,
35 if (event.button == 2) 114 mainSubscriptionTitle: mainSubscriptionTitle,
36 return; 115 mainSubscriptionURL: mainSubscriptionURL
37 116 });
38 // Search the link associated with the click 117 }
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
OLDNEW
« no previous file with comments | « lib/child/main.js ('k') | lib/ui.js » ('j') | lib/ui.js » ('J')

Powered by Google App Engine
This is Rietveld