What is the difference between a pop up script and a background script? [duplicate] - javascript

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.

Related

Display a web page in a container, scroll, switch to next

I have a list of urls I need to show on a screen for a presentation. After the page have loaded, I want to scroll to the end of the page, and when it's reached load the next one.
The problem is, most of those pages have 'X-Frame-Options' to 'sameorigin', so I can't use iframes. What other options do I have ?
I thought about, maybe, a chrome extension will complete rights over navigation that would handle the whole process...
Thanks ahead.
So, the solution was indeed to build a Chrome extension. Only the software containing the page gives you that much control over it when you don't have access to its code - namely, the browser.
I built a very simple extension using chrome.tabs in the background to open a new tab or update it, injecting a script in the page whenever it's loaded, and using messaging to inform the background when scrolling is finished and it's time to load a new page in the list.

How to prevent browser_action from reloading its popup window every time?

I'm developing a webextension and I'm stuck with following problem.
I have browser_action that displays popup window with some content. The problem is, it takes 3-4 seconds to load, and user has to wait every time he opens this browser action.
I want somehow force browser_action to load DOM of popup window just once, so when I open this action again it shows me already rendered window.
I can't preload any assets through background script, because the asset is <iframe> of remote website and you can't easily store rendered DOM in localStorage just to display it later on user request.
I thought about putting <iframe> inside background script and displaying it somehow in browser_action popup window, but I failed to find out how to do that.
Background page is loaded once and stays forever while extension is enabled, so maybe I could display it contents inside browser_action popup window somehow?
Unfortunately, I don't think that's not possible. Your best bet would be to show some useful UI while the iframe loads.
It's certainly not possible to make the popup page itself be persistent.
And I'm 98% positive you can't swap in a loaded tab/frame into the page of the popup. Chrome does it internally sometimes (preload then swap, see tabs.onReplaced), but you have little control over this mechanism.
It seems you can trigger it with preload links:
<link rel="preload" href="https://example.com/"
as="document" crossorigin="anonymous">
However, it serves little purpose to embed that into the popup page (it will be fetched when the popup loads, which is milliseconds of difference) and I'm reasonably sure that preloading it from, say, the background page won't carry over to the popup, despite them normally sharing the browser process. You can experiment though!

How do I create a link to a page and force it to immediately execute a javascript

I need to open a specific view of a website. In fact the webpage is only one page and the links on the page execute javascript functions to change the visible content. My question is:
How can I create a link to this webpage that tells the page which javascript function to be executed in order for it to show the desired page view?
If you're targeting modern browsers, you may use postMessage to send messages between windows.
https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
You could be listening for messages on your main window while your popup send them.

Will Firefox start my SDK extension automatically after the browser starts - loading screen

I am new on add-on development using the SDK.
I want to ask you guys if it is possible to start my extension automatically after I open my browser? At the moment I starts after I press my widget icon in the toolbar (the panel shows a table with some data I get from the DOM).
Another thing I want to ask you: is it possible to show a loading screen (like a ajax gif) inside my panel (my extension needs a few seconds after switching a tab, to get the DOM data) every time I press the toolbar button.
First of all: One question per post, please.
Extensions are always started with the browser. When it comes to SDK add-ons, your main.js will be called. It's your job to perform any additional initialization form there.
Panels contain regular HTML pages and therefore can use images.
It's impossible to tell you more, without you providing more details and the code you got so far!

Background Page in popup- chrome extension

I am embedding a dynamic webpage in a popup. Currently its working and every time popup is loaded the webpage is loaded again, thus me losing the work i did on the webpage in popup. Though its fine, but i want that webpage remain loaded in background and i just show it in popup on click. to do this i copied complete code from my pop up page(script+html) to background.html. Now how should i access the page completely in popup and show directly(i want to show html also-from background page)
Thanks
Popups live in the same process (the extension process) as the background page, and one page can get the DOM Window of the other. A popup gets the background page by calling chrome.extension.getBackgroundPage(). So every time you open the popup, just read and write to some variable on the background page, for example chrome.extension.getBackgroundPage().enteredData = "value";.
Alternately, you can use HTML5 localStorage to store variables even after the browser is shut down; e.g. localStorage['enteredData'] = "value".

Categories