2

I'm writing a Google chrome extension and I need it to make an Api call and show a notification on every page load but I can't get it to work. The notification will not show except some random times that it will. This is the background code:

*background.js*    
window.addEventListener('load', function() {
        var opt = {
          iconUrl: "icon.png",
          type: 'list',
          title: 'Primary Title',
          message: 'Primary message to display',
          priority: 1,
          items: [{ title: 'Item1', message: 'This is item 1.'},
                  { title: 'Item2', message: 'This is item 2.'},
                  { title: 'Item3', message: 'This is item 3.'}]
        };
        chrome.notifications.create('notify1', opt, function() { console.log('created!');console.log(chrome.runtime.lastError)});
      });

Does anyone know how can it work on every page load? Edit: This is the manigest.json file:

{
  "manifest_version": 2,
  "name": "Chrome extension",
  "description": "form",
  "version": "2.0",
  "commands": {
    "toggle-feature-foo": {
      "suggested_key": {
        "default": "Ctrl+Shift+Y",
        "mac": "Command+Shift+Y"
      },
      "description": "Opens Popup.",
      "global": true
    },
    "_execute_browser_action": {
      "suggested_key": {
        "windows": "Ctrl+Shift+Y",
        "mac": "Command+Shift+Y",
        "chromeos": "Ctrl+Shift+U",
        "linux": "Ctrl+Shift+J"
      }
    },
    "_execute_page_action": {
      "suggested_key": {
        "default": "Ctrl+Shift+E",
        "windows": "Alt+Shift+P",
        "mac": "Alt+Shift+P"
      }
    }
  },
  "background": {
    "scripts": ["event.js"],
    "persistent": false
  },
  "browser_action": {
    //"default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "content_security_policy": "script-src 'self' https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js https://cdnjs.cloudflare.com/ajax/libs/bootstrap-tagsinput/0.8.0/bootstrap-tagsinput.min.js; object-src 'self'",
  "permissions": ["http://*/*", "https://*/*","notifications", "webNavigation", "storage", "tabs","activeTab", "background" ]
}
Kevin Ku
  • 53
  • 7
  • The background script runs in a separate background page which is not related to web pages. This hidden page runs on messaging and other API events which is why you see the notification randomly. Use an API listener like chrome.tabs.onUpdated. Look for examples in the documenation and google. – wOxxOm Oct 24 '19 at 10:52
  • That actually helped, I can see in the console that it actually loads on every page but it still won't create a notification. – Kevin Ku Oct 24 '19 at 11:02
  • Not sure what you meant by that. Anyway I can't help further without seeing manifest.json. – wOxxOm Oct 24 '19 at 11:12
  • I added the manifest file too. – Kevin Ku Oct 24 '19 at 11:20
  • The manifest looks fine. Now you need to use a proper listener in the background script instead of `load`. Like chrome.tabs.onUpdated or chrome.webNavigation API, see the documentation. Also open the [background page console](/a/10258029) and inspect the console for errors. – wOxxOm Oct 24 '19 at 11:57

0 Answers0