0

So I have two questions here. The first is:

I am trying to write a code for an extension that closes idle open tabs in Google Chrome after a set amount of time. For testing, I have made this set time to be 10 seconds. This is my manifest.json

{
  "manifest_version": 3,
  "name": "Idle Tab Closer",
  "version": "1.0",
  "permissions": ["tabs", "idle"],
  "background": {
    "service_worker": "background.js"
  }
}

And this is my background.js

chrome.idle.onStateChanged.addListener((state) => {
  if (state === "idle") {
    chrome.tabs.query({}, (tabs) => {
      const currentTime = Date.now();
      const tenSeconds = 10 * 1000; // 10 seconds in milliseconds

      tabs.forEach((tab) => {
        chrome.tabs.get(tab.id, (currentTab) => {
          if (currentTab.lastFocusedWindow && currentTab.lastFocusedWindow.focused) {
            const tabLastActive = currentTab.lastFocusedWindow.focusedTime || currentTab.lastFocusedWindow.timestamp;
            if (currentTime - tabLastActive > tenSeconds) {
              chrome.tabs.remove(tab.id);
            }
          }
        });
      });
    });
  }
});

But this code does not work. It does not result in errors, but the tabs still do not close after this set amount of time. What I do is I switch to a tab and then wait for the other tabs to close but they never do.

The second question is simple: Is there a place to keep asking questions safely and get help with my project without spamming stackoverflow or get into risk of having my account closed?

Thank you!

Dark Eagle
  • 107
  • 3
  • 10
  • There are no things like lastFocusedWindow or focusedTime in Chrome, see [the documentation](https://developer.chrome.com/extensions/tabs#type-Tab). Until https://crbug.com/1419613 is implemented you'll have to track each tab's activation in another event like chrome.tabs.onActivated and store the data in chrome.storage.session, [example](/a/73090402). P.S. [How to see background.js console?](/a/10258029) – wOxxOm May 28 '23 at 03:11
  • @DarkEagle - Did you use ChatGPT to generate the code? – Thomas Mueller May 28 '23 at 04:12
  • @ThomasMueller I did in some parts of the code, but I have written other parts myself. The problem is I am quite used to manifest v2, and manifest v3 is presenting me with some problems. – Dark Eagle May 28 '23 at 12:00
  • @DarkEagle - "I did in some parts of the code" - You shouldn't have done that, because 1) Stack Overflow has a blanket ban on AI-generated content, for both answers and questions, see [Temporary policy: ChatGPT is banned](https://meta.stackoverflow.com/questions/421831/temporary-policy-chatgpt-is-banned) 2) ChatGPT loves making stuff up out of thin air. As @wOxxOm already said, `Tab.lastFocusedWindow`, `Tab.lastFocusedWindow.focusedTime` and `Tab.lastFocusedWindow.timestamp` simply do not exist. ChatGPT invented them. – Thomas Mueller May 28 '23 at 17:48
  • @DarkEagle - I'm willing to help people with code they've written themselves, even if it's really bad code. But I refuse to fix code written by an AI, so I'm done with this question. Sorry, not sorry! – Thomas Mueller May 28 '23 at 17:50

0 Answers0