This question already has an answer here:
Difference between background script and content script in chrome extension
(1 answer)
Closed 8 years ago.
I want to create an extension that reads all the colors of a site and change all the colors accordingly, if you click on the button.
Where should I write this code?
In a content script or in a background script?
What is actually the difference between the two?
Read the well-written Overview at the documentation. This should answer a lot of your questions.
In short, content scripts execute in an isolated context of a webpage, having access to its DOM, but have very limited Chrome API access.
A background script is usually used for central handling of tasks, while content scripts act as intermediaries between it and pages you want to interact with.
As for your situation:
You need to have a background script to listen to the button click event.
You need to have a content script to interact with a page.
So, you need both, and the background script can message the content script to do its magic.
Related
This question already has an answer here:
What is the difference between a background page/script and a popup page?
(1 answer)
Closed 17 hours ago.
Basically we can do everything via background script that can be done using pop up script so why do we need to have multiple scripts?
I am just exploring around extensions
Background scripts run all the time, while popup scripts are started when the popup opens and stopped when it closes. So whatever you do in a popup script needs user interaction.
A pop-up script and a background script are two types of scripts used in web development for different purposes.
A pop-up script, also known as a modal script, is a JavaScript code that displays a pop-up window on top of the current web page. This type of script is often used to show alerts, confirmations, or to collect user input. When a pop-up script is executed, it interrupts the user's current interaction with the web page and requires them to interact with the pop-up before they can continue. Pop-up scripts are typically triggered by user actions such as clicking a button, link or an image.
On the other hand, a background script is a script that runs in the background of a web page without any visible user interface. It is typically used to perform tasks that do not require user interaction or to provide additional functionality to a web page. A background script can run continuously or be triggered by specific events such as page load or user input. Some examples of tasks that can be performed by background scripts include data processing, network requests, and DOM manipulation.
The main difference between a pop-up script and a background script is that a pop-up script displays a pop-up window that requires user interaction while a background script runs in the background without any visible user interface.
Basically we can do everything via background script that can be done
using pop up script
Firstly, there are no longer background pages in manifest V3. You have a service worker instead.
Secondly, there are things such as playing audio which cannot be done from a service worker. This is why chrome.offscreen has been introduced.
This question already has answers here:
Cannot modify content of iframe, what is wrong?
(3 answers)
Closed 2 years ago.
I would like to add an iframe to my webpage. The problem is that there are several malicious scripts and ads in the iframe. Is there a way to block certain scripts (like Adblock does) using HTML or JavaScript?
tl;dr: No
The sandbox attribute on the iframe element lets you apply some limits to what scripts can do (e.g. you can block popups) but it can't be used to block specific scripts.
All the techniques for blocking specific scripts have to be applied by the owned of the actual page the script is embedded in. If you trust the owner of the page you are putting in a frame, then they can do what they like (except interact with your page thanks to the same origin policy).
There is a proposal to allow a CSP to be applied to a frame from the parent document, but it doesn't seem to have been updated in half a decade so I'm assuming the idea has died on the vine.
This question already has answers here:
Warn user before leaving web page with unsaved changes
(17 answers)
Closed 3 years ago.
In my webapplication, I have a certain webpage that has an iframe inside of it with a certain source (can't tell which one, but I think (?) not really important for this question), but as soon as the webpage on my webapplication that contains the iframe in it is loaded, it throws a pop up which asks if I want to leave the page. I don't want this, I want to stay on the page with the iframe element inside of it.
The weird thing is, that when I for instance use http://example.org as the source (for testing), it shows the webpage normally without any pop up (and so did other pages I tested that contained HTML, CSS and most importantly JS).
What is it that causes this pop up? The pop up does not appear when I manually open the webpage that is included as the source of the iframe, in a browser.
You get that message if the page you're trying to leave has added a listener for the beforeunload event.
The "certain webpage" may have code that's redirecting itself. Maybe it checks whether it's in an iframe and does this, so it only happens when you try to load that page in your iframe, not when you load it normally. It's hard to be more specific without knowing what the webpage is and how it's written.
There are serveral rules of iframe any page or website in your webpage. If it is not Same-origin it will allow you to iframe that website, if it is on different origin it will not allow you to iframe that webpage. For more information refer this link https://code.google.com/archive/p/browsersec/wikis/Part2.wiki#Origin_inheritance_rules
This question already has an answer here:
Chrome extension - Get html from a separate page of a website in the background
(1 answer)
Closed 6 years ago.
I'm using a site that has a contact block in a page. The person's name in the contact block is linked to a second page that shows their administrative rights via a column of check boxes.
I'm attempting to add a feature to an existing chrome extension I created that will open that link in the background (hidden) when the page is loaded, read which boxes are checked, and then add small images/suffixes to the person's name in the contact block so I can at a glance see what administrative rights they possess.
I'm slowly teaching myself how to send messages between the content script and background.html/background.js but I don't know how to begin loading and manipulating the site in the background pages.
Any help or direction of resources would be greatly appreciated. I tried to read the documentation on google's developer's site but it just isn't clicking for me.
I think you can get the html through ajax request.
for example(suppose you have included jquery)
$.get("http://stackoverflow.com/questions/40963011/how-do-i-load-url-in-background-page-and-parse-information-from-it-with-js", function(data){console.log(data)})
the data is just html string.
But if the page can't been accessed from public, you need get it from content script and pass the data to background.
Hope this is helpful.
This question already has an answer here:
how to view video in a blocked web site [closed]
(1 answer)
Closed 9 years ago.
I have a video on youtube but it cannot be viewed in some countries because access to youtube has been blocked in those countries. So, what would be your suggestion? i know i can put another link in html and ask the clients to click that link but i would like to know
Is there any way instead of putting 2 links ,something like putting 1 link that points to youtube and if the user cannot see the video there ( because access is blocked) , the user will automatically direct to another website or
just put the video on the server and ask the users to download it?
if none of proposed solutions is great , what is your idea?
is there any Javascript code for this
Also, If I host it on my website, i will increase the size of my page.So, does it have any effect on the estimated time to view the page by users?
How can I do it in html4? i know we can emded video file in html 5 but what should I do for earlier versions?
Thank you all,
Do the following:
Examine the YouTube website and find some JavaScript file there, and examine the JavaScript code to find some global variable it sets or changes. Hopefully you can find a JavaScript file that has no side-effects on your web page.
Add it as a <script ...> in your page.
Write some JavaScript of your own that checks this global variable, which will tell you whether or not the YouTube JavaScript was loaded, which will tell you whether or not YouTube is blocked
Alternatively, you can do something similar with a CSS file from YouTube.