Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"]
}
]
like image 550
sharataka Avatar asked Dec 22 '17 20:12

sharataka


People also ask

How do I find my referrer URL in Chrome?

To check the Referer in action go to Inspect Element -> Network check the request header for Referer like below. Referer header is highlighted. Supported Browsers: The browsers are compatible with HTTP header Referer are listed below: Google Chrome.

How do I capture a referrer URL?

$_SERVER['HTTP_REFERER'] will give you the referrer page's URL if there exists any. If users use a bookmark or directly visit your site by manually typing in the URL, http_referer will be empty. Also if the users are posting to your page programatically (CURL) then they're not obliged to set the http_referer as well.


1 Answers

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
        });
    }
});
like image 82
Kapil Barad Avatar answered Oct 19 '22 19:10

Kapil Barad