How do you make Greasemonkey Click a link that has specified text?
Asked Answered
C

2

11

Alright basically i want to click a link that changes but always has the same text name. Heres an example of what the code might be

<a href="unlock.php?confirm=MD5hashere">Click Here</a>
Collagen answered 18/2, 2011 at 1:20 Comment(0)
T
14

Here is a starter script that does that. Note that it uses jQuery and assumes you are running Firefox or using Tampermonkey, if you are on Chrome.

// ==UserScript==
// @name     _YOUR_SCRIPT_NAME
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/

//--- Note that the contains() text is case-sensitive.
var TargetLink = $("a:contains('Click Here')")

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


See also:

Tanga answered 18/2, 2011 at 1:52 Comment(0)
R
1

Vanilla JS version using XPath:

document.evaluate("//a[text()='Click Here']", document).iterateNext().click()
Repress answered 21/1, 2022 at 12:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.