javascript - 如何在 Chrome 中使用现有链接自动创建新选项卡?

标签 javascript json google-chrome-extension

当我点击扩展程序图标或按键“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/

相关文章:

javascript - 在 Meteor Node.js 中动态加载模块

javascript - 解析 Javascript 函数中的多个对象

python - 如何处理TypeError : Object of type 'bytes' is not JSON serializable?

javascript - document.getElementById 不返回 null,但也没有执行我想要的操作。 JavaScript 控制台没有错误

html - 单击链接时关闭 html5 HTML 通知

javascript - 在单个 channel 中播放振荡

javascript 隐藏的 div 可见 - 找不到元素

java - 映射 @manyToOne 不返回外键 JSON

java - 如何使用 Jackson 排除父类(super class)属性

javascript - Chrome 扩展上下文菜单 : how to append div to page after clicking menu item