8

I'm trying to build a Chrome Extension that needs to send a POST request to a server when the user is on a certain url/path.

I have that part working, except the code I have right now only runs when I click on the icon of my Chrome Extension, how would I make the code run every time I'm on a certain url?

I also need data stored in chrome.storage to be accessible in the code that runs every time aswell

Tyler Chong
  • 650
  • 2
  • 12
  • 24
  • Is your code in a content script? Content scripts run in the background of web pages. – Loaf Jan 05 '17 at 02:02
  • chrome.storage may only be accessible via the background page. you will need to sendMessage to the background page, then sendMessage back. I remember having an issue which was resolved by including the third param (true) – neaumusic Jan 05 '17 at 02:09
  • Possible duplicate of [Chrome Extension run for a specific page](https://stackoverflow.com/questions/10396634/chrome-extension-run-for-a-specific-page) – Tyler Chong Jul 04 '19 at 01:28

1 Answers1

6

The answer is to use a content script

Specify the matching url in your manifest.

A background page is always running, but the content script is only injected on specific pages (you can specify before/after load end)

neaumusic
  • 10,027
  • 9
  • 55
  • 83
  • When I use it as a Content Script, it doesn't have access to the chrome.storage object, do you have any ideas on how I would do this? – Tyler Chong Jan 05 '17 at 02:13
  • anything secure is done on the background page. the background page can access chrome.storage if you requested that permission in the manifest. your content script has to postMessage or sendMessage to the background page, the background page accesses chrome.storage, then the background page sends insecure data back to the content script – neaumusic Jan 05 '17 at 02:16
  • similar to how you would write javascript that interfaces with your secure server environment – neaumusic Jan 05 '17 at 02:16
  • Thanks! To clarify, I need to make two files, and pass the data I need from the background to the content script by using sendMessage and having the content script code run onMessage received, correct? – Tyler Chong Jan 05 '17 at 02:26
  • That sounds right, I've only done it content script -> background -> content script, but I'm sure there is a way to just do background -> content script with onMessage listener – neaumusic Jan 05 '17 at 02:30
  • 1
    I finished a while ago, but just to give an update what he said above worked perfectly! – Tyler Chong Jan 24 '17 at 00:43
  • @neaumusic In content, we can only specify the domain right? What if I want the code in a .js content script file to be injected only at a specific URL including the full path? e. g. ```xxxx://domain.com/x/y/z/file.html``` ? – Verminous Nov 17 '22 at 20:20
  • 1
    @Verminous well, I'm not an expert but you can specify `matches` in the [manifest.json](https://developer.chrome.com/docs/extensions/mv3/match_patterns/) or just use and check the window.location in javascript. My chrome extension as an [example](https://github.com/neaumusic/selection-highlighter/blob/master/src/chrome_extension/manifest.json) – neaumusic Nov 18 '22 at 21:07
  • @neaumusic `````` means more like all domains right? Because what I need is for the content script to be executed at a specific full path: e.g. ```xxxx://domain.com/x/y/z/file.html``` otherwise I get a bunch of console errors because the ```addEventListener``` is trying to listen for event in all pages. Maybe I will ask a question about this. – Verminous Nov 18 '22 at 22:29
  • @Verminous yea if `matches` doesn't work to only add the content script on certain pages, you can add the content script to all pages but selectively run your code like "addEventListener" if the content script verifies window.location matches – neaumusic Nov 18 '22 at 23:43
  • You mean something lke this: ```if (window.location.toString().includes("xxxx://domain.com/x/y/z/file.html")) {``` ? I tried this but still getting errors from ```addEventListener ``` trying to listen to all pages from the domain ```domain.com``` in mentioned ```matches``` on the manifest. – Verminous Nov 18 '22 at 23:49