最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Chrome Extension: Properly adding a context menu entry with non-persistent background page - Stack Overflow

programmeradmin0浏览0评论

I'm working on a simple link sharing extension (pinboard, readability, delicious, etc), and have a question about how to properly deal with a context menu item. In my non-persistent background page I call chrome.contextMenus.create and chrome.contextMenus.onClicked.addListener to setup/respond to the context menu.

The context menu entry works as expected. But the background page is showing the following error (right after it starts and before I've used the entry) :

contextMenus.create: Cannot create item with duplicate id id_share_link at chrome-extension://.../share.js:52:30 lastError:29 set  

This made me realize that at no point do I remove the item or the listener. Knowing little about javascript and extensions, I'm left wondering if I'm doing everything correctly. I'm assuming this top-level code is going to re-execute every time the background page is invoked. So there are going to be redundant calls to create and addListener (and hence the error I see being logged).

I clearly can't do cleanup in response to suspend, as these calls need to be present to wake up the background script.

Should I be handling things differently?

I'm working on a simple link sharing extension (pinboard, readability, delicious, etc), and have a question about how to properly deal with a context menu item. In my non-persistent background page I call chrome.contextMenus.create and chrome.contextMenus.onClicked.addListener to setup/respond to the context menu.

The context menu entry works as expected. But the background page is showing the following error (right after it starts and before I've used the entry) :

contextMenus.create: Cannot create item with duplicate id id_share_link at chrome-extension://.../share.js:52:30 lastError:29 set  

This made me realize that at no point do I remove the item or the listener. Knowing little about javascript and extensions, I'm left wondering if I'm doing everything correctly. I'm assuming this top-level code is going to re-execute every time the background page is invoked. So there are going to be redundant calls to create and addListener (and hence the error I see being logged).

I clearly can't do cleanup in response to suspend, as these calls need to be present to wake up the background script.

Should I be handling things differently?

Share Improve this question edited Aug 22, 2013 at 17:31 yname 2,25515 silver badges24 bronze badges asked Aug 22, 2013 at 17:06 VictorVictor 431 silver badge3 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

If you want to use an event page, ie a non-persistent background page, as you call it, you should register a context menu via contextMenus.create in the event handler of runtime.onInstalled, as these context menu registrations ”persist“ anyways.

You have to add the listener-function for the contextMenus.onClicked event every time the event page gets reloaded, though, as the registration of your wish to listen on that event persists, while the handler callback itself does not. So generally don't call contextMenus.onClicked.addListener from runtime.onInstalled, but from top level or other code, that is guaranteed to be executed each time the event page loads.[1]

You can handle it one of two ways:

  1. You can add the context menu and the listeners on install using:

    chrome.runtime.onInstalled.addListener(function() {
      /* Add context menu and listener */
    });
    
  2. You can remove the context menu and listener, and then re-add it each time the file is called.

[solution may no longer be the case, read ment]

runtime.onInstalled is not triggered if you disable/enable your extension.
My solution is to always add menu items and swallow errors:

'use strict';

{

  let seqId = 0;

  const createMenuLinkEntry = (title, tab2url) => {

    const id = (++seqId).toString();

    chrome.contextMenus.create({
      id: id,
      title: title,
      contexts: ['browser_action'],
    }, () => {

      const err = chrome.runtime.lastError;
      if(err) {
        console.warn('Context menu error ignored:', err);
      }

    });

  };

  createMenuLinkEntry('Go to Google', (tab) => 'https://google.');
  createMenuLinkEntry('Go to GitHub', (tab) => 'https://github.');

} // namespace

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论