Skip to content
Advertisement

Enable chrome extension without clicking

How to enable chrome extension without clicking it.

I need to perform a certain function from my extension every time i reload a page(no clicking) is there a way to do it.

My code which contains the on click method

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; 

and

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

Advertisement

Answer

To include jQuery:

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

Using only pure JavaScript you can do this with:

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

only the code within that function will be immediately executed.

In jQuery you can wrap the code you want executed upon loading of page inside of $(document).ready(function(){, e.g.

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

$(document).ready() checks for when a page is loaded enough to have functionality.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement