Vanilla JavaScript代码可在控制台中运行,但不能从(greasemonkey)脚本中获取 [英] Vanilla JavaScript code works in console but not from a (greasemonkey) script
问题描述
我将Windows10家庭版与最新的Firefox和Greasemonkey一起使用。
I use Windows10 home with latest Firefox and Greasemonkey.
我在 livedns.co.il ,这是以色列的域名注册机构。然后,我登录了。
I created an account in livedns.co.il, which is an Israeli domain register. I then logged in.
登录到个人概述区域后,我试图将自己移至域管理区域,通过此代码:
After logging in to the personal overview area, I tried to move myself to the domains management area, via this code:
window.location.href = "https://domains.livedns.co.il/DomainsList.aspx";
它可以在控制台中运行,但不能通过油脂猴子脚本运行。
It worked in console but not from a greasemonkey script.
我想我可能需要在脚本中使用 setTimeout()
:
I thought I might need setTimeout()
in the script:
setTimeout(()=>{
window.location.href = "https://domains.livedns.co.il/DomainsList.aspx";
}, 1000);
但此代码也仅在控制台中有效。
but this code also worked only in console.
创建帐户并登录后,这是我使用的原始模式:
After creating an account and logging in, this is the original pattern I used:
// ==UserScript==
// @name livednsAutoLogin
// @include https://domains.livedns.co.il/Default.aspx
// ==/UserScript==
console.log(window.location.href); // This fails in Greasemonkey if the code below exists; If I'll delete all code below, it will succeed in Greasemonkey;
document.querySelector("#ctl00_TopLoginBox1_txtUname").value = "myUsername";
document.querySelector("#ctl00_TopLoginBox1_txtPassword").value = "myPassword";
document.querySelector("#ctl00_TopLoginBox1_btnLogin > .FltRt").click();
setTimeout(()=>{
window.location.href = "https://domains.livedns.co.il/DomainsList.aspx";
}, 250);
只需更改用户名和密码,然后进行测试。
Just change username and password, then test.
是什么使普通的JavaScript代码可以在控制台中工作,但不能从(greasemonkey)脚本中工作呢?特别是,为什么更改文档的href位置仅在控制台中起作用,而在Greasemonkey脚本中却不起作用?
What makes a vanilla JavaScript code to work in console but not from a (greasemonkey) script? In particular, why does the changing of href of location of the document works only in console but not in a Greasemonkey script?
我不知道如何在这种情况下进行调试一种情况是因为我在运行脚本时没有在控制台中看到错误。
I don't know how to debug in such a case because I don't see an error in console when I run the script.
推荐答案
我在运行时遇到了逻辑错误来自错误网址的代码(我应该确保我在 https://domains.livedns.co.il/Main.aspx
中能够成功运行该代码。我通过使用if-then条件语句解决了这一问题(使用这种方法,我会发现自己是否受到任何重大的安全威胁)。
I had a logical mistake when running the code from the wrong url (I should have ensured that I'm in https://domains.livedns.co.il/Main.aspx
to successfully run the code. I fixed that by using if-then conditionals (I'll find out if I'm having any significant security threat using this way).
请注意我如何更改 @include
并具有更丰富的URL使用。
Note how I changed the @include
and had richer usage of URLs.
// ==UserScript==
// @name livednsAutoLogin
// @include *livedns.co.il/*
// ==/UserScript==
if ( document.location.href == "https://domains.livedns.co.il/" ) {
document.querySelector("#ctl00_TopLoginBox1_txtUname").value = "myEmail";
document.querySelector("#ctl00_TopLoginBox1_txtPassword").value = "myPassword";
document.querySelector("#ctl00_TopLoginBox1_btnLogin > .FltRt").click();
}
if ( document.location.href == "https://domains.livedns.co.il/Main.aspx" ) {
console.log(window.location.href);
document.location.href = "https://domains.livedns.co.il/DomainsList.aspx"
}
这篇关于Vanilla JavaScript代码可在控制台中运行,但不能从(greasemonkey)脚本中获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!