How can chrome app reload itself? (document.location.reload is not allowed) - javascript

I am developing chrome app. I need to refresh it often but it is not possible by calling:
window.location.reload()
or
document.location.reload()
In console there is error message:
Can't open same-window link to
"chrome-extension://dmjpjfnceeilhihfbkclnbnhjpcgcdpn/window.html"; try
target="_blank".

Instead use this:
chrome.runtime.reload();
If you have developer tools opened, they will be refreshed as well. Also it seems that if you have only opened app window and developer tools (and you closed browser itself), this will close app but will not load it back again, so you have to keep browser open (e.g. minimized).
During development you can add callback for F5 key to refresh the app:
window.addEventListener('keydown', function () {
if (event.keyIdentifier === 'F5') {
chrome.runtime.reload();
}
});

Related

React: window.close() working in Chrome but not Firefox

I have a single-page react app hosted on github pages, which comes with many limitations. In order to get my "Login with Twitter" flow working, I found it was necessary to open the dynamic twitter login url in a new tab, give the Twitter API an endpoint on my backend as the callback URL, and then have my backend redirect the user to the root url of my frontend client with the generated JWT token in the URL as a query (/?token=abcd1234). I then have some code on the landing page of my client that looks like this:
useEffect(() => {
// at /?close, just close the window
if (props.location.search === '?close') {
window.close()
closeWindow()
setShowCloseMessage(true)
// at /?token, save the token to localstorage, then close the window
} else if (tokenUrlRE.test(props.location.search)) {
const token = props.location.search.match(tokenRE)[0]
localStorage.setItem('token', token)
window.close()
closeWindow()
setShowCloseMessage(true)
}
}, [])
This works fine in Chrome, but not Firefox - perhaps Firefox is more stringent about requiring that Windows only be closed by the script that opened them, and Chrome allows any script on the same domain that opened a window to close it?
Anyway, several other answers here on Stackexchange recommended the following code, which I added inside the closeWindow() function appearing in the above code:
function closeWindow() {
console.log('clicked close')
window.open('', '_parent', '')
window.close()
}
But this also did not work in Firefox.
Furthermore, this additional solution I found caused React to crash:
open(location, '_self').close()
Is there any way to accomplish what I want with Firefox? For the time being I have simply used that showClose state variable seen in the first code block to display a "Sign-in successful, please return to the previous tab" message, but this is less than ideal.

How to do browser automation in javascript natively?

I am working on a javascript project where I want to perform different operations in browser like:
startBrowser() -> Start the browser(User Input) and opens given URL(User Input)
stopBrowser()-> Kill the specified opened browser.
getLatestURL()-> Fetched last visited URL (Doesn't matter browser is running or it's closed)
deleteAllHistory()-> Delete all the data -> Browser History, cookies, cache, saved passwords, bookmarks etc.
How do I do it without using any 3rd party package?
I have written some code:
function openBrowser(url)
{
//open chrome browser
require('child_process').exec('open ' + url);
}
function killBrowser()
{
//close chrome browser
require('child_process').exec('killall chrome');
}
openBrowser("www.google.com")
killBrowser()
The above code does work like,it opens and close the browser instantly but when I call killBrowser method on already opened browser,it doesnt work.
Can someone help?

Detect if another Chrome app is running

I've got a Chrome extension and a Chrome app, which can communicate with each other over messaging. There are certain actions I want the extension to take only when the Chrome app isn't running. Given the Chrome app's Id, how can I detect from the Chrome extension if it is running?
I've tried using closed and suspend lifecycle events from the chrome app to assist, but that route isn't seeming possible due to various reasons. Is there any other way to detect?
Well, you are already using Messaging, so presumably App's event page will wake up to answer if you call it, even if the app is not launched.
You define "running" as having windows open. From the event page, you can check that:
chrome.runtime.onMessageExternal.addListener(function(message, sender, sendResponse) {
/* ... */
if (chrome.app.window.getAll().length) {
// Launched
} else {
// Not launched
}
});
Something like:
chrome.management.get(appId,function(theapp){
if(theapp.enabled){
// it is running
} else {
// it is not running
}
});

Launch chrome application on startup

I would like to open a packaged chrome application automatically when the browser starts.
I have tried: chrome-extension://app id/
But it doesn't work. I get chrome-extension://invalid/ error page.
Use the chrome.runtime.onStartup event, "Fired when a profile that has this extension installed first starts up".
chrome.runtime.onStartup.addListener(function() {
chrome.app.window.create("main.html")
})
https://developer.chrome.com/extensions/runtime#event-onStartup

onInstalled chrome runtime debugging?

chrome.runtime.onInstalled.addListener(function(){
chrome.tabs.update(null, {url: ''});
alert();
});
Above code doesn't work when I install my extension in developer mode. I want to update the active tab to be a blank tab once my extension installed in chrome web store.
That's an invalid URL, that's why it does not work.
You can't just "erase" the URL without navigating to what you supply as the new URL.
If you are trying to navigate to the New Tab page, it has a URL (that's normally hidden) chrome://newtab. If you want a blank page in terms of content, it's about:blank.

Categories