programmatically switch to tab in Google Chrome, and open URL if tab not found - javascript

I'd need the following functionality for Google Chrome.
I didn't find a command line parameter that could help me here.
check if a URL is open in one of the Google Chrome tabs
if so, activate this tab
if not, open the URL in a new tab
Any ideas how to solve this?

I think Chrome extensions docs - Tabs will help you!
Just to make it clear you can't NOT access anything beyond your Domain in JavaScript because of Same origin policy.

Using Javascript, triggering an alert can have the desired effect. Run this code in your console in one tab and switch to another tab in the same browser.
setTimeout(function(){
alert("Switched tabs");
},
5000);
The alert appearing after the timeout will trigger tab switch. Or you can do something similar! This should however be used if absolutely necessary to move user from current tab, like Google Calendar does.
Update 2019
With new versions of Chrome, this no longer works. As pointed out in the comment below, Chrome now shows a small icon in the tab to show a window/alert has been triggered

Related

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.

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! :-)

disable onclick ads with a content-script in Google Chrome

There are some video streaming sites that pop up an ad anytime you click anywhere on the page. The problem is, you have to click on the page to press play! So I was thinking of making a UserScript that disables the script that does this. The only problem is, I already disable all the scripts on the site and when I do it still pops up. Is there a way that I can disable them ? I'm also using jQuery, so if I can do it through their interface, that would be great.
edit: Two perfect examples of such sites are daclips.in and gorrilavid.in
I have Adblocker Plus, and it seems like it is not recognizing "on Click" events as pop-ups, rather normal clicked links. And the logic is simple, no Adblocker will block you from clicking something intentionally and it (the link) opening in another window/tab.
The problem is the new window contains your clicked Url, while the original window/tab "Refreshes" (i.e. redirects) to another url.
Advertising companies seem to use this trick to bypass adblocking software.
Just ditch Chrome and use Firefox. Firefox already have built-in mouse-click popups. I think all addons like Adguard or Adblock can not disable mouse-click popups. If you use Firefox, these are the steps:
Type about:config in the browser's address bar and hit the enter key.
First time users need to confirm that they be careful on the next page.
Type or paste dom.popup_allowed_events into the search field.
The value of the preference highlights all events that are allowed to spawn popups.
Edit the value to remove some or all of the items here.
Why not just use a browser extension such as AdBlock?
https://chrome.google.com/webstore/detail/adblock/gighmmpiobklfepjocnamgkkbiglidom?hl=en
My go-to is right click and open in new tab. onClick events only happen with a left click. It's cumbersome but it still ends up being less work than closing the pop-up and whatever annoying prompts it may have.
I do not there's a practical solution for this.
Moreover, I think some of the answers here are missing the specific case in OP, where clicking anywhere on the page will cause the pop up to happen, not just clicking on links. According to this, neither right-clicking then choosing "open", nor noticing and blocking the target URL will help. I do not know of an add blocker that helps here either, because it's not trivial to meaningfully filter a click event that is taking place on the whole page object.
Only the solution provided by #Monkey would work, at the drawback of possibly breaking other things.

Window opens with toolbar even when using toolbar=no

I'm working on an app that uses this to open the homepage of the application after they have successfully entered the login details:
window.open("app_homepage_url","myApp","toolbar=no,status=yes,scrollbars=no,location=no,menubar=no,directories=no");
This normally works as expected and hides the toolbar but one user has reported that they still have the toolbar showing. They are using Internet Explorer.
My first thought was maybe there's already a window open with the name 'myApp' and the toolbar shown so the homepage is getting loaded into that window instead of a new one but I tried doing that and the application does not let you log in due to the way the login is handled with sessions so I don't think it can be that.
The comment on this answer seems to suggest there is a way to force the browser to always show the toolbar so I wonder if it is to do with this. Does anyone know how you actually do this?
Any other ideas?
This might be a browser configuration issue.
Have a test on the following configuration settings on IE7 and IE8:
Goto Tools->Internet Options->Security Tab
Select an appropriate security zone settings
Click on Custom level.. button
On the settings tab, look for Miscellaneous Node
Then have a test on the following settings
Allow script-initiated windows without size or position constraint
All websites to open windows without address or status bar.
Closing the browser might helpful when changing configuration settings.

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