如何通过Tampermonkey在“新选项卡"中打开单击的链接? [英] Way to Open Clicked Link in New Tab via Tampermonkey?

查看:126
本文介绍了如何通过Tampermonkey在“新选项卡"中打开单击的链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个似乎很简单的问题.我正在尝试使用以下代码自动打开页面上的特定链接:

So I have what seems to be a simple problem. I'm trying to automatically open a specific link on a page using the following code:

// ==UserScript==
// @name     AutoClicker
// @include  https://example.com/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @grant    GM_addStyle
// ==/UserScript==

var TargetLink = $("a:contains('cars')")

if (TargetLink.length)
    window.location.href = TargetLink[0].href

//--- but open it in a new tab

出色地工作.

唯一的问题是我不知道在新标签页中打开所选链接的方法.我尝试了以下代码的迭代,但无济于事:

The only problem is that I don't know a way to open the selected link in a new tab. I've tried iterations of the following code, but to no avail:

var TargetLink = $("a:contains('cars,' '_blank')")

我知道我需要使用 _blank ,但是我不确定确切的位置或是否应该在jQuery中编写.我也尝试过将_blank放置在contains之外,但是我不确定如何在jQuery中编写代码.

I know I'll need to use _blank, but I'm not sure exactly where or whether I should write it in jQuery or not. I've also tried placing _blank outside of contains, but I'm not exactly sure how I'd write the code in jQuery.

我只是希望所选链接在点击后打开新标签.有什么建议或想法吗?

I simply want the selected link to open in a new tab upon being clicked. Any suggestions or thoughts?

推荐答案

问题不明确,问了两个不同的问题.是否要在没有用户交互的情况下打开标签页?

The question is not clear, asking two different things. Do you want the tab to open with no user interaction or not?

如果是, Tampermonkey具有此功能: GM_openInTab() Doc .

If yes, Tampermonkey has a function for that: GM_openInTab()Doc.

所以:

// ==UserScript==
// @name     AutoClicker
// @include  https://example.com/*
// @require  https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @grant    GM_openInTab
// ==/UserScript==

var TargetLink = $("a:contains('cars')");

if (TargetLink.length)
    GM_openInTab (TargetLink[0].href);


如果不是,使用 jQuery的 attr()也很容易 Doc .

所以:

// ==UserScript==
// @name     NOT an AutoClicker, per question text
// @include  https://example.com/*
// @require  https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @grant    GM_addStyle
// ==/UserScript==

var TargetLink = $("a:contains('cars')");

if (TargetLink.length)
    TargetLink.attr ('target', '_blank');


对于javascript驱动的页面(也适用于静态页面):


For a javascript-driven page (also works on static pages):

// ==UserScript==
// @name     NOT an AutoClicker, per question text
// @match    *://YOUR_SERVER.COM/YOUR_PATH/*
// @require  https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// @grant    GM.getValue
// ==/UserScript==
//- The @grant directives are needed to restore the proper sandbox.

waitForKeyElements ("a:contains('cars')", blankifyLink);

function blankifyLink (jNode) {
    jNode.attr ('target', '_blank');
}

这篇关于如何通过Tampermonkey在“新选项卡"中打开单击的链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆