Addon problem #2: adding a 'load' handler fails

In this example, there is one bug in the simple background code that causes a “dead object” error. There is a second bug when a ‘load’ handler is uncommented. A full step-by-step replication is provided below.

Manifest:

{
  "manifest_version": 2,
  "name": "Bug1",
  "version": "0.2",
  "icons": {
    "48": "icons/link-48.png"
  },

"permissions": [
	"activeTab",
	"contextMenus",
	"notifications"
],

  "background": {
    "scripts": ["PM_bg.js"]
  }
}

PM_bg.js:

// S: Promise to show string in notification box
function S(Msg,Title)
	{
	return browser.notifications.create({
		"type": "basic",
		"iconUrl": browser.extension.getURL("icons/link-48.png"),
		"title": Title,
		"message": Msg});
	} // S

S('bg running','Direct from bg').then(empty).catch(err);

/*
window.addEventListener('load',function(e)
	{
	S('bg loaded','Direct from bg').then(empty).catch(err);
	}, true);
*/

// Functions

function empty(ignored)
	{
	console.log('doing nothing');
	} // empty

function err(Msg)
	{
	console.log('*** '+Msg);
	} // err
// End

Instructions:

  1. Add Bug2 as a temporary extension.

  2. Click about:debugging#addons > Debug link.

  3. Click trash can in the debugger to clear out any errors by other extensions or Firefox bugs.

  4. Ctrl+F5 to restart my background code.

  5. Note “TypeError: can’t access dead object” error. Find what I’m doing to cause this.

  6. Note that a “bg running” notification box appears. Close the debugger.

  7. Go back in my code and remove the comment brackets around the call to window.addEventListener().

  8. Click about:debugging#addons > Reload link.

  9. Do steps 2-4 again.

  10. Note this time that the notification box doesn’t appear. Find my bug that causes this.