如何通过 TamperMonkey 设置文本字段的值? [英] How do I set the value of a Text Field through TamperMonkey?
问题描述
我是 javascript 和 tampermonkey 的新手,请尽量在解释中记住这一点.
I'm new to javascript and tampermonkey, try to remember that in your explanations please.
如您所见,这非常简单.然而,没有办法让网站记住你的密码,现在我要做的就是制作一个脚本,用你猜对了的用户名和密码填写用户名和密码字段.
As you can see it's pretty simple. However there's no way to make the site remember your password, and right now all I'm trying to do is make a script that will fill in the username and password fields with, you guessed it, my username and password.
根据我在网上找到的一些教程(似乎没有很多关于在 TamperMonkey 中编写脚本的教程),我设法想出了以下代码
Working off of a few tutorials I found online (it doesnt seem like there's a lot of tutorials for writing scripts in TamperMonkey), I managed to come up with the following code
// ==UserScript==
// @name Powerschool Memory
// @namespace http://use.i.E.your.homepage/
// @version 0.1
// @description Makes PowerSchool remember your username and password.
// @match https://powerschool.avon.k12.ct.us/gaurdian/home.html
// @require http://code.jquery.com/jquery-latest.js
// @copyright 2013+, You
// ==/UserScript==
jQuery(function($) {
document.getElementById($('input[type=password]').attr('id')).textContent = 'password_here';
document.getElementById('fieldAccount').innerHTML = 'username_here';
});
如您所见,我尝试了两种设置用户名(设置元素的innerHTML)和密码(设置文本内容)字段内容的方法,但似乎都不起作用.
As you can see I've tried two ways of setting the content of the field for the username(setting the element's innerHTML) and password(setting the textContent), however neither seem to work.
我相当确定脚本正在运行,因为每当我要导航到网站时,我都会转到仪表板并重新启动脚本.
I'm fairly sure the script is running, as whenever I'm about to navigate to the website I go to the dash and restart the script.
我应该提一下,用户名字段的 ID 是通过右键单击文本框,检查元素,然后复制并粘贴接下来的 id 变量获得的
The username field's ID, I should mention, was obtained by right clicking on the text box, inspecting the element, and copying and pasting the next following id variable
你们能帮我看看我的错误在哪里吗,也许更重要的是,我正在做些什么来搞砸?
Could you guys help me see where my error is and, perhaps more importantly, what I'm doing to mess it up?
推荐答案
对于教程和问题,您可以搜索 Greasemonkey 主题.除了少数例外,如果它适用于 Greasemonkey,那么它在设计上也适用于 Tampermonkey.
For tutorials and questions, you can search for Greasemonkey topics. With only a few exceptions, if it works for Greasemonkey, it works for Tampermonkey by design.
关于问题代码,有很多问题:
As for the the question code, there are numerous issues:
- 这不是您为
text
和password
元素设置值的方式.在 jQuery 中,您可以使用
.val()
函数.例如:$("#fieldAccount").val("foo");
- 需要没有
@grant
指令的 jQuery.这将破坏脚本、破坏页面或破坏两者. @match
指令与页面不匹配.gaurdian
不正确,页面使用了guardian
.
您可以通过查看右上角的 Tampermonkey 图标来判断您的@match
指令何时正确.如果该页面上有任何脚本处于活动状态,则会显示一个带有活动脚本数量的红色图标.@match
指令与页面不匹配.匹配指定https
,但该站点不支持 SSL (!!!).您需要匹配不安全的页面,因为这是唯一提供的页面,然后对网站所有者大喊大叫,停止传播您的敏感信息.getElementById
使用不当.$('input[type=password]').attr('id')
是此目标页面的undefined
.在脚本文件中存储用户名和密码!这是本书中最古老的错误和漏洞之一.如果你这样做,你被掠夺只是时间问题.
- That is not how you set values for
text
andpassword
<input>
elements. In jQuery, you set them with the.val()
function. EG:$("#fieldAccount").val("foo");
- Requiring jQuery without a
@grant
directive. This will bust the script, or bust the page or bust both. - The
@match
directive does not match the page.gaurdian
is incorrect, the page usesguardian
.
You can tell when your@match
directives are correct by looking at the Tampermonkey icon in the upper-right. If any scripts are active on that page, it will show a red icon with the number of active scripts. - The
@match
directive does not match the page. The match specifieshttps
, but that site does not support SSL (!!!). You need to match the unsecure page, as that's the only one offered, and then yell at the site owners to stop broadcasting your sensitive information. - Improper use of
getElementById
.$('input[type=password]').attr('id')
isundefined
for this target page. Storing username and password in a script file! This is one the the oldest mistakes, and exploits, in the book. If you do this, it is just a matter of time before you get pwned.
在您得到大致的想法后,将一个敏感信息"框架(如这个)合并到脚本中.
After you get the rough idea working, Incorporate a "sensitive information" framework like this one into the script.
综合起来,这个完整的脚本会起作用:
// ==UserScript==
// @name Powerschool Memory
// @version 0.1
// @description Makes PowerSchool remember your username and password.
// @match http://powerschool.avon.k12.ct.us/guardian/home.html
// @match https://powerschool.avon.k12.ct.us/guardian/home.html
// @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.
*/
//-- If/when SSL becomes available, switch to only using the https pages.
$("#fieldAccount").val ("username_here");
$("#login-inputs input[name='pw']").val ("password_here");
但是,请改用上面第 6 期中链接的框架.
BUT, use the framework linked in issue 6, above, instead.
这篇关于如何通过 TamperMonkey 设置文本字段的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!