启用Chrome扩展程序而不点击

时间:2014-03-09 16:12:24

标签: javascript function google-chrome google-chrome-extension onclick

如何在不点击的情况下启用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)
});

1 个答案:

答案 0 :(得分:-1)

包含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(){内加载页面时包装您想要执行的代码,例如

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

$(document).ready()检查页面加载的时间是否足以具有功能。