当我点击扩展程序图标或按键“Shift+Ctrl+K”时,如何使该功能运行?
$(document).bind('keydown', 'shift+ctrl+k', function replaceWeiboLink()
{
这个 content.js 似乎不起作用。
$(document).bind('keydown', 'shift+ctrl+k', function replaceWeiboLink()
{
var url = document.location.href;
if (url != undefined && url.indexOf("club.pchome.net/thread_1_15_")!=-1)
{
url = url.replace("club.pchome.net/thread_1_15_","wap.kdslife.com/t/1/15/");
url = url.replace("__.html","/?u=0&sc=235&rnd=0359251496");
window.open(url);
window.focus(); //send back the focus to the current window
}
window.setTimeout(replaceWeiboLink, 100);
});
仅焦点位于新页面上,而不是打开选项卡作为背景。该字符串不起作用,有什么想法吗?
window.focus(); //send back the focus to the current window
我的manifest.json是
{
"name": "linkmv",
"version": "0.1",
"description": "linkmv",
"icons": {
"48": "icon.png"
},
"browser_action": {
"default_icon": "icon.png"
},
"permissions": [
"tabs",
"http://*/*"
],
"content_scripts": [ {
"matches": ["http://*.club.pchome.net/*"],
"js": ["content.js"],
"all_frames": true
}]
}
如果 Chrome 标签页网址匹配,此扩展程序将执行以下操作
http://club.pchome.net/thread_1_15_6865723__.html
它将打开一个未激活的新选项卡(在后台),其新网址如下所示。 6865723这个数字在不同的页面是不同的。其他已修复。
http://wap.kdslife.com/t/1/15/6865723/?u=0&sc=235&rnd=0359251496
原始脚本来自( https://github.com/guiwuu/guiwuu/tree/master/chrome/linkmv )
最佳答案
尝试window.open,
而不是:
document.location.href=document.location.href.replace(club.pchome.net/thread_1_15_,"wap.kdslife.com/t/1/15/");
试试这个:
var url =document.location.href.replace(club.pchome.net/thread_1_15_,"wap.kdslife.com/t/1/15/";
window.open(url);
window.focus(); //send back the focus to the current window
注意:这可能会被浏览器首选项设置覆盖。
关于javascript - 如何在 Chrome 中使用现有链接自动创建新选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9390708/