2

I need to open all the search results of each link new tab automatically. I have tried below script which I have found from here. I am unable to achieve the expected outcome. I am absolute new to scripting and help me fix or guide me through achieve the outcome.

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

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

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

Error 1

Error 1

Error 2

Error 2

Martin Brisiak
  • 3,872
  • 12
  • 37
  • 51

3 Answers3

1

I made this script ages ago, tell me if it doesn't work.

// ==UserScript==
// @name         Open All Links in New Tab
// @namespace    http://tampermonkey.net/
// @version      3.5.6
// @author       Firey Chicken
// @match        *
// @match https://*/*
// @match http://*/*
// @match *//*/*
// @grant       GM_addStyle
// @grant        window.close
// @require      http://code.jquery.com/jquery-3.4.1.min.js
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
})();
var $ = window.jQuery;
$(document).on('click', 'a', function(e){
    e.preventDefault();
    var url = $(this).attr('href');
    window.open(url, '_blank');
});
  • So far it is working I manually click on a link it is opening in new tab. I want open all the links automatically without me manually clicking. – Exception776 Nov 14 '20 at 10:47
  • I foresee a problem in that, if you want it to open all links, that would cause you to get many new tabs. Say you get that script working, and go and search something on Google or whatever search engine you use, it would open every result into a new tab. That would cause you to get many new tabs, which I don't see a reason why you would want hundreds of new tabs almost automatically. That would be infuriating and cause much lag. This is the closest you can get while still selecting all the links you want. – FireyChicken Nov 16 '20 at 14:13
  • If my understanding is correct my op code has something like contains place holder. if a link a contains particular keyword it should open automatically. Is it possible? – Exception776 Nov 16 '20 at 21:30
  • Alright, I'll try to add some variables and jQuery plugin to your code if you just insert your code instead of just sending an image, I'll see if I can get it working. – FireyChicken Nov 18 '20 at 15:41
0

You need to declare global value - basically, you add such line below Metadata to prevent Tampermonkey editor (which is using ESLint) from showing errors - Source, example:

/* globals $ */

If you want to open multiple items, you have to iterate over the query. Using targetLink you are pointing to e.g. 6 objects. Usage of loop is almost necessary.

So the final result would be:

// ==UserScript==
// @name        AutoClicker
// @match       https://www.google.com/*
// @description Example
// @version     0.0
// @require     https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @grant       GM_openInTab
// ==/UserScript==

/* globals $ */
(function() {
    'use strict';

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

    for ( let i = 0; i < TargetLink.length; i++ )
    {
        GM_openInTab (TargetLink[i].href);
    }
})();
0

Although the question isn't the most recent one, here's a simple solution without any external dependencies:

// ==UserScript==
// @name            AutoClicker
// @match           https://www.google.com/*
// @grant           GM_openInTab
// ==/UserScript==

for (const a of document.querySelectorAll("a")) {
  if (a.textContent.includes("example")) {
    GM_openInTab(a.href)
  }
}
Tad Wohlrapp
  • 1,848
  • 1
  • 13
  • 18