为什么我的TampermonKey脚本在某些特定网站上运行
我制作了一些脚本来自动插入用户名和密码,并在访问网站时为我按登录按钮。在某些网站上,
document.getElementById('username').value='myname'
document.getElementById('loginButton').click()
当我访问网站时,所有这些操作都将立即完成。但是,在某些网站上,例如 https://login.payoneer.com/ ,脚本domn'' T根本运行。当我将脚本粘贴到控制台中时,它可以正常工作。但是,当页面加载时,它不会自动运行。有人可以提出一种使脚本工作的方法吗?这是我的脚本:
// ==UserScript==
// @name payoneer
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://login.payoneer.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=payoneer.com
// @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.com/, 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.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://login.payoneer.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=payoneer.com
// @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...
})();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了一个类似的问题,脚本根本没有运行。
问题是开发人员模式未在Google Chrome中启用。
这在Tampermonkey常见问题中提到: https:///www.tampermonkey.net/faq.phppt Q407
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.net/faq.php#Q407
根据我的经验,通常是计时的问题(等待页面在运行脚本之前完全加载)。我会仔细研究:
通过调试上述来检查您的Tampermonkey或浏览器设置,您可以在问题来源中提出一个更好的想法
In my experience it's usually a problem with timing (waiting for the page to completely load before running the script). I'd take a closer look at:
at the very leaast, by debugging the above, you can come up with a better idea on the source of the problem