Discussions » Creation Requests

Переход по ссылке в Tampermonkey

Всем привет с берегов Невы!
Какой скрипт добавить в Tampermonkey, который после открытия html-страницы, найдёт на ней нужную ссылку и перейдёт по ней?

NotYouMod
§
Posted: 25-12-2022

Hello, @Евгений Нестеров!

It's easier than you think!

Here is the code:

(function() {
  const URL = 'https://example.com/'

  try {
    let anchorElement = document.querySelector(`[href="${URL}"]`)

    if(anchorElement) {
      anchorElement.click()
    }
  } catch(_) {}
})()

Replace value of URL, with that URL that you need to find. (Make sure that quotes are still used!)


Translated by Google Translate

Здравствуйте, @Евгений Нестеров!

Это проще, чем вы думаете!

Вот код:

(function() {
  const URL = 'https://example.com/'

  try {
    let anchorElement = document.querySelector(`[href="${URL}"]`)

    if(anchorElement) {
      anchorElement.click()
    }
  } catch(_) {}
})()

Замените значение URL на тот URL, который вам нужно найти. (Убедитесь, что кавычки все еще используются!)

Thanks for the response, but it doesn't work. What could be the reason?

(function() {
const URL = 'https://cash-click.ru/?serf=100'

try {
let anchorElement = document.querySelector(`[href="${URL}"]`)

if(anchorElement) {
anchorElement.click()
}
} catch(_) {}
})();

P.S. At the very end of the script, I added a missing semicolon.

NotYouMod
§
Posted: 25-12-2022
Edited: 25-12-2022

@Евгений Нестеров, not all anchors elements have URI directly written on element, some of them can be generated dynamically. There is updated code, that may help you:

(function() {
  const URL = 'https://cash-click.ru/?serf=100'

  let _url = URL.replace(location.origin, '').replace(/^https?:\/\//, '')

  try {
    let anchorElements = Array.from(document.querySelectorAll('a'))

    for (let i = 0; i < anchorElements.length; i++) {
      let element = anchorElements[i]

      if(element.href.indexOf(_url) > -1) {
        return element.click()
      }
    }
  } catch(_) {}
})()

P.S. JavaScript doesn't require semicolons.

Hello! Thanks for the updated code, but it doesn't work either. I bring the script completely from Tampermonkey, because maybe I need to change something in its upper part.

// ==UserScript==
// @name         r=3 to 1x100 http
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        http://*/?r=3
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// ==/UserScript==

(function() {
  const URL = 'https://cash-click.ru/?serf=100'

  let _url = URL.replace(location.origin, '').replace(/^https?:\/\//, '')

  try {
    let anchorElements = Array.from(document.querySelectorAll('a'))

    for (let i = 0; i < anchorElements.length; i++) {
      let element = anchorElements[i]

      if(element.href.indexOf(_url) > -1) {
        return element.click()
      }
    }
  } catch(_) {}
})()

P.S. If js does not require semicolons, then it is not clear why they are put in Tampermonkey scripts.

Thank you for your help. I was prompted with a short script on another forum.

Post reply

Sign in to post a reply.