最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why doesn't my Tampermonkey script run on some particular websites - Stack Overflow

programmeradmin2浏览0评论

I made some scripts to automatically insert the username and password and press the Log in button for me when I visit websites. On some websites, it is as easy as

document.getElementById('username').value='myname'
document.getElementById('loginButton').click()

And when I visit the website, all those actions will be done instantly. However, on some websites, such as /, the script doesn't run at all. When I paste the script into the console, it works fine; however, it doesn't automatically run when the page loads. Can someone suggest a way to make the script work? This is my script:

// ==UserScript==
// @name         payoneer
// @namespace    /
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        /*
// @icon         ;domain=payoneer
// @grant        none
// @run-at document-start
// ==/UserScript==

(function() {
     window.onload = function (){
         function replaceValue(selector, value) {
  const el = document.querySelector(selector);
  if (el) {
    el.focus();
    el.select();
    if (!document.execCommand('insertText', false, value)) {
      // Fallback for Firefox: just replace the value
      el.value = 'new text';
    }
    el.dispatchEvent(new Event('change', {bubbles: true})); // usually not needed
  }
  return el;
}
replaceValue('#username',"[email protected]");
    document.getElementsByClassName('text-box__input')[1].setAttribute("id","passworde");
    replaceValue('#passworde',"MyPASsword123!")


     }
    'use strict';

    // Your code here...
})();

I made some scripts to automatically insert the username and password and press the Log in button for me when I visit websites. On some websites, it is as easy as

document.getElementById('username').value='myname'
document.getElementById('loginButton').click()

And when I visit the website, all those actions will be done instantly. However, on some websites, such as https://login.payoneer./, the script doesn't run at all. When I paste the script into the console, it works fine; however, it doesn't automatically run when the page loads. Can someone suggest a way to make the script work? This is my script:

// ==UserScript==
// @name         payoneer
// @namespace    http://tampermonkey/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://login.payoneer./*
// @icon         https://www.google./s2/favicons?sz=64&domain=payoneer.
// @grant        none
// @run-at document-start
// ==/UserScript==

(function() {
     window.onload = function (){
         function replaceValue(selector, value) {
  const el = document.querySelector(selector);
  if (el) {
    el.focus();
    el.select();
    if (!document.execCommand('insertText', false, value)) {
      // Fallback for Firefox: just replace the value
      el.value = 'new text';
    }
    el.dispatchEvent(new Event('change', {bubbles: true})); // usually not needed
  }
  return el;
}
replaceValue('#username',"[email protected]");
    document.getElementsByClassName('text-box__input')[1].setAttribute("id","passworde");
    replaceValue('#passworde',"MyPASsword123!")


     }
    'use strict';

    // Your code here...
})();
Share Improve this question asked Jul 5, 2022 at 18:47 Randy VURandy VU 1092 gold badges2 silver badges10 bronze badges 3
  • Related: How to change the value of an input field and Enter data into a custom-handled input field? – ggorlen Commented Jul 5, 2022 at 18:59
  • Auto-writing value works fine for me. This website just deletes everything you write into the input field after a few milliseconds. – Konrad Commented Jul 5, 2022 at 19:05
  • I managed to input the text into the input field and keep it there. What I can't do is make the script run by itself when the page loads. – Randy VU Commented Jul 5, 2022 at 21:52
Add a ment  | 

2 Answers 2

Reset to default 2

I was having a similar issue where the script was not running at all. The issue was that the Developer Mode was not enabled in Google Chrome.

This is mentioned in the Tampermonkey FAQ: https://www.tampermonkey/faq.php#Q407

In my experience it's usually a problem with timing (waiting for the page to pletely load before running the script). I'd take a closer look at:

  • if your script is running, but ing across an error before being able to run entirely. If so, I'd take a look at solutions that other munity members have e up with.
  • if your script really doesn't run at all, maybe check your tampermonkey or browser settings

at the very leaast, by debugging the above, you can e up with a better idea on the source of the problem

发布评论

评论列表(0)

  1. 暂无评论