XMLHttpRequest stops when my panel loses focus in google chrome - google-chrome-extension

I am sending a xmlhttprequest from my google chrome ui after a button is pressed in the popup.html. This code works fine as long as the popup.html remains open. But the moment the popup.html closes(bu clciking the mouse outside popup.html area) the xmlhttprequest automatically aborts and stops. This causes my program to stop execution. I want to know if there is any way in which i can continue the xmlhttprequest even if the windows gets hidden and loses focus.

Your AJAX request should be done by a background script. Execution of this script can not be stopped easily by the user.
You should modify your extension like this:
user clicks something in the popup
popup calls a function from a background page
(from this place on popup is no longer needed to be open)
background page sends an AJAX call and retrieves the answer
(background page may ignore/store the answer form AJAX call or pass it to some content script/popup script etc.)

Related

Chrome extension: How to do not lose state of my extension when user changes/reloads the page

I'm currently developing an extension that comes in different parts:
- background script (Chrome background.js
- content script (Chrome content-script.js)
- popup script (popup.js and loaded from the iFrame)
- iframe.html (iframe to be loaded when user activate the extension)
It works great except that I lose all the settings/context (used in the popup script to display info in the iframe) when the user change of page or reload the page in the tab.
Technically I want to isolate the extension per tab (each tab can have its own config) but when a user navigate in a same tab the context should be kept.
Question is: what is the best way to achieve that?
I'm thinking about persisting all the context in the content script and reload it when the event chrome.tabs.onUpdated is fired. Is it the right approach?
I recommend you to read about chrome.storage. You can store data there, both background and content script have access to it.
And probably you will need chrome.runtime.sendMessage and chrome.runtime.onMessage to have conversation between background and content. Each time content script runs in a tab it can request background whether this tab have some existing data or not.

After clicking Browser back button, BeforePageLoad-Code not run

My problem is that after clicking the Browser back button the code in the BeforePagesLoad-Event not triggered. There is a function to get set an object in a manged bean.
I dry to set the cache control to no-store but that don't work. the-order-of-events-in-xpages
Is there a way to trigger a event after clicking the browser back button?
I am no x-pages expert, but it looks to be a server side technology, meaning that a server call must be made in order for the code to run.
The first thread is a solution for forcing a page reload via cookies, while the second involves javascript/jquery. The second also may touch on some cache control of different browsers.
Force a page refresh when page visited from back button
Is there a cross-browser onload event when clicking the back button?

What can you do in background scripts that you can't do in other js files?

*By other js files, I mean the files you include in popup.html.
The following code works, so why should I use a background script?
Content script
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse) {
//Some code
}
);
Script included in popup.html
chrome.tabs.query({active:true,windowId: chrome.windows.WINDOW_ID_CURRENT},
function(tab) {
chrome.tabs.sendMessage(tab[0].id, {method: "someMethod"},
function(response){
//Some code
});
});
This
Background pages live as long as the Chrome browser is active (and even longer if the "background" permission is set). Popup pages are only active when the badge's popup is open. The popup can only be opened by the user.
The background page's document is never visible, whilst the popup page becomes visible on click of the badge's button.
Besides that, there's no difference between background pages and popup pages. They run in the same extension's process, and have access to the same set of APIs.
If your extension only needs to be active while the popup is active, you don't need a background page. To save the state of the popup, just use the synchronous localStorage or the asynchronous chrome.storage API. When the variables you use are too complex to be stored using either API, a background page may be useful.
One example where the use of a background page is beneficial:
Imagine an extension that downloads a huge text file from a server. The creation of the resource is very resource-intensive for the server. Technically, everything can be done from within the popup. However, offloading the task to the background page allows the user to do other tasks while the file is downloading (if you use a popup only, the download will stop when the user closes the popup).
Though you didn't ask, I'd like to make you aware of event pages. They are similar to background pages, with one difference: Event pages are automatically closed when the extension is idle. In other words, event pages are only active when needed! By doing this, your extension will profit from the advantages of background pages without needlessly wasting the user's memory.
My last example is also a perfect example of when an event page has to be used. Besides doing the http request on behalf of the popup, the background page does nothing. If you use an event page instead of a background page, you get the best of both worlds: the popup page can be closed without interrupting the download, and the extension will not waste memory.
Documentation
Learn more about Background pages and Event pages
"Popup" in this answer refers to the optional panel of the chrome.browserAction or chrome.pageAction API, set by declaring the "default_popup" key in the manifest file, or programatically using the setPopup method.

click of a submit button on any website using Google extension

On the click of a submit button on any website eg. Amazon, I want a .html to be triggered using google extension. please let me know if it is possible.
You can indeed execute scripts on (almost) any page using content scripts that can manipulate the DOM, allowing you to add an onsubmit event listener to a form (more reliable than onclick on a button as it is fired however the form is submitted - e.g. hitting ENTER).
What exactly do you mean you "want a .html to be triggered"? Triggered in what way? If you want to open another tab when this happens, you can do this using message passing to tell your background page to do this for you using the chrome.tab.create API method.

browser related question

I get different behavior between IE and Mozilla.
Say for ex I have 2 buttons on my webpage. Button-1 and Button-2
In mozilla browser, when I click Button-1 then it process the click request of Button-1, while it is processing the request, immediately if I Click on button-2, browser will not proceed request for Button-2.
In IE browser, after clicking Button-1 If I immediately click Button-2 then it will first process request of Button-1 and then it will process request for Button-2.
Why there is different behavior for both browser for same web page?
PS. IE 8 and Mozilla 3
Thats's life
Browser differences are one of the biggest hurdles in modern web development. You're best bet is to paste the code to let users take a look.

Resources