Get last visited site in another window using Javascript - javascript

I am creating a Firefox extension where I need to find out what was the last location visited in another window. Here is an example of what I mean. I present the user with a list of sites from a main interface window. The user clicks on one of the sites and a new window is opened to that site. From there, the user will navigate the site in the new window and will eventually stop browsing or close the window. I need to find out what was the last visited location from that window. Also, the user may be navigating more than one site at a time, so there could be many windows open. I need to know what was the last location for every one of them. So far, the only way that I got it to work is to use a timer and poll the new window every second for document.location.href. There must be a better way.

As far as I know this is not possible in (pure) Javascript due to security/privacy considerations. On the other hand, since you are writing a Firefox plugin you have additional mechanisms you can use.
For instance, you can create a greasemonkey plugin that is applicable to every site. This plugin will record the href using GM_setValue(). The main plugin will read this information via GM_getValue().

Related

Opening a link in a different browser

I'm trying to find a way to take a link from one browser and open it in another browser. This could be taking a link from a Firefox tab and opening it in Chrome, or taking a link in a Chrome Incognito window and opening it in a non-incognito Chrome window.
Here's some more detail. I have a webpage that refreshes every second, and uses javascript(via Greasemonkey/Tampermonkey) to search for certain keywords. When a keyword in my list matches one associated with a link on the page, it automatically opens that link in a new tab. If it's possible, I need to take those links to a different browser somehow, automatically.
AFAIK, something like this isn't possible with javascript due to security issues. The only two solutions I can think of are:
1: Using AutoHotKey to make a macro to copy the link, alt-tab, and paste into the other program. This is manual, I want something automatic. EDIT: I realized I can use AHK to monitor a page, but I don't know if it could be done without introducing more latency than I would like. Keeping the total time from the webpage refreshing to opening the link as low as possible is the most important thing.
2: Having some other program handle it for me. I'm not aware of any and wonder how difficult/costly it would be to roll my own or have someone make one. I'm not even sure if I could interface it with my current script.
I'm fairly certain it would be possible with number 2, although I don't know about cost or difficulty... could there be another way to accomplish this?
For reference, this is the relevant section of code that I'm currently working with. It opens any link which matches a list of keywords in a new tab. These are the links I'm trying to figure out a way to open in a different browser. It uses dynamic object names and a dynamic URL, but essentially this is just saying if the checkboxes are checked and a link matches my autoOpenList(keyword list), then open the link in a new tab.
if(jQuery.inArray(autoOpenTemp,autoOpenList) != -1 && window['autoAccept' + autoOpenTemp].checked && autoAccept_input.checked ){
var tempURL = LINK_BASE+obj.acc_link;
window.open(tempURL, '_blank');
}
Use Java's HttpServlet Class to create a web application. You can setup the server by Tomcat. Servlets Quick Guide.
Start CLI by Java and open browser through CLI.
Call the web application by url on your page.

How to turn off sound if audio is playing in another tab or window

I am fully aware that playing background music in a website is a horrible idea. However, my client insists on it. We've had all sorts of trouble with it. First, we had to find a script to have the song "follow" the browser (it's called "HTML5 background audio player" for those looking for such a thing), remembering where the song was cut off between page loads.
However, we now have a new problem. If the site is open in more than one tab or window at a time, the music plays over itself.
I am also fully aware that the simple solution is to just manually mute the sound if you decide to open up another page in the site in a new tab or window. I discussed this. However, my client is a very "particular" man and insists that the computer "should know when I open a new page."
My question is: does such a thing exist? I have no idea how this would be accomplished in JavaScript.
You should use cookie or/and HTML5 session storage to record that your playing has started.
Then if it has, you don't start it on subsequent page loads.
If you are talking about dynamically opening a new tab, by clicking on a link and having that click call window.open();, then .open() returns a reference to the child window, which you can then communicate with in the parent script: var childWindow = window.open(url);
Otherwise, cookies?
A session-parameter in the query string, which you could change if a new tab was opened.
Another suggestion, if you're using html5-audio exclusively, would be something like using localStorage.
Most browsers supporting HTML5 window.Audio; also support window.localStorage;.
You'd have to expressly check to see that localStorage is indeed supported and enabled, but that's just good citizenship.
Then you should be able to set a randomized ID for the tab, save that ID in session-storage, set an "is_playing" or whatever field in localStorage to the ID you gave the tab in sessionStorage...
...and then on each new tab, check to see if "is_playing" is empty in localStorage, OR if the ID is equal to the one set in sessionStorage.
The difference between localStorage and sessionStorage is that sessionStorage is emptied after you close the browser, and it also should not be shareable between tabs.

How to identify browser tab?

I would like to identify browser tabs (on my domain) using JavaScript.
I mean that if user open several tabs with my website and submit web form only on one page I want to notify only this page, even if user moves from this page.
It should be max cross browsers solution.
P.S. One of the possible solutions is using "window.name" property, but I do not want to use it because somebody else can use it.
P.S-2: I found one more possible solution: using sessionStorage. It supported by FF3.5+, Chrome4+, Safari4+, Opera10.5+, and IE8+. Oooohhh, I need IE7!!!!
Thank you in advance!
I don't think this can be done. Each browser tab that is opened is basically like a new browser instance. Just like if the user opened another browser. One tab knows nothing about the other tab by design. This is how it should be. Can you imagine the implications if a web site developer could add code to their page to "see" what other sites you have opened in your browser?
window.name is the only persistent data element you can use for this purpose, as described your requirements.
I want to notify only this page, even if user moves from this page.
This is impossible. Once a user navigates away from a page, you lose control over that tab. You can't push to a page, it needs to make a server request FROM that page, even if it's ajax.
Using sessionStorage. It supported by FF3.5+, Chrome4+, Safari4+, Opera10.5+, and IE8+.
For IE7 using "window.name" property.

detecting user closing tabs or leaving the site

I'd like to be able to tell when the user leaves the site so I can update a field in my database (particularly, a last_visit field in order to determine unread items for each user). However, I'm not sure how to manage the fact that sometimes, a user opens several tabs of the site, so I can't use onbeforeunload reliably to accomplish this goal.
Ideally, I would be able to update this field only when there is only one open tab of the site.
On the hand, maybe I could get more functionality by simply using a table to record read items for several days and assuming that threads older than that are read by default.
What do you think?
Regards
All I can think of is using either cookies or local storage to update the time at which they're viewing your site on each page load. This way, once they close all the tabs where your website is open, the cookie/local storage entry won't update, and you can access that value later on when they return.
So run this every time the page loads:
window.localStorage.setItem('lastVisit',Date.now);
And to grab it:
var lastVisit = window.localStorage.getItem('lastVisit');

Is it possible to have browser open a link in already opened website?

I have an application where I have tabs where each tab represent a particular sub application.
I'd like to have all links such as site.com/* to open in an already existing browser tab, and somehow possibly firing a JavaScript event that a new link was received/opened so that I could open it in my application instead.
So basically, I'm building a web application and I'd like to have links opening in the same application instance rather than creating multiple instances of my application.
Is this possible?
Update: I'm talking about if someone sends a link over Skype or email, that the links would open in my application. I'm aware of most web technologies and I can't think of any approach, that's why I left this question if someone might have an idea. Or a plugin, if not, someday we might get one and I'll add the answer my own / update question.
Yes. Just use the target attribute of the links to open them in their sub-application browsing context (i.e. tab, window).
There is no event that fires on opening links. You may add a click event handler to all links, executing what you need when the URL matches.
If you want you want your web application to open specific protocols or mime types, even that is possible with HTML5. You can use window.navigator.registerProtocolHandler() and window.navigator.registerContentHandler(). See the HTML5 Draft on Custom scheme and content handlers.
If a users opens a new tab/window for a link he received by email etc, you usually can't catch and prevent that. But you could use Cross-document messaging, e.g. window.postMessage, to look whether your app is already opened somewhere in the browser and then close one tab and focus the other.

Categories