7 ms·
You can open tabs using `var w = window.open()`, and close them using `w.close()` Only thing is users must disable popup blocking for HN, in the unlikely case
by zuck9 10y ago
You can open tabs using `var w = window.open()`, and close them using `w.close()`
Only thing is users must disable popup blocking for HN, in the unlikely case that you want to open more than 2 links at once, or open links when the user doesn't click something (like onload).
- dang 10y agoThat sounds promising. Is it possible to send messages between the tabs? My Chrome extension does that like crazy. Forgive me for hijacking this thread into technical support territory, but I've found documentation on this stuff hard to come by.
- zuck9 10y agoYep, using w.postMessage()
- dang 10y agoThat's pretty close to what I'm doing now, but I use chrome.tabs.sendMessage. Thanks; I'd love to get this working without that dependency.
- jakub_g 10y agoAFAIU postMessage() works between the frames (even cross-origin) within a tab. If you need communication between the tabs, localStorage is the way to go
- skeoh 10y ago`postMessage()` works between ANY window references -- tabs or frames. As long as you have a reference to the window (which you will if you call `window.open()`) then you can post messages to it.
- deleted 10y ago[deleted]
- koolba 10y agoYou can do that with local storage. Not sure if there's an existing project that simplifies the process but the general idea is to use it as a hash map that's available to all tabs attached to the same domain. You'd have to synchronize things yourself to prevent tripping over yourself. Ditto for firing off functions in response to events (i.e. Poll loop and fire handler).
- JBReefer 10y agoSo just straightforward cache invalidation-style stuff?
- pcmaffey 10y ago>Is it possible to send messages between the tabs? Save objects to local storage: localStorage.name = "dang"; alert(localStorage.name);
- jakub_g 10y agoyes, the localstorage is the way to go You save stuff to localstorage in one tab, and in other tab you listen to "storage" event: window.addEventListener('storage', function() {..}) [0] https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage... [1] https://developer.mozilla.org/en-US/docs/Web/Events/storage https://developer.mozilla.org/en-US/docs/Web/Events/storage Another option is shared web workers, though I never used it. [2] http://stackoverflow.com/questions/12107424/communication-between-browser-tab http://stackoverflow.com/questions/12107424/communication-be... [3] https://developer.mozilla.org/en-US/docs/Web/API/SharedWorker https://developer.mozilla.org/en-US/docs/Web/API/SharedWorke... I confused things in my other comment (I deleted it)
- johnsonjo 10y agoAlso something that could be promising depending on the circumstances you could use a shared webworker. They allow you to share a webworker script across multiple tabs. I think you have to be from the same domain, but I assume that you have to do that for local storage.
- amichal 10y agoit used to be possible to simply access the other DOM via window. As long as they where in the same context (basically domain) Edit: Yep w2 = window.open('/'); w2.document.click() Is fine if complying with the sameOrigin policy https://developer.mozilla.org/en-US/docs/Web/API/Window/open https://developer.mozilla.org/en-US/docs/Web/API/Window/open
- m52go 10y agoRe: hijacking. It's OK, I'm sure the mods will take care of it. EDIT: I know this isn't reddit but I couldn't resist. Dan it's admirable how nice and down-to-earth you are in spite of all the stuff you see here everyday...huge thanks for all your work in making HN what it is.
- tedmiston 10y agoCan you elaborate on what the extension does? e.g., for moderating comments more effectively, for reading deeply nested comments, some kind of internal actions we don't have access to, etc.
- dang 10y agoHmm, let's see. It does a lot of moderatory things, but general readery things include highlighting stories or commments and jumping to related pages (e.g. the story url, the thread url, the user's profile page, the user's comments page, other stories from that domain), opening and closing those tabs; navigating up and down comment trees (go to parent, go to prev/next sibling); marking stories or comments as 'seen' and thus hiding them, but keeping track of whether 'seen' items have changed and displaying how they changed; finding comments that match regexes and highlighting the matches; if a comment contains a url, open that url in a new tab; jump around between open tabs; go to the root url of a comment (i.e. the original story item); select text and open tabs based on that text; show HN search results in the form of an HN story list that looks like the front page; take various actions based on what's in the clipboard, or copy HN urls or titles to the clipboard; filter stories or comments being displayed according to various criteria; upvote or flag a post; go to the internet archive for a url; show the current rank of a story on pages other than the front page; request the 'more' url at the bottom of a page and inline its results instead of navigating to it. Edit: I forgot one of my favorites, which is aligning the current selection to the center, top, or bottom of the window, a la ctrl+L in Emacs. It's all done by keyboard shortcuts, which means the mapping to keys is pretty complicated and not as consistent as I'd like. Edit: It's also written in a Lisp we made that only three of you have ever heard of :)
- tedmiston 10y agoThat's a great list. Some of these specific features I've considered building as an extension from a reader perspective. Especially for dealing with deeply nested comments, and "find the top HN discussion if any for this URL that I'm currently on". And you can compile that Lisp to JavaScript to build the Chrome extension?
- tptacek 10y agoYes: there's a couple ways to do it, but the most common is to use localStorage and the 'storage' event, which will post in every open window when you modify localStorage. It's very easy to check for localStorage support (some people disable it just like cookies) and it works, I think, everywhere except Opera, in almost every mainstream version. (I use it a bunch).
- joshschreuder 10y agoWorks in Opera too so far as I can see: http://caniuse.com/#feat=namevalue-storage http://caniuse.com/#feat=namevalue-storage
- jakub_g 10y agoAs parent said, you just need to store a handle to the window when you open it, and then you can use .close() https://developer.mozilla.org/en-US/docs/Web/API/Window/close#Examples https://developer.mozilla.org/en-US/docs/Web/API/Window/clos...
- dang 10y agoWhen opening a tab are you allowed to say where you want it in the tab order? or to change its position in the tab order later? Thanks everyone for all the help!
- tempestn 10y agoUnfortunately I don't think so. In fact you can't even specify that you want it to be a tab, as opposed to a new window, but the default in all major browsers is a tab these days. I haven't tested, but you might be able to kind of fake it by having the tab immediately before the one you want to open do the opening. That wouldn't work for tabs that have opened external sites (the stories themselves), but you could at least send a message to one of the other HN tabs telling it to open your new tab, which should give you some control.
- dang 10y agoI see. The Chrome.tabs library that I use does allow this. Your clever workaround might be enough though. While I'm at it, is there a way to give a regular web app the kind of permissions that a Chrome extension manifest can give? It isn't just tab mechanics I need, but clipboard access and cross-domain privileges. Obviously this would still require client consent (and might still be Chrome-only) but it would be easier than an extension with all its deployment headaches.
- jakub_g 10y agoThere's a clipboard API [1] in newest browsers but browser support varies. Regarding cross-domain, what do you want to do exactly? In general there's no built-in support at least in Firefox to override default same-origin policy, but there are Chrome/Firefox extensions that for instance inject CORS headers to each response. Edit: In Chrome, it seems you can disable same-origin policy with command line flag [2] [1] http://caniuse.com/#feat=clipboard http://caniuse.com/#feat=clipboard [2] http://stackoverflow.com/a/3177718/245966 http://stackoverflow.com/a/3177718/245966
- deleted 10y ago[deleted]