| Index: lib/child/subscribeLinks.js |
| =================================================================== |
| rename from chrome/content/subscribeLinkHandler.js |
| rename to lib/child/subscribeLinks.js |
| --- a/chrome/content/subscribeLinkHandler.js |
| +++ b/lib/child/subscribeLinks.js |
| @@ -10,97 +10,108 @@ |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| * GNU General Public License for more details. |
| * |
| * You should have received a copy of the GNU General Public License |
| * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| */ |
| -(function() |
| +let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); |
| + |
| +let {port} = require("messaging"); |
| + |
| +Services.obs.addObserver(onContentWindow, "content-document-global-created", |
| + false); |
| +onShutdown.add(() => |
| { |
| - addEventListener("click", onClick, false); |
| - addMessageListener("AdblockPlus:Shutdown", onShutdown); |
| + Services.obs.removeObserver(onContentWindow, |
| + "content-document-global-created"); |
| +}); |
| - function onShutdown(message) |
| +function onContentWindow(subject, topic, data) |
| +{ |
| + 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!
|
| { |
| - if (message.data == Components.stack.filename) |
| + let eventTarget = subject.QueryInterface(Ci.nsIInterfaceRequestor) |
| + .getInterface(Ci.nsIWebNavigation) |
| + .QueryInterface(Ci.nsIDocShell) |
| + .chromeEventHandler; |
| + eventTarget.addEventListener("click", onClick, true); |
| + } |
| +} |
| + |
| +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
|
| +{ |
| + if (onShutdown.done) |
| + 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
|
| + |
| + // Ignore right-clicks |
| + if (event.button == 2) |
| + return; |
| + |
| + // Search the link associated with the click |
| + let link = event.target; |
| + while (!(link instanceof Ci.nsIDOMHTMLAnchorElement)) |
| + { |
| + link = link.parentNode; |
| + |
| + if (!link) |
| + return; |
| + } |
| + |
| + let queryString = null; |
| + if (link.protocol == "http:" || link.protocol == "https:") |
| + { |
| + if (link.host == "subscribe.adblockplus.org" && link.pathname == "/") |
| + queryString = link.search.substr(1); |
| + } |
| + else |
| + { |
| + // Firefox doesn't populate the "search" property for links with |
| + // non-standard URL schemes so we need to extract the query string |
| + // manually |
| + let match = /^abp:\/*subscribe\/*\?(.*)/i.exec(link.href); |
| + if (match) |
| + queryString = match[1]; |
| + } |
| + |
| + if (!queryString) |
| + return; |
| + |
| + // This is our link - make sure the browser doesn't handle it |
| + event.preventDefault(); |
| + event.stopPropagation(); |
| + |
| + // Decode URL parameters |
| + let title = null; |
| + let url = null; |
| + let mainSubscriptionTitle = null; |
| + let mainSubscriptionURL = null; |
| + for (let param of queryString.split("&")) |
| + { |
| + let parts = param.split("=", 2); |
| + if (parts.length != 2 || !/\S/.test(parts[1])) |
| + continue; |
| + switch (parts[0]) |
| { |
| - removeEventListener("click", onClick, false); |
| - removeMessageListener("AdblockPlus:Shutdown", onShutdown); |
| + case "title": |
| + title = decodeURIComponent(parts[1]); |
| + break; |
| + case "location": |
| + url = decodeURIComponent(parts[1]); |
| + break; |
| + case "requiresTitle": |
| + mainSubscriptionTitle = decodeURIComponent(parts[1]); |
| + break; |
| + case "requiresLocation": |
| + mainSubscriptionURL = decodeURIComponent(parts[1]); |
| + break; |
| } |
| } |
| - function onClick(event) |
| - { |
| - // Ignore right-clicks |
| - if (event.button == 2) |
| - return; |
| - |
| - // Search the link associated with the click |
| - let link = event.target; |
| - while (!(link instanceof content.HTMLAnchorElement)) |
| - { |
| - link = link.parentNode; |
| - |
| - if (!link) |
| - return; |
| - } |
| - |
| - let queryString = null; |
| - if (link.protocol == "http:" || link.protocol == "https:") |
| - { |
| - if (link.host == "subscribe.adblockplus.org" && link.pathname == "/") |
| - queryString = link.search.substr(1); |
| - } |
| - else |
| - { |
| - // Firefox doesn't populate the "search" property for links with |
| - // non-standard URL schemes so we need to extract the query string |
| - // manually |
| - let match = /^abp:\/*subscribe\/*\?(.*)/i.exec(link.href); |
| - if (match) |
| - queryString = match[1]; |
| - } |
| - |
| - if (!queryString) |
| - return; |
| - |
| - // This is our link - make sure the browser doesn't handle it |
| - event.preventDefault(); |
| - event.stopPropagation(); |
| - |
| - // Decode URL parameters |
| - let title = null; |
| - let url = null; |
| - let mainSubscriptionTitle = null; |
| - let mainSubscriptionURL = null; |
| - for (let param of queryString.split("&")) |
| - { |
| - let parts = param.split("=", 2); |
| - if (parts.length != 2 || !/\S/.test(parts[1])) |
| - continue; |
| - switch (parts[0]) |
| - { |
| - case "title": |
| - title = decodeURIComponent(parts[1]); |
| - break; |
| - case "location": |
| - url = decodeURIComponent(parts[1]); |
| - break; |
| - case "requiresTitle": |
| - mainSubscriptionTitle = decodeURIComponent(parts[1]); |
| - break; |
| - case "requiresLocation": |
| - mainSubscriptionURL = decodeURIComponent(parts[1]); |
| - break; |
| - } |
| - } |
| - |
| - sendAsyncMessage("AdblockPlus:SubscribeLink", { |
| - title: title, |
| - url: url, |
| - mainSubscriptionTitle: mainSubscriptionTitle, |
| - mainSubscriptionURL: mainSubscriptionURL |
| - }); |
| - } |
| -})(); |
| - |
| + port.emit("subscribeLinkClick", { |
| + title: title, |
| + url: url, |
| + mainSubscriptionTitle: mainSubscriptionTitle, |
| + mainSubscriptionURL: mainSubscriptionURL |
| + }); |
| +} |