Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript How to write chrome extensions background.js to inject file with each new http request?

Status
Not open for further replies.

bernd7

Coder
Hi, hope someone can help me.
I wanna build some chrome extension that injects a certain javascript file into a website each time an http request is done.
(since it for some unknown reason doesnt work with my intended website if the script jsut gets injected once at the start. it stops working at a specific point)
currently my manifest.json file looks like this:

JavaScript:
{
  "name": "Test3",
  "description": "blabla",
  "version": "1.0",
  "manifest_version": 3,
  "background": {"service_worker": "bg.js"},
  "permissions": ["scripting","webRequest"],
  "host_permissions": ["<all_urls>"]
}


Then we got the bg.js that looks like this:

JavaScript:
chrome.runtime.onInstalled.addListener(async () => {
  const old = await chrome.scripting.getRegisteredContentScripts();
  if (old[0]) await chrome.scripting.unregisterContentScripts({ids: old.map(s => s.id)});
  await chrome.scripting.registerContentScripts([{
    id: 'write',
    js: ['write.js'],
    matches: ['<all_urls>'],
    runAt: 'document_end',
    world: 'MAIN',
  }]);
});

and we finally have the write.js which's content hopefully dont matter here:

Important is that currently the bg.js injects the write.js code just once into the site.
and I wanna change stuff so write.js code gets injected into the site each time a request is made.
(Cause I dont know, the site does tons of loading new stuff and such and suddenly the injected code just doesnt work anymore, so probably gets deleted somewhere along the way)

so I think I would have to have bg.js be something like

JavaScript:
chrome.webRequest.onBeforeRequest.addListener(
chrome.scripting.executeScript(
    {
      target: {tabId: window.tabs[0].id},
      files: ['write.js'],
    });
);

or similar but I dont know how to properly write this, like what's the right syntax. or if it is even right in general.
basically whenever an onBeforeRequest event happens (which should logically be each time a request is about to be made) I wanna inject the write.js into the website.
how do I do this properly?
I already looked into the documentations but honestly I dont get them. They dont exactly explain stuff too well or show obviously where Arrays, Strings, etc. are used^but some abstract concepts.
for the most part, I am already confused when with chrome extensions you use {} [] and such and in what order.
 
Hi, hope someone can help me.
I wanna build some chrome extension that injects a certain javascript file into a website each time an http request is done.
(since it for some unknown reason doesnt work with my intended website if the script jsut gets injected once at the start. it stops working at a specific point)
currently my manifest.json file looks like this:

JavaScript:
{
  "name": "Test3",
  "description": "blabla",
  "version": "1.0",
  "manifest_version": 3,
  "background": {"service_worker": "bg.js"},
  "permissions": ["scripting","webRequest"],
  "host_permissions": ["<all_urls>"]
}


Then we got the bg.js that looks like this:

JavaScript:
chrome.runtime.onInstalled.addListener(async () => {
  const old = await chrome.scripting.getRegisteredContentScripts();
  if (old[0]) await chrome.scripting.unregisterContentScripts({ids: old.map(s => s.id)});
  await chrome.scripting.registerContentScripts([{
    id: 'write',
    js: ['write.js'],
    matches: ['<all_urls>'],
    runAt: 'document_end',
    world: 'MAIN',
  }]);
});

and we finally have the write.js which's content hopefully dont matter here:

Important is that currently the bg.js injects the write.js code just once into the site.
and I wanna change stuff so write.js code gets injected into the site each time a request is made.
(Cause I dont know, the site does tons of loading new stuff and such and suddenly the injected code just doesnt work anymore, so probably gets deleted somewhere along the way)

so I think I would have to have bg.js be something like

JavaScript:
chrome.webRequest.onBeforeRequest.addListener(
chrome.scripting.executeScript(
    {
      target: {tabId: window.tabs[0].id},
      files: ['write.js'],
    });
);

or similar but I dont know how to properly write this, like what's the right syntax. or if it is even right in general.
basically whenever an onBeforeRequest event happens (which should logically be each time a request is about to be made) I wanna inject the write.js into the website.
how do I do this properly?
I already looked into the documentations but honestly I dont get them. They dont exactly explain stuff too well or show obviously where Arrays, Strings, etc. are used^but some abstract concepts.
for the most part, I am already confused when with chrome extensions you use {} [] and such and in what order.
Hi there, is there a particular reason why you would want to inject a file on http request?
 
Status
Not open for further replies.

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom