Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect an item in a search result page has been opened in another browser tab

I searched through SO and this question has been asked before several times, but I found no answers that explain how the behaviour I want to implement is actually realized.

What I have in mind is to mimic booking.com feature, in which when you open a search result from the search results list, a div indicating that the result has been opened in another tab (or window) appears, and it fades away when that other tab (or window) has been closed.

Screenshot: enter image description here

First attempt was to check if some javascript has been attached to the links, but that's not the case, because if you simply copy the url from the page source, and open it, the feature works.

So I thought it could be someway managed on the server side, fired up wireshark to try to trap any ajax calls between the two opened pages and the server. There were many requests, but no one seems to pass the needed info (I guess the hotel ID or some url). Also note that opening the result item URL, which includes the session ID, in an incognito window or in another browser (I mean, firefox instead of chrome) window, breaks the feature; so that excludes any session or ip address tracking.

Last attempt I could think was the search results page is polling some cookie which is set on the domain when another page is opened and unset when it is closed. So I checked the cookies, whose content is actually a little cryptic to understand, but what I have seen is that no cookie actually is changed on other tab opening and closing, so, again, it cannot be done this way.

What's left?

like image 868
guido Avatar asked Nov 03 '22 03:11

guido


1 Answers

In theory, it should be possible to use localStorage/sessionStorage to do this by setting the current page URL on every page, and checking every once in a while on the "original" page to see if the value is not the same as document.location.

If you set the current time in the storage thing as well, you can check to see if the page is closed by seeing if the "last tab time" is, say, more than 30 seconds ago, or some such.

As for the website, I visited it, and did a search, and didn't see any warnings or anything about new tabs being open at all when I tried it.

like image 138
Ross Brunton Avatar answered Nov 09 '22 09:11

Ross Brunton