无需点击即可启用Chrome扩展

如何在不点击的情况下启用Chrome扩展。

我需要从我的扩展中执行某个功能,每当我重新加载一个页面(不点击)有办法做到这一点。

我的代码包含点击方法

chrome.extension.onMessage.addListener(function(request, sender) {
  if (request.action == "getSource") {
    message.innerText = request.source;
  }
});

function onWindowLoad() {

  var message = document.querySelector('#message');

  chrome.tabs.executeScript(null, {
    file: "getPagesSource.js"
  }, function() {
    // If you try and inject into an extensions page or the webstore/NTP you'll get an error
    if (chrome.extension.lastError) {
      message.innerText = 'There was an error injecting script : n' + chrome.extension.lastError.message;
    }
  });

}

window.onload = onWindowLoad; 

chrome.extension.sendMessage({
    action: "getSource",
    source: started(document)
});

要包含jQuery:

<head>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script>
        /*all your normal JavaScript (or include as link)*/
    </script>
</head>

只使用纯JavaScript,你可以这样做:

window.onload = function(){/*your JavaScript code*/};

只有该函数内的代码才会立即执行。

在jQuery中,您可以在加载$(document).ready(function(){ ,eg

$(document).ready(function(){
    /*code goes here*/
});

$(document).ready()检查页面何时加载足够的功能。

链接地址: http://www.djcxy.com/p/91123.html

上一篇: Enable chrome extension without clicking

下一篇: Chrome extensions: unpacked extension caching