2016-07-30 13 views
0

コンテキストタイプが"editable"のコンテキストメニュー項目を作成すると、<input>タグでコンテキストメニューを開いたときに表示されます。入力タイプ「日付」は編集可能ではありません

chrome.contextMenus.create({ 
    ... 
    contexts: ["editable"] 
    ... 
}); 

<input>は通常のテキストボックスであるとき、それはそれは、このようなdatetimeなどのタイプを持っている場合しかし、メニューはもはや現れ、働かない:

<input type="date"> <!-- Nope --> 

がそれであり、ある理由(異なる種類の<input>を含む編集可能な要素にのみ表示されるように)

答えて

2

source codeによれば、非読み取り専用の無効でないテキスト入力要素は"editable"です。

解決方法:コンテキストメニュー項目のcontextを動的に変更してください。

は、集束要素をチェックして、コンテキストメニュー項目のcontext"page"かに応じて"editable"からupdateにバックグラウンドスクリプトにメッセージを送信し、マウス/キーボードイベントハンドラを持つすべての/指定されたURLのコンテンツスクリプトを宣言します。

  • manifest.jsonを:

    "permissions": ["contextMenus"], 
    "content_scripts": [{ 
        "matches": ["<all_urls>"], 
        "js": ["content.js"], 
        "run_at": "document_start" 
    }], 
    "background": { 
        "scripts": ["background.js"], 
        "persistent": false 
    }, 
    
  • content.js:

    window.addEventListener('mousedown', toggleContextMenu); 
    window.addEventListener('keydown', toggleContextMenu); 
    
    function toggleContextMenu(e) { 
    
        if (e.button == 2 || 
         // "Apps Menu" key 
         e.keyCode == 93 && !e.altKey && !e.shiftKey && !e.ctrlKey && !e.metaKey) { 
    
         var tag = e.target.localName; 
         var type = e.target.type; 
    
         var forceMenu = 
          tag == 'input' && /^(date|time|month|color)$/.test(type) || 
          tag == 'select'; 
    
         chrome.runtime.sendMessage(forceMenu ? 'page' : 'editable'); 
        } 
    } 
    
  • background.js:

    chrome.contextMenus.create({ 
        id: 'hello', 
        title: 'Hello', 
        contexts: ['editable'] 
    }, function() { 
        var ignoreErrors = chrome.runtime.lastError; 
    }); 
    
    chrome.contextMenus.onClicked.addListener(function(info, tab) { 
        if (info.menuItemId == 'hello') { 
         console.log('Hello'); 
        } 
    }); 
    
    chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) { 
        chrome.contextMenus.update('hello', {contexts: [msg]}); 
    }); 
    
+0

I assu私はコンテンツスクリプトを使用することが唯一の解決策かもしれませんが、私は "activeTab"パーミッションを宣言していますので、これはおそらく適切ではありません。とにかく答えてくれてありがとう。 –

+0

残念ながら、他の解決策の余地はありません。 https://bugs.chromium.org – wOxxOm

+0

で機能リクエストを送信するか、編集可能なページとページの両方のコンテキストで表示するだけです。視覚的にはそれほどエレガントではありませんが、 '' activeTab "' -onlyアクセス権セットだけは価値があります。 – wOxxOm

関連する問題