Chrome to read when window is focused,aka opened - javascript

I am still fairly new at programing. I have played around with JavaScript before, but it is still tricky for me.
I got this great idea for an extension for Google Chrome- in the future it would be nice to port it into other browsers. For now I think Google Chrome would be the easiest way to develop for.
I investigated a little and finished the kitties tutorial on the extensions site.
From there it makes sense- easy for the most part, but my idea sounds impossible to me. Simply, the extension would automatically reload the browser when window is selected or focused on the screen. Saves time by not pressing Ctrl+R (PC), or Cmd+R (Mac), or reload button every time the developer checks an update on code.
I was reading through the API documentation and found the method
chrome.browserAction.onClick, is there like chrome.browserAction.focused? Is this idea even feasible?
I also have to take in consideration that Chrome is visited by multiple OS. I wonder if Mac OS, Linux, and Windows version need different JavaScript instruction to pull this off? This simple idea is overwhelming...
Thanks for input in advance :)

Sounds like you'll want to use chrome.windows.onFocusChanged:
Fired when the currently focused window changes. Will be chrome.windows.WINDOW_ID_NONE if all chrome windows have lost focus. Note: On some Linux window managers, WINDOW_ID_NONE will always be sent immediately preceding a switch from one chrome window to another.
Here's an example of how to reload a newly-focused window's active tab:
chrome.windows.onFocusChanged.addListener(function(windowId) {
if (windowId != chrome.windows.WINDOW_ID_NONE) {
chrome.tabs.query({ active:true, windowId:windowId }, function(tabs) {
if (tabs.length == 1) {
var tab = tabs[0];
chrome.tabs.reload(tab.id);
}
});
}
});
You'll also need to declare the tabs permission in your manifest.

Related

how return focus on parent window using javascript?

is it possible return focus on tab parent using javascript?
I read some threads about this problem, but i didn't find solutions.
I tried
window.opener.parent.focus()
and
window.opener.focus();
but it doesn't work.
Can someone help me?
Thanks
Generally, you cannot do this inside a web page. Because it's the user's choice which tab/window she wants to focus on and browsers such as firefox and chrome respect such choices by providing configs to open new tabs in the background or not. But under several very special cases, you may still achieve this.
If you want to open a new tab and return focus immediately, you can try to simulate a 'ctrl+click' event on a link to open the tab on the background. Refer to this thread Open a new tab in the background?(Only for chrome, API may already changed. So it may only works on an obsoleted version)
If you are shipping with an extension, do it in the extension code. For example: in chrome extension.
If your script is for a customized browsers which you have control on / you can affect the design, you can implement the function in the browser side and expose an API for your script.

Detect Chrome Extension background page console opened

Just like the title says, I'm wondering whether it's possible to detect when I click on "Background Page" for my test plugin on the "chrome://extensions/" page.
Right now when I open the background page, the console is undocked. I saw this post and found ways to make it work inside each content script. But is there a way to make the plugin detect it's own console opening?
This function from that post inside my background.js doesn't seem to detect anything.
chrome.tabs.query({url:'chrome-devtools://*/*'}, function(tabs){
if (tabs.length > 0){
console.log('opened');
}
});
Any advice would be highly appreciated.
Devtools windows are not visible to tabs/windows APIs anymore.
This has changed since the answer from '14 was posted - therefore this method no longer applies.
Unfortunately, I can't find a link to the Chromium issue that implemented that.

Is there a way, using a possibly a chrome extension, to bring Chrome into focus in windows when a different app has focus?

Use case:
1) user has chrome open on my website (which, by the way, is an iframe from which I don't have security sandbox privileges to access the outer window).
2) user goes to different application; MS Word for example.
3) a js event fires on my website (rendered in an iframe) and should bring chrome into focus. This is an internally-facing web app.
I tagged chrome-extensions because it seems like this should be possible:
google calendar already does exactly what is detailed above.
chrome.windows.update(windowId, {focused: true}) would do what I need... is there already a chrome extension that has hooks into this method?
To detect switching from Chrome to another application, you can indeed use chrome.windows via a chrome extension. Also, simple firing alert() will bring the browser application back into focus.
chrome.windows.onFocusChanged.addListener(function(window) {
if (window == chrome.windows.WINDOW_ID_NONE) {
// application lost focus
alert('Don\'t leave me!');
}
});

Userscript won't close window if it isn't active

I'm brand new to StackOverflow and userscripts, and I'm trying to get a fresh start!
Worthless information aside, I'm having a bit of trouble with a tiny script I recently whipped up.
(function () {
$("#enbut").click();
setTimeout(function () {
open(location, '_self');
window.close();
}, 100);
})();
What it does is clicks a button, waits a second for the website to register that I've clicked it, and then it closes the webpage.
The only issue that I am having is that it does not seem to want to close the webpage when the tab isn't the one I currently have active (Opened up so I can see it).
The script works fine if I open the webpage directly, but if I right click and open in a new tab, it's able to click the button, but the page doesn't close until I open up the tab.
Is there any reason this would be happening, or any way to fix it?
I'm using Chrome, so maybe it's just a browser security feature like what they have done with closing windows?
window.close(); throws a security issue when used as a general userscript in today's browsers. Even when using the work around ( open(location, '_self'); ), it does not seem to allow it in tabs that are not the active window.
In order to resolve this, I had to convert the userscript to a Chrome Extension, which gives the script full control over Chrome's security measures. I don't think there would be any other way to get this working as a plain userscript without messing with Chrome's internals, which would be a stupid thing to do for a simple script.
Thanks for the help, guys!

Preventing Gmail From Closing / Keeping It Visible

Disclaimer before the uppity types start in: This is not to be deployed "in the wild". This is for local, personal use by a Chrome add-on only. I am not trying trick visitors to my sites or do anything else unsavory. I've seen a bit of chastising of those wanting to hook onbeforeunload.
Some Background
Far and away my biggest gripe with Chrome (at least on Mac OS) is its tab close buttons which, when I have several tabs open (as I usually do), results in my frequently closing tabs unintentionally as the tabs themselves become fairly small and thus the click target area that isn't covered by the tab close button is very small. On certain tabs with news stories, blog posts, documentation, etc. it's annoying but not a huge inconvenience. I just CMD+T to reopen the tab and there's no real harm done. However, certain tabs, in particular Gmail, have definite downsides to being closed. I frequently have one or more chat dialogs with coworkers open and reopening a tab does not restore the chat dialog, and initiating a new chat loses me the current chat history (yes, it's saved, but it's not conveniently accessible by scrolling backwards in context). There are a handful of requests for Google to add the option to simply remove tab close buttons (I prefer to use CMD+W myself), but I'm not holding my breath on that.
A Partial Solution
A while back a friend pointed me towards the dotJS Chrome add-on which allows per-domain custom JavaScript execution, akin to GreaseMonkey, but slightly different. At any rate, it gave me a means to "fix" issues/wants on a number of sites I visit regularly, and I've found it to be very helpful thus far. The other day it occurred to me that I could probably keep my Gmail, et al tab(s) open with a bit of JavaScript. I threw together a small script that based on a regular expression would prompt you before closing a tab. The gist of the code is as follows:
var unloadHandler = function(e) {
if (/(mail.google.com|google.com\/reader|gmail.com)/.test(location)) {
return 'Are you sure you want to close: ' + location.host;
}
};
window.onbeforeunload = unloadHandler;
Lo and behold, this did the trick for most of the sites I tried it on; except one: Gmail. Let me correct that: it worked, insofar as it prompted me to confirm the closing of the Gmail tab, and if I opted to not close it, it would keep the tab open, but before the dialog prompted me, the page had gone completely white. Element inspector shows that the markup is all still there (so far as I can tell), and the styles on the elements shouldn't be hiding things (i.e. display: none; visibility: hidden; etc) and the elements' positioning is still correct (e.g. they're within the viewable area). I went through one-by-one removing elements to see if anything was obscuring the Gmail interface, but was never able to reveal it on screen. I cannot for the life of me figure out what's going on. I'm not sure if Gmail is hooked into some event I'm unaware of (an on*before*beforeunload?), or if Google's browser is doing something special with their Gmail page, or what is causing the strange behavior. Google Reader is unaffected by this oddity (I can prevent close and retain the contents of the page) as are all of the other sites I've tested with.
Does anyone know what might be causing this issue?
For the record, I am running the following: Mac OS X 10.6.5, Google Chrome 10.0.648.205, and dotJS 1.3.
I appreciate any feedback, but I am not looking for solutions that involve: pinning tabs, changing my workflow/usage (e.g. not selecting tabs with the mouse), etc. I really want to figure out what, specifically, Gmail (or perhaps Chrome?) is doing that is sabotaging my efforts here. Thanks in advance.
I think Gmail's own code causes this behavior. I can reproduce your problem (on linux machine) with subscribing for this event from the console with this function
window.onbeforeunload = function(e) {
return "Hey what\'s wrong with you?!";
};
After it I've started the developer tool profiler, the last call after closing the window (and choose the 'stay on this page' answer for the question) is a removeChild function call, which removes some content from the <iframe id="canvas_frame" />. So the content elements are not there anymore.
function Fc(b) {
return b && b.parentNode ? b.parentNode.removeChild(b) : m
}
I've found some 'traces' in the obfuscated code for subscribing for the beforeunload event, but it's hard to be sure :)
function It(b, a) {
this.Qc = jCa++;
this.ea = b;
this.ka = new J(this);
this.Qa = a;
this.Ka = [];
this.Za = !1;
this.ka.ya(this.ea, "unload", this.Da);
this.ka.ya(this.ea, "beforeunload", this.ab);
Ypa(Zd(a), this);
this.ia()
}
I tried to reproduce this behavior in Firefox but I think google ships different javascript codes for different browsers so I can't reproduce it.

Categories

Resources