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

发布于 2025-01-07 12:54:41 字数 1721 浏览 1 评论 0原文

当我点击扩展图标或按键“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选项卡url匹配,

http://club.pchome.net/thread_1_15_6865723__.html

它将打开一个不活动的新选项卡(在后台),新的url如下所示。 6865723这个数字在不同的页面是不同的。其他的都是固定的。

http://wap.kdslife.com/t/1/15/6865723/?u=0&sc=235&rnd=0359251496

原始脚本来自 (https://github.com/guiwuu/guiwuu/tree/主/chrome/linkmv)

How can I make this function run when I click on the extension icon or key "Shift+Ctrl+K"?

$(document).bind('keydown', 'shift+ctrl+k', function replaceWeiboLink()
{

This content.js seems does not work.

$(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);
}); 

Only the focus was on the new page, not opening tab as background. This string is not working, any idea?

window.focus(); //send back the focus to the current window

my manifest.json is

{
  "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
  }]
}

This extension to do following, if the Chrome tab url match

http://club.pchome.net/thread_1_15_6865723__.html

it will open a new tab not active(in background) with the new url like following. The number 6865723 is different in different pages. Others are fixed.

http://wap.kdslife.com/t/1/15/6865723/?u=0&sc=235&rnd=0359251496

Original script come from (https://github.com/guiwuu/guiwuu/tree/master/chrome/linkmv)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨光如昨 2025-01-14 12:54:41

尝试 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

注意:这可能会被浏览器首选项设置覆盖。

Try window.open,

instead of:

document.location.href=document.location.href.replace(club.pchome.net/thread_1_15_,"wap.kdslife.com/t/1/15/"); 

try this:

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

NOTE: this might be overridden by a browser preference settings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文