Content script loading in chrome extension

I have a problem developing chrome extensions.

I have a content script:

window.addEventListener("load",function(){
   var html = document.getElementsByTagName('html')[0];
   var title = document.getElementsByTagName('title')[0].innerHTML;
   if(html){
      chrome.extension.sendRequest({akce: 'content', title: title},function(response){});
      alert(title);
   }
},false);

then I have a BG page:

chrome.extension.onRequest.addListener(function(request,sender,sendResponse){
    if(request.akce == 'content'){
        console.log(request.title);
    }
});

The problem is that when I start typing on the address bar, my script content is uploaded to the site, which is the first in the autocomplete list. As you can see in the screenshot below, the contents of the script are loaded before I press Enter in the address bar and load on the site where I have not been.

I have no idea what is going on. Please help me.

screenshot

+5
source share
2 answers

The solution is entered by the script from the background page.

Example: get link from bg page

chrome.tabs.executeScript(tabId,{
    code: "chrome.extension.sendRequest({action: 'content_refer', url: document.referrer},function(response){});"
});

chrome.extension.onRequest.addListener(function(request,sender,sendResponse){
    if(request.action == 'content_refer'){
        wipstats.allPages[sender.tab.id].ref = request.url;
    }
});
+4
source

, " " Chrome, , .

0

All Articles