Is there a way to get read/unread status of a tab via extension?

  • I have a private extension made with a function of hibernating new background tabs. Hibernating the tab when it is just created doesn't work - the tab doesn't get the url at all and stays blank; adding a delay isn't a good approach either, since each time it takes a different amount of time to get the url. So, at the moment I am using chrome.tabs and an array of tabIds to know which tabs to hibernate(onCreated adds the tabId to the array, each onUpdated checks if this tabId is in the array, acts accordingly). However, this approach doesn't work very well... If user is faster than page loading(if you are very fast, it works ok, if you are about as fast the page load - which is usually the case, conveniently - the tab becomes stuck at empty and has to be cloned or url reopened in new tab, to get to the webpage). And tabIds tend to change when other tabs are closed(maybe even when new tabs are opened as well), for some reason, so some of the tabs don't hibernate at all. Unread markers seem to stay put with the tab and the read/unread bool could be very useful for this, if it exists.
    So, the question: is there a bool for each tab's read/unread status and is there a way to read it from an extension? Or maybe there is a better way to go about hibernating new background tabs?

  • Moderator

    As i know we have no API yet to access the read/unread state of a tab.

  • kk, tnx

  • Moderator

    @ostankin There isn't an official API, but I suspect vivaldi is adding in something special of its own which might make this possible!

    If you use your command to get a Tab object, that looks like this:

    active: true
    audible: false
    autoDiscardable: true
    discarded: false
    extData: {"read":true,"useDefaultIcon":false,"ext_id":"cjsqeocam00043s5ltkq45w11"}
    favIconUrl: ""
    windowId: 10

    You'll notice that inside extData there are some extra custom keys that vivaldi uses. One of those is "read", which either appears with the value true, or doesn't appear at all if a tab hasn't been read yet.

    There doesn't seem to be any official documentation for this, but you can easily use that to read if a tab is read/unread. I suspect you can also write changes to it.

    Just be careful to JSON decode/encode it as a string when setting or reading from a tab's extData.

  • @LonM Ayyy, thank you very much! Works as I envisioned! ❤
    I doubt it's the very best way to do it, but if anyone is interested in a simple auto-hibernation for new background tabs, here's what I got in background.js:

    chrome.tabs.onUpdated.addListener(function  (tabId,info,tab) {
        if (!(/"read":true/.test(tab.extData))&&tab.title!=""&&!(/^http/.test(tab.title)))
    		chrome.tabs.query({active: true, currentWindow: true},function(tabs) {
    			if(tabs[0].id!=tabId) chrome.tabs.discard(tabId);

    Interestingly, tab.url is always present, but if you hibernate the tab right away, it will stay empty on wakeup, so I check the title instead, and to make it more user-friendly, wait for it to display the actual title of the page, not just url, as it does at first. :3 Thanks again!

    Edit, right away: There still seems to be a window of time when the page becomes unresponsive, so it will need more work. If you can read this, I still wasn't motivated enough to fix it 😛

Looks like your connection to Vivaldi Forum was lost, please wait while we try to reconnect.