sharataka
sharataka

Reputation: 5132

How to get the current tab's referrer from a Chrome extension?

From my chrome extension, I am trying to get the referrer link when a user navigates to Amazon.com from another website but I am running into some issues.

I am using accessing the current html page from chrome extension html-page-from-chrome-extension?noredirect=1&lq=1 and Accessing Current Tab DOM Object from "popup.html"? from-popup-html but still have issues.

My js in confirmation.js:

chrome.tabs.onUpdated.addListener( function (tabId, changeInfo, tab) {
  if (changeInfo.status == 'loading') {

          console.log(tab);

          console.log(document);
          //console.log(document.referrer);


  }
});

Currently, when this outputs the DOM of popup.html; not the current DOM of the tab the user is on. How do I get the document of the current tab the user is on?

Console.log(tab) provides the information for the current tab the user is on but I do not see any referrer attribute here.

Any advice on how I should solve this?

My manifest.json:

"permissions": [
    "activeTab",
    "tabs",
    "storage",
    "notifications"
],
"content_scripts": [
{
"matches": ["https://www.amazon.com/*"],
    "js": ["confirmation.js"]
}
]

Upvotes: 11

Views: 2976

Answers (1)

Kapil Barad
Kapil Barad

Reputation: 717

Try this...

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    if(changeInfo.state === 'complete'){
        chrome.tabs.executeScript(tabId, {
            code: "document.referrer;"
        },
        function(result) {
            // Here, you'll get the referrer
        });
    }
});

Upvotes: 11

Related Questions