0

I made a function that collects forecast information from a certain website. When I am on the website and call the function from the console of Chrome Browser it works perfectly. However, I want to automate the process and call the function whenever this certain webpage is loaded. I thought of building a Chrome extension but I am new to this. So is there any possible way to attach a JS function to the browser? I don't own the webpage so I can only access its information from the DevTools of Chrome. Sorry if it's a duplicate topic but I couldn't find one that answers my question.

Thanks in advance.

  • 1
    Why don't you just put the function in a script tag on that page and call it? – user2085143 Nov 22 '16 at 17:02
  • Because it's a public page (i don't own it). – Daniel Georgiev Nov 22 '16 at 17:03
  • You could try to use an iFrame for this? ( this would allow you to call the public page and your code ) Some sites will not work in iFrame. – Neo Nov 22 '16 at 17:09
  • you could try to use a bookmarklet - https://code.tutsplus.com/tutorials/create-bookmarklets-the-right-way--net-18154 and http://benalman.com/projects/run-jquery-code-bookmarklet/ – Craicerjack Nov 22 '16 at 17:10
  • 1
    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 22 '16 at 17:41
  • See: [Content Scripts](https://developer.chrome.com/extensions/content_scripts). In addition to making a full extension to do this (a relatively easy thing to do), there are many alternate ways to have a user script for a page. There are a variety of extensions which exist whose purpose is to allow you to run small user scripts on specific pages without the need to make a complete extension for each one. – Makyen Nov 22 '16 at 17:46

1 Answers1

-1

Since you are making a chrome extension, you can use background.js to inject a script into the webpage

chrome.tabs.executeScript(tab.id, {
    file: 'inject_script.js'
});

And inside your script, you can write the javascript code (onload event) to execute this script on page load.

Also for this you'll have to add permission tabs in your manifest and the following code in your manifest.json :

"background": {
    "scripts": ["background.js"],
  }

See background_pages

mrid
  • 5,782
  • 5
  • 28
  • 71