简体   繁体   中英

Content script loading in chrome extension

I have problem with developing chrome-extensions.

I have 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 to address bar, my content script is loaded on the site which is first in autocomplete list. As you can see in the screenshot below, the content script is loaded before I hit enter in address bar and is loaded on the site where I am not yet.

I have no idea what is happening. Please, please help me.

screenshot

Solution is injected script from background page.

Example: get referrer 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;
    }
});

我猜你已经在Chrome中启用了“即时浏览”选项,我前段时间遇到了同样的问题,正因为这个原因我禁用了它并且再也没用过了。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM