Questions tagged [content-script]

Content scripts are JavaScript files, used by browser extensions or add-ons. They are the part of an extension that can interact with the document context of the current web page. This mainly applies to Firefox or Chrome extensions.

Content scripts are JavaScript files, used by browser extensions or add-ons. They are the part of an extension that can interact with the document context of the current web page. This mainly applies to Firefox or Chrome extensions.

Content scripts interact with a webpage via the DOM API, and they frequently have enhanced privileges and access to privileged browser-API functions compared to ordinary webpage javascript.

For this reason, content scripts are isolated from a target-page's javascript by scoping, and/or sandboxing, and/or anonymous function wrapping. Content-script javascript cannot (normally) directly interact with a page's javascript objects, and vice versa.

Content scripts are very similar to userscripts and Greasemonkey scripts, but as part of full-fledged extensions (add-ons), content scripts can have both more powerful options and more involved coding requirements.

References:


Related tags:

719 questions
8
votes
1 answer

Is that possible calling content script method by context menu item in Chrome extension?

I'm trying to use context menu item to invoke my method written in content script. Is that possible? As i have tried, context menu could only doing stuff in backend. E.g. // A generic onclick callback function. function genericOnClick(info, tab)…
7
votes
1 answer

"Extension context invalidated" error when calling chrome.runtime.sendMessage()

I have a content script in a Chrome Extension that's passing messages. Every so often, when the content script calls chrome.runtime.sendMessage({ message: 'hello', }); it throws an error: Uncaught Error: Extension context invalidated. What does…
Atav32
  • 1,788
  • 3
  • 23
  • 33
7
votes
2 answers

Chrome Extension: Run Content Script before any Embedded Scripts run on Page

I'm trying to change some behavior of the YouTube player, by changing some variables inside of the player_api script that is embedded into the html watch page of videos. Problem is, whatever i try, the embedded script of the player always runs…
7
votes
3 answers

access USB devices from Chrome Extension OR Communicate with Chrome App from Web Page

I know that we can access chrome.hid.getDevices() in chrome app. but I have also heard that chrome.hid.getDevices() can only be used in chrome app. But can we somehow access chrome.hid.getDevices() in chrome extensions. Because I need to inject…
Waqar Ahmed
  • 1,414
  • 2
  • 15
  • 35
7
votes
1 answer

Dynamic loading of content script (chrome extension)

I have an chrome extension, with 2 content script injected by manifest and one background script. { "manifest_version": 2, "name": "Test", "permissions": [ "tabs", "", "activeTab", "storage" ], …
dakov
  • 1,039
  • 2
  • 12
  • 33
7
votes
5 answers

Trying to communicate from default_script to content_script in chrome extension (JavaScript) not working

Alright, so I'm changing the color scheme of a site via an extension, it's my first time using content_scripts so yes, I am a complete newbie, feel free to treat me as one. The problem is tabs.connect it isn't working, I need the tab id or…
7
votes
0 answers

Chrome Extension - Detect Status Code of Page

I have a content script which manipulates a webpage. If the webpage doesn't load (maybe the server gives you an error 500, or chrome can't connect) I want it to retry and refresh the page after a minute or two. Content scripts aren't injected into…
7
votes
1 answer

Undefined response from content script in chrome extension

I can't get a response from my content script to show up in my popup.html. When this code runs and the find button is clicked, "Hello from response!" prints, but the variable response is printed as undefined. The ultimate goal is to get the current…
7
votes
1 answer

Create a tab and inject content-script in it gives permission error

In the background page of my Chrome extension I need to do the following: create a tab with an html page that resides in my extension folder or that is dynamically generated by the background page; once the new tab is loaded, inject a…
guari
  • 3,715
  • 3
  • 28
  • 25
6
votes
0 answers

monkey-patching react's render method in devtools extension

I'm trying to create a monkey patch to React's render method from a devtool extension because I'm trying to recreate something similar to react_devtool's api for a feature in my extension. I'm spoofing the inspected window's virtual DOM with these…
6
votes
1 answer

Why are my ajax calls synchronous in Chrome extension?

I've been trying to use some ajax in a chrome extension. I have a content script which is supposed to add some html to an existing page. I've tried JQuery.get, JQuery.load and ng-include. And all of them show a warning in the console,…
Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
6
votes
2 answers

Can I stop a click from triggering website's usual actions?

As an experiment with React.js, I am trying to build a chrome extension that uses a content script to inject a input field (and now a button too) into certain websites. In this case I am trying to inject it into Twitter. It looks like this: please…
6
votes
3 answers

How to access page variables from Chrome extension background script

With a content script you can inject a script tag into the DOM in order to access variables in the original page (as explained in this question). I want to avoid injecting my code into every page and instead only do that when the user clicks on the…
Matt Zeunert
  • 16,075
  • 6
  • 52
  • 78
6
votes
1 answer

How to control the (i)frame depth, that content scripts are injected, in a Chrome extension?

When creating the manifest file for a Chrome extension there is an option all_frames that allows the content scripts to be injected in the top frame/iframe, or in all. I want this behavior to stop at some specific level (e.g.,2 ). Is there a way to…
5
votes
0 answers

Load remote JS script in Content script for Chrome extension

I have a chrome extension. I have a lot of code in the content script. Nowadays Google takes a lot of time to publish the extension. About 10-15 days due to covid etc. I have the same extension on Firefox too where I load all remote code through…
AMBasra
  • 969
  • 8
  • 24