我想阻止中间点击打开链接,但只能选择 - 在Chrome扩展程序的设置中,可以设置为防止与否。
当有人在Chrome扩展程序的设置中更改该选项(防止或不防止)时,它必须立即影响网站而不重新加载当前页面(ctrl + r)。要做到这一点,我把" chrome.runtime.sendMessage ....."进入" $(文件).on("点击...."检查EnablePrevent的值(真或不)(来自本地存储)"。所以检查每次点击的价值(我不需要重新加载页面ctrl + r来检查价值)。
这是我的代码。我不知道如何从块" chrome.runtime.sendMessage ....."获得价值(真实)。我不能把块" if(e.button === 1 ..." into" chrome.runtime.sendMessage ....."因为阻止不会工作......(它需要" $(文件).on("点击......")
[DataContract]
public class Foo
{
[DataMember(Name = "id")]
public string Id {get;set;}
[DataMember(Name = "name")]
public string Name {get;set;}
[DataMember(Name = "email")]
public string Email {get;set;}
}
对于Firefox的附加组件,这不是问题:
$(document).on("click", function(e) {
chrome.runtime.sendMessage({method: "getLocalStorage", key: "EnablePrevent"}, function(response) {
if (response.data == "true") {
// ...
}
});
if (e.button === 1 && e.target.closest("a"))
e.preventDefault(); // not optional, always works..
}
});
答案 0 :(得分:1)
在click事件中等待messageResponse是错误的逻辑
您应该已经在内容脚本中准备了决定
并且要使用Chrome storage API
<强>清单强>
"permissions": ["storage",.....]
选项页面
//on user changing option
var EnablePrevent= ....//true or false
chrome.storage.local.set({'disableMMC': EnablePrevent});
//to update it in real time, reroute it through background page
chrome.runtime.sendMessage({'user': 'choice', 'value': EnablePrevent});
背景页
chrome.extension.onMessage.addListener(function (message) {
if (message.user=== 'choice') {
var passValue = message.value;
//send new value to all tabs
chrome.tabs.query({}, function(tabs) {
for (var i=0; i<tabs.length; i++) {
chrome.tabs.sendMessage(tabs[i].id, {'newValue': 'fromBG', 'newV':passValue});
}
});
}
});
内容脚本
var disableMiddleButton;
chrome.storage.local.get('disableMMC', function(result) {
disableMiddleButton = result.disableMMC;
});
//to update it in real time, listen for MSG from BG
chrome.extension.onMessage.addListener(function (message) {
if (message.newValue=== 'fromBG') disableMiddleButton = message.newV;
});
//use disableMiddleButton variable in your click logic
$(document).on("click", function(e) {
if (disableMiddleButton && e.button === 1 && e.target.closest("a"))
e.preventDefault();
});
另一种解决方案是在内容脚本中监听存储更改。在这种情况下,您不需要从“选项”页面发送任何消息(或使用通过后台页面重新路由并在内容脚本中使用onMessage侦听器)
内容脚本
var disableMiddleButton;
chrome.storage.local.get('disableMMC', function(result) {
disableMiddleButton = result.disableMMC;
});
//to update it in real time, listen for change in storage
chrome.storage.onChanged.addListener(function(changes, namespace) {
if ('disableMMC' in changes) disableMiddleButton = changes.disableMMC.newValue;
});
//use disableMiddleButton variable in your click logic
$(document).on("click", function(e) {
if (disableMiddleButton && e.button === 1 && e.target.closest("a"))
e.preventDefault();
});
答案 1 :(得分:0)
尝试:
$(document).on("click", function(e) {
var disableMiddleButton = 0;
chrome.runtime.sendMessage({method: "getLocalStorage", key: "EnablePrevent"}, function(response) {
if (response.data == "true") {
disableMiddleButton = 1;
}
});
if (e.button === 1 && e.target.closest("a") && disableMiddleButton == 1 )
e.preventDefault();
}
});