0

If I have code similar to:

var filter = {
  url:
  [
    {hostContains: "example.com"},
    {hostPrefix: "developer"}
  ]
}

function logOnDOMContentLoaded(details) {
  console.log("onDOMContentLoaded: " + details.url);
}

browser.webNavigation.onDOMContentLoaded.addListener(logOnDOMContentLoaded, filter);

The details.url returns the loaded URL. What if I want the original URL that the user entered in the URL bar? is there any way to get this?

user6875880
  • 651
  • 1
  • 7
  • 17

1 Answers1

0

As turned out from the post HERE, it is possible to extract the URL using pure javascript code. This code snippet will do the purpose. You feed it with the string that contains the error URL (the string), and the variable name that contains the request URL (sub-string) which is in the webNavigation.onDOMContentLoaded.addListener is named u. So, by calling:

var url = getParameterByName("u",str);

I could get the request URL (website URL).

function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}
user6875880
  • 651
  • 1
  • 7
  • 17