0

In my chrome extension app inside the background script, I am trying to implement this code, here is my code:

function foo(title, message, timeout) {
    chrome.notifications.create({
      type: 'progress',
      iconUrl: 'img/GS icon.png',
      title: title,
      message: message || '',
      progress: 0
    }, function(id) {
      // Automatically close the notification in 4 seconds by default
      var progress = 0;
      var interval = setInterval(function() {
        if (++progress <= 100) {
          chrome.notifications.update(id, {progress: progress}, function(updated) {
            if (!updated) {
              // the notification was closed
              clearInterval(interval);
            }
          });
        } else {
          chrome.notifications.clear(id);
          clearInterval(interval);
        }
      }, (timeout || 4000) / 100);
    });
  };

  window.onload=function(){
    foo('ttttitle','messsageee',10000);
  }

but it always stays on 1% and doesnt move:

enter image description here

can not find the problem.

manifest.json:

{
    "background": {
        "scripts": ["js/background.js"],
        "persistent": false
    },
    ...
}
Codey
  • 487
  • 1
  • 8
  • 27
  • You're basically repeating the [same question](/q/58541634/), but you don't provide more details like manifest.json and where the posted code runs. Extensions have many moving parts so it's important to identify each one exactly. Also it's not clear why you can't use [the answer I created](/a/58577052) specifically to solve the case of nonpersistent background script. – wOxxOm Oct 29 '19 at 09:25
  • Note, if you run this code in the popup, the popup runs only while it's shown, all the code in the popup is terminated when the popup is hidden, so you'll need to move the code into the background script and use messaging. – wOxxOm Oct 29 '19 at 09:36
  • It runs on background script – Codey Oct 29 '19 at 09:36
  • Good, one point down, several more to go. – wOxxOm Oct 29 '19 at 09:37
  • No. One more point to go. – wOxxOm Oct 29 '19 at 09:40
  • I tried to call `allowUnload()` from the onload function but no change – Codey Oct 29 '19 at 09:44
  • Uhm, you need to run preventUnload() to ... prevent the automatic unloading of the background script. – wOxxOm Oct 29 '19 at 09:46
  • I have no page named `bg-iframe.html` – Codey Oct 29 '19 at 09:48
  • It's in the answer. Create the file manually and copy the code from the answer. – wOxxOm Oct 29 '19 at 09:49
  • I've added a more verbose example in [that answer](https://stackoverflow.com/a/58577052). – wOxxOm Oct 29 '19 at 10:02
  • @wOxxOm Are you sure about the line 'chrome.runtime.connect();' ? it gives error `Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.` (I added the files bg-iframe.html and bg-iframe.js ) – Codey Oct 29 '19 at 11:07
  • Should work and works for me. Sounds like you forgot to add the onConnect listener shown in the answer. – wOxxOm Oct 29 '19 at 11:10
  • Still got same error :( – Codey Oct 29 '19 at 12:24
  • Well, since it works for me I can only assume you've copied it wrong or didn't reload the extension after making changes. – wOxxOm Oct 29 '19 at 12:30

0 Answers0