0

I'm trying to create an extension to automate some tasks. I'm trying to load a page and then execute a code once the page has been loaded but I couldn't find the right way to do this.

custom.js:

var func=function(){

var actualCode = '(' + function() {
    //load the new page
    window.location='http://page.com/'
} + ')();';
    // inserts the code into the current page tab
    chrome.tabs.executeScript(null, { code:actualCode})

    //--Execute this code once the page is loaded--

}

document.getElementById('btn').addEventListener("click",func);

index.html:

<!DOCTYPE html>
<html>
 <head>  
 </head>
 <body>
  <button id="btn"> BTN </button>
  <script src="custom.js">
  </script>
 </body>
</html>
Geoffrey-ap
  • 380
  • 3
  • 12
  • I guess every time script executed, you're navigated away so your further code will never run. – Mouneer Nov 21 '16 at 01:15
  • Please [edit] the question to be on-topic: include a **complete** [mcve] that duplicates the problem. Including a *manifest.json*, some of the background *and* content scripts. Questions seeking debugging help ("**why isn't this code working?**") must include: ►the desired behavior, ►a specific problem or error *and* ►the shortest code necessary to reproduce it **in the question itself**. Questions without a clear problem statement are not useful to other readers. See: "**How to create a [mcve]**", [What topics can I ask about here?](http://stackoverflow.com/help/on-topic), and [ask]. – Makyen Nov 21 '16 at 04:06
  • I would suggest that you read the [Chrome extension architecture overview](https://developer.chrome.com/extensions/overview#arch). It has overall architecture information which should help your understanding of how things are generally done/organized. – Makyen Nov 21 '16 at 04:07
  • Your current issues would be simplified by changing your `actualCode` to `var actualCode = "window.location='http://page.com/';";` This would at least make it such that your code was doing what you appear to have written it to do (not necessarily what you want it to do, but at least what you have written it to do). However, your question is too broad for this, in and of itself, to be an answer. – Makyen Nov 21 '16 at 04:32
  • If your *index.html* is a browser action popup, why are you using a button within a popup instead of just a [`browserAction`](https://developer.chrome.com/extensions/browserAction) button? – Makyen Nov 21 '16 at 04:36

4 Answers4

1

To track the new page and run any logic on it, one solution could be adding this logic inside a content script file that loads at the document_end and every time you navigate to the any page using window.location, this logic will run. You may add some conditions to make sure you logic run on the wanted pages/elements.

Am I clear enough?

Mouneer
  • 12,827
  • 2
  • 35
  • 45
1

Try using chrome.tabs.onUpdated.addListener to check if the page has been fully loaded where status is equal to 'completed'.

Use the code samples which was given in this SO thread:

chrome.tabs.onUpdated.addListener( function (tabId, changeInfo, tab) {
  if (changeInfo.status == 'complete') {

    // execute some codes here

  }
})
Community
  • 1
  • 1
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
1

You can use Content Scripts.

Example from docs: you need to modify manifest.json to allow extension automatically inject some javascript for websites if urls are matching to the pattern.

{
  "name": "My extension",
  ...
  "content_scripts": [
    {
      "matches": ["http://www.google.com/*"],
      "js": ["myscript.js", "myscript2.js"]
    }
  ],
  ...
}

You can also define run_at property to control when the files in js are injected. It can be "document_start", "document_end", or "document_idle".

Timur Bilalov
  • 3,522
  • 1
  • 16
  • 14
0

I'm not sure I understood well but this should work if I understood well.

document.addEventListener("DOMContentLoaded",yourFunctionGoesHere)
Matija
  • 493
  • 4
  • 9
  • 21
  • I tried this but it's not working, I believe that the reason is that when I load the new page it generates a new DOM and this new DOM is not being tracked. – Geoffrey-ap Nov 21 '16 at 00:45