How to get the URL of the clicked link?

I am trying to create an add-in through the Mozilla Add-On Builder. I need to know how to get the URL of the left link on the active tab through the add-in and open it in a new tab.

I know that this process involved adding an eventlistener through mod-mod and then using the tabs module, however I cannot understand that the syntax is correct.

Edit: (This is what I have)

var Widget = require("widget").Widget;
var tabs = require('tabs');
var pageMod = require("page-mod");

exports.main = function() {


    pageMod.PageMod({
    include: '*',
    contentScriptWhen: 'ready',
    contentScript: "window.addEventListener('click', function(event) { self.port.emit( 'click',event.target.toString() )},false)",
    onAttach: function(worker) {
        worker.port.on("click", function(urlClicked) {
            tabs.open(urlClicked);

        });
     }

    }); 

};
+1
source share
1 answer

The code you have there is mostly correct and works for me. However, there are two questions with your content script:

  • event.preventDefault(), . , , .
  • , event.target . node .

, script :

window.addEventListener("click", function(event)
{
  var link = event.target;
  while (link && link.localName != "a")
    link = link.parentNode;

  if (link)
  {
    self.port.emit("click", link.href);
    event.preventDefault();
  }
}, false);

script, , contentScript, data/. contentScriptFile :

contentScriptFile: require("self").data.url("contentScript.js"),
0

Source: https://habr.com/ru/post/1546421/


All Articles