1

So I have this problem where I need to open a none active tab and once in a while to set it's URL, everything is working well until an undefined period of time passes, then it seems like the alarm that responsible to set the URL dies along with all of the background script data (variables are wiped), in my manifest I set permission of "background" but it didn't help, I also tried using setInterval but it didn't help much, here's some code for you:

async function setGetJobAlarm() {
    // try {
    //     chrome.alarms.clear("getAndExecuteJobs");
    // } catch { }
    // chrome.alarms.create("getAndExecuteJobs", { periodInMinutes: 0.3 });
    // chrome.alarms.onAlarm.addListener(async (alarm) => {
    //     if (alarm.name == "getAndExecuteJobs") {
    //         try {
    //             await getAndExecuteJobs();
    //         }
    //         catch (err) {

    //             console.log(err);
    //         }
    //     }
    // });
    if (getAndExecuteJobs > 0) {
        clearInterval(getAndExecuteJobsInterval);
    }
    getAndExecuteJobsInterval = setInterval(async () => {
        try {
            await getAndExecuteJobs();
        }
        catch (err) {

            console.log(err);
        }
    }, 30000);
}

Manifest:

{
  "name": "aaaaa",
  "version": "0.0.1",
  "manifest_version": 3,
  "background": {
    "service_worker": "bgjob.js"
  },
  "permissions": [
    "tabs",
    "alarms",
    "activeTab",
    "background"
    //"identity",
    //"identity.email"
  ],
  "host_permissions": [
    "http://*/",
    "https://*/"
  ],
  "icons": {
    "16": "aaaaa.png",
    "48": "aaaa.png",
    "128": "aaaa.png"
  },
  "action": {

    "default_popup": "/popout/pop.html",
    "default_title": "aaaaa"
  },
  "content_scripts": [
    {
      "matches": [
        "http://*/*",
        "https://*/*"
      ],
      "js": [
        "jquery-3.6.0.slim.min.js"
      ]
    }
  ]
}

I'm not able to figure out what is missing, googled a lot but no use,

Second problem is that I'm trying to load a simple extension's html file named "hello.html", the html get's opened but I get this error:

Cannot access contents of URL"chrome-extension://locblcbeeombbgmpiofcnmhfimfpjipb/hello.html". Extension manifest must request permission to access this host.

I tried to add "chrome-extension://*/" but didn't work, thanks!

Amit Shadadi
  • 617
  • 2
  • 7
  • 20
  • Don't post multiple problems in one question, open a new one with a proper [MCVE](/help/mcve). – wOxxOm Mar 13 '21 at 20:41

1 Answers1

3

The background script automatically terminates after 30 seconds so setTimeout/setInterval with a delay like that or longer will never run.

Remove setTimeout/setInterval and use chrome.alarms API with a periodInMinutes at least 1 because this is the minimum interval allowed for published extensions in the web store.

If your workflow really needs intervals below 1 minute you'll have to prolong the service worker's life artificially, see the second part of this answer.

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • Thanks for your answer though I'm not sure I am getting you, if background script dies after 30 seconds, what good will it make if I set periodInMinutes to 1 minute? I did use Alarm as you can see, i just tried different things since I couldn't get it to work, also, why will the background script terminates if I set "background" permission? – Amit Shadadi Mar 13 '21 at 20:46
  • `chrome` API will wake the background script, of course. But you've used setTimeout/setInterval to wait another 30 seconds after chrome.alarms has woken the script, then the script died again. As for `background` permission, it's not what it does, see its [description](https://developer.chrome.com/extensions/declare_permissions), it won't help here. – wOxxOm Mar 13 '21 at 20:52
  • Thanks, I'm new to chrome extensions and didn't knew that, I'll try it in a clean background file now, as for the setInterval, it was like an option B, they didn't ran together, I just comment what didn't work and tried a different approach. – Amit Shadadi Mar 13 '21 at 20:54
  • When it "Wakes up" the background script, does the background script recreate all variables? say i save a global variable of tabId and init it with 0, when the bg script gets awaken will it set tabId =0 again ? – Amit Shadadi Mar 13 '21 at 20:59
  • Yes, it resets every variable. It runs the background script each time from zero. You can use the asynchronous `chrome.storage.local` – wOxxOm Mar 13 '21 at 21:06
  • Just incase anyone having the same problem with background script stop executing, not sure if it's the right way to do it but my problem was that I created the alarm inside the background script rather than at the content_script, once I changed the creation location everything worked as expected and as wOxxOm said - background.js woke up as expected. – Amit Shadadi Mar 14 '21 at 16:40