How to fire an event when a chrome tab is opened from command line? - javascript

i am opening a chrome tab from a command line, for example:
open -n -a 'Google Chrome' --args --profile-directory='Profile 35' 'http://localhost:3001/'
Its works fine. The tabs opened as expected. But i am creating a chrome extension that i need to execute an action to reload the page in case the chrome tab gets infinity loading.
Imagine you just executed that command line and the chrome tab was loading forever... i want to catch this moment. in my background.js i am listening to the onBeforeNavigate() event to start a countdown for a page reload. It works fine if i manually refresh the page, but doesnt get immediately executed if the page is opened from command line.
chrome.webNavigation.onBeforeNavigate.addListener((details) => {
console.log(details)
if (
details.frameType === 'outermost_frame' &&
details.documentLifecycle == 'active' &&
!details.url.includes('chrome://')
) {
//execute some action
//works fine if page is first updated manually
}
})
You can ask why i am using onBeforeNavigate(), because i want to fire the countdown exactly after the user access a new url. It was the solution i could handle
I tried using onUpdated as well, but it didnt work. I dont know why events are not dispatched if the tab is not opened manually... Anyone could help me with this? :)
Thanks in advance

Related

Why is window.close() suddenly not working?

My application opens new PHP from an icon or link in a new browser tab. For several months now, I've been using a simple JavaScript function to close the current browser tab.
Suddenly, over the past few days, the Close link only works when the Window is first opened. The moment you do something on the page (click a link, press a submit button), it doesn't work. I'm at a loss as to what the problem can be. Nothing has changed in the code.
The HTML href statement is:
<span>Close</span>
And the JavaScript function is:
function windowClose() {
window.open('','_parent','');
window.close();
}
This is happening with both Edge and Chrome on a Windows 10 platform.
Could it be caused by some update to the O/S, Chrome or Edge application?
According to MDN, "scripts can not close windows, they had not opened"
If the tab has been opened by you (you typed the url and hit enter manually), there's no way you can close the window with Javascript. However, if for instance, you started your project with npm start (React in my case), the page can be closed with the code you've provided. Though when you try to re-do it by manually opening the webpage and closing the tab - you will fail.
All in all, don't try to close the window that was not opened by js.
Reference: https://developer.mozilla.org/en-US/docs/Web/API/Window/close#closing_the_current_window

How can I stop Chrome blocking me setting a window.location after the user has changed the URL?

I open a new window to point at a page in a manual with var manual_win = window.open("", "manual");
I then set the location (after a bit of async code that tests the existence of the page) with manual_win.location = url;
This works and opens the manual page in a new named tab. I can later run the same code with a different URL and it works fine.
If the user edits the URL manually on the named tab to be some other site such as google.co.uk then the code no longer works on Chrome 76. It just brings the tab to the foreground and doesn't change the location. There is no issue with Firefox. I presume this is due to some sort of popup blocking logic in Chrome.
Is there any way to get around this?
It seems like a bug in Chrome that it blocks me from changing the location yet still brings it to the foreground. It would have made more sense for it to have cleared the name from the tab when it took control way from me so I end up opening a new tab instead.

use javascript to terminate firefox browser

i programmatically start a browser, running firefox, from a windows application. i want to add a button and connect the button, using javascript, to an onClick event to terminate the browser. I did try window.Close(), but that does not work. the order of events is as follows: app -> start firefox -> runs my own js script -> on the script i have an exit button -> depress exit button -> firefox terminates and control back to the app.
$(element).click(function(){ window.close(); });
Note: you cannot close any window that you didn't open with window.open . Directly invoking window.close() will ask user with a dialogue box.

Google Chrome closes when I open a new tab using my browser action Javascript, no idea why

I'm in the process of writing a Google Chrome extension, and I'm hung up on what should be a very simple task. I have a browser action that opens a popup, "popup.html", which then loads a small Javascript file, "popup.js". As soon as I get the DOMContentLoaded signal from the popup page, my Javascript opens a new tab with a certain URL. This behavioral quirk is by design - in some cases it will display a menu, and in other cases it will just open this URL.
I currently have some very simple code which is supposed to do this, but whenever it opens this tab, Chrome suddenly closes. I'm using a Mac (OS X Mavericks), so it doesn't crash entirely (still running), the window just closes. When I open it up again, the tab it was supposed to open is there, but the "popup.html" menu is awkwardly hanging over the UI still and won't go away.
Here is my complete popup.js file:
function stuff() {
window.open("http://stackoverflow.com/");
}
document.addEventListener('DOMContentLoaded', stuff);
I'm guessing that I may need to gracefully close my popup window before going to this link or something, but that seems like I'm overthinking it. After all, I've seen other extensions that have normal links in their browser action popups, and you can click those to open new tabs without any Javascript magic.
I've also tried using the chrome.tabs.create function, yet the same thing happens. The Chrome developer console doesn't show any errors. Thoughts?
Edit1: Just disabled all extensions and tried again. Still breaking. Going to boot up my Windows 8 box and see what happens on it.
Edit2: Works with a short delay before opening the window now on Mac, see my answer below.
Ok, I think I may have figured this out, but it's still weird. I did this on my Windows 8 PC, and the browser didn't crash, but the browser action popup didn't close the way it's supposed to either.
On a hunch, I altered my code on the Mac to give the browser some time to render the popup window:
function stuff() {
window.open("http://stackoverflow.com/");
}
setTimeout(stuff, 500);
Worked without a problem on the Mac this time. I'm guessing it's an issue with the browser being interrupted while rendering the popup window, but it's still weird. I would still appreciate an explanation from someone who actually knows what's going on here! :-)

Activate Firefox tab in Greasemonkey/Javascript? Is this possible?

I developed a greasemonkey script that refreshes a page and checks for certain updates. I would like to run this script in a tab and browse the internet in another tab, but then have the script automatically activate it's tab when an update is found.
Im not sure how clear that was, maybe this is better:
Tab 1 is running a greasemonkey script, refreshing every x seconds looking for the word "foo"
Tab 2 is browsing stackoverflow
-- Now on a refresh, the GM script finds the word "foo". This is when I want the tab focus to automatically shift from Tab 2 to Tab 1.
Is this possible, and if so, how do I achieve this?
Thanks.
I'm pretty sure that firefox gives focus to tabs that call alert(). So just pop up an
alert('found foo')
You can achieve this with Scriptish's GM_openInTab implementation, like so:
GM_openInTab(window.location.href, true);
That should cause the page that a GM script is on to be focused.
Have you looked at GM_openInTab()?

Categories