1

右クリック メニューを作成するコードは次のとおりです。

chrome.contextMenus.create({
    title: "copy '%s' to clipboard", 
    contexts: ["selection"], 
    onclick: function(info) { 
        wordObject[wordObject.length] = {
            word: info.selectionText,
            definition: " 'add definition' "
        };
        runArray();
        chrome.storage.sync.set({"myValue": wordObject});
    }
});

問題は、ページを更新するたびに、メニューの下に別の選択肢が作成されることです。

編集

ちょっと修正しましたが、クリックするたびにページを更新する必要があります。これを回避する方法はありますか?

chrome.contextMenus.create({
    title: "copy '%s' to clipboard", 
    contexts: ["selection"], 
    onclick: function(info) { 
        wordObject[wordObject.length] = {
            word: info.selectionText,
            definition: " 'add definition' "
        };
        runArray();
        chrome.storage.sync.set({"myValue": wordObject});
        chrome.contextMenus.removeAll();
        location.reload(); // Refreshes page! Probably not good
    }
});
4

2 に答える 2

0

@apsillers の回答を拡張するために、非永続的な拡張機能 (ブラウザー アクションの代わりにページ アクションを使用する拡張機能) のインストール時にコンテキスト メニュー項目を作成する例を、この回答から引用します

chrome.runtime.onInstalled.addListener(function() {
    chrome.contextMenus.create({
        title: 'My menu',
        id: 'menu1', // you'll use this in the handler function to identify this context menu item
        contexts: ['all'],
    });
});

chrome.contextMenus.onClicked.addListener(function(info, tab) {
    if (info.menuItemId === "menu1") { // here's where you'll need the ID
        // do something
    }
});
于 2016-12-05T20:30:18.693 に答える