Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript “输入类型”;日期“;isn';不可编辑_Javascript_Google Chrome_Google Chrome Extension - Fatal编程技术网

Javascript “输入类型”;日期“;isn';不可编辑

Javascript “输入类型”;日期“;isn';不可编辑,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,当我创建上下文类型为“可编辑”的上下文菜单项时,当在标记上打开上下文菜单时,它会显示出来 chrome.contextMenus.create({ ... contexts: ["editable"] ... }); 当是一个普通文本框时,它可以工作,但是当它的类型为日期或时间时,菜单不再显示: <input type="date"> <!-- Nope --> 这是为什么?是否有办法使其显示(仅在可编辑元素上,包括不同类型的)?根据

当我创建上下文类型为“可编辑”的上下文菜单项时,当在
标记上打开上下文菜单时,它会显示出来

chrome.contextMenus.create({
    ...
    contexts: ["editable"]
    ...
});
是一个普通文本框时,它可以工作,但是当它的类型为
日期
时间
时,菜单不再显示:

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

这是为什么?是否有办法使其显示(仅在可编辑元素上,包括不同类型的
)?

根据说明,只有非只读、非禁用的文本输入元素是
“可编辑的”

解决方案:动态更改上下文菜单项的
context

使用鼠标/键盘事件处理程序为所有/指定的URL声明内容脚本,该事件处理程序检查聚焦元素,并将消息发送到上下文菜单项的
上下文
“页面”
“可编辑”

  • manifest.json:

    "permissions": ["contextMenus"],
    "content_scripts": [{
        "matches": ["<all_urls>"],
        "js": ["content.js"],
        "run_at": "document_start"
    }],
    "background": {
        "scripts": ["background.js"],
        "persistent": false
    },
    
  • 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]});
    });
    
根据唯一的非只读、非禁用的文本输入元素
“可编辑”

解决方案:动态更改上下文菜单项的
context

使用鼠标/键盘事件处理程序为所有/指定的URL声明内容脚本,该事件处理程序检查聚焦元素,并将消息发送到上下文菜单项的
上下文
“页面”
“可编辑”

  • manifest.json:

    "permissions": ["contextMenus"],
    "content_scripts": [{
        "matches": ["<all_urls>"],
        "js": ["content.js"],
        "run_at": "document_start"
    }],
    "background": {
        "scripts": ["background.js"],
        "persistent": false
    },
    
  • 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]});
    });
    

我假设使用内容脚本可能是唯一的解决方案,但是我只声明了“activeTab”权限,因此这可能不合适。无论如何,谢谢你的回答。很遗憾,没有其他解决方案的余地。您可以在上提交功能请求,也可以仅在可编辑和页面上下文中显示它。它在视觉上没有那么优雅,但是只有权限集值得。我假设使用内容脚本可能是唯一的解决方案,但是我只声明了“activeTab”权限,所以这可能不合适。无论如何,谢谢你的回答。很遗憾,没有其他解决方案的余地。您可以在上提交功能请求,也可以仅在可编辑和页面上下文中显示它。它在视觉上不那么优雅,但是
“activeTab”
-只有权限集值得这么做。