【发布时间】:2019-03-19 04:20:32
【问题描述】:
最近有报道说我的应用程序的上下文菜单消失了。如果您删除该应用程序并重新安装它,它就可以工作。但消失又发生了。
我发现了一个错误。我不确定该错误是否会导致上下文菜单消失。但我想解决这个问题,因为我发现的只有这个。
此应用显示您在页面中选择的文本。当您在普通页面中选择文本并单击浏览器操作按钮时,它可以正常工作。但是如果你在 Google Docs 上尝试,你会得到错误“Unchecked runtime.lastError: Could not建立连接。接收端不存在”。
恐怕我不知道该怎么办。我可能有两个问题。如果您能给我一些建议,将会很有帮助。
[manifest.js]
{
"manifest_version": 2,
"name": "Test Chrome Extension",
"short_name": "Test",
"version": "1.0",
"description": "This is a test.",
"icons": {
"128": "128.png"
},
"content_scripts": [{
"matches": ["<all_urls>"],
"js": ["googleDocsUtil.js", "content_scripts.js"]
}],
"background": {
"scripts": ["background.js"],
"persistent": true
},
"browser_action": {
"default_icon": {
"48": "48.png"
},
"default_title": "Test Chrome Extension"
},
"permissions": [
"contextMenus",
"tabs",
"background",
"http://*/*",
"https://*/*"
]
}
[背景.js]
chrome.contextMenus.create({
type: 'normal',
id: 'testchromeextension',
title: 'Test Chrome Extension',
contexts:['selection']
});
chrome.contextMenus.onClicked.addListener(function(info,tab){
if( info.menuItemId == 'testchromeextension' ){
var selectedText = info.selectionText.replace(/ /g, "\n");
doSomething(selectedText);
}
});
chrome.browserAction.onClicked.addListener( function(tab) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {method: "getSelection"}, function(response) {
doSomething(response.data);
});
});
});
function doSomething(selectedText) {
console.log(selectedText);
}
[content_scripts.js]
chrome.runtime.onMessage.addListener( function(request, sender, sendResponse) {
if (request.method == "getSelection") {
var post_val = window.getSelection().toString();
if ( !post_val ) {
var googleDocument = googleDocsUtil.getGoogleDocument();
post_val = googleDocument.selectedText;
}
sendResponse({data: post_val});
}
});
【问题讨论】:
-
某些公司网络中可能会保护类似 google 文档的声音 - 查看 chrome://policy
-
谢谢!我会的。
-
我检查了 chrome://policy,但没有任何错误迹象..
-
对不起,我应该更清楚地向你解释。没有受限制的 URL。我只看到 Chrome 政策“MaxInvalidationFetchDelay”。 capture这是用日语写的。
-
另一种猜测:离线文档扩展已安装,并以某种方式干扰。