将表单字段数据从一个表单复制到另一个网站上的另一个表单?
因此,我必须将信息输入到某个网站的表单中,我们将其称为网站A 。我必须输入相同的信息在另一个网站的状态,我们将其称为 websiteB 。
So I have to input information into a form on a certain website, we'll call it websiteA. I have to enter the same information on another website for the state, we'll call it websiteB.
我正在寻找一种简化流程的方法,并将来自 websiteA 的信息自动放入 websiteB 。这只是在我自己的计算机上本地使用。
I'm looking for a way to streamline the process and have the information from websiteA automatically placed into the matching form fields on websiteB. This is just for local use on my own computer.
我是新进程,并已阅读关于不同的方法来做到这一点。我目前试图这样做在Tampermonkey,因为这似乎是我做最好的选择做一点研究。
到目前为止,以下是我有。例如,我只是使用一个需要名称的表单字段。元素的ID是 name
。
I'm new to the process and have been reading about different ways to do this. I'm currently trying to do this in Tampermonkey as that seems like my best option from doing a bit of research.
So far, below is what I have. As an example I'm just using one form field that requires a name. The ID of the element is name
.
// ==UserScript==
// @name Form Copier
// @namespace http://localhost
// @match https://websiteA.com
// @match https://websiteB.com
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==
if(document.URL.indexOf("https://websiteA.com") >= 0){
window.open('https://websiteB.com'); //opens the destination website
document.getElementById("name").value = GM_setValue("name");
}
else if(document.URL.indexOf("https://websiteB.com") >= 0){
document.getElementById("name").value = GM_getValue("name");
}
这是目前我所拥有的,它不能正常工作。我试图寻找更好的方法来完成这一切,没有任何运气。
This is currently what I have and it's not working right at all. I've tried to look for better ways to get this done and haven't had any luck. If any of you could help me out it would be greatly appreciated.
有几件事情:
- 这不是怎么使用
GM_setValue()
。请参阅GM_setValue
的文档。 - 这些
@match
指令最后需要一个/ *
。 - 如果任一页面使用JavaScript技术,请使用
waitForKeyElements
- 为了避免失火,最好在使用后, b $ b
- That's not quite how to use
GM_setValue()
. See the documentation forGM_setValue
. - Those
@match
directives need an/*
at the end. (Unless you really want the exact home page, and no others.) - In case either page uses javascript techniques, use
waitForKeyElements
(or similar) to handle timing issues. - To avoid misfires, probably best to have websiteB delete the stored value after it has used it.
将它们放在一起,脚本将是这样:
Putting it all together, the script would be like this:
// ==UserScript==
// @name Form Copier, Cross-domain
// @match https://Sender.com/*
// @match https://Receiver.com/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_deleteValue
// @grant GM_openInTab
// ==/UserScript==
//-- Wait for the element with the ID "name".
waitForKeyElements ("#name", setOrGetName, true);
function setOrGetName (jNode) {
if (location.hostname == "Sender.com") {
//-- Store the `name` value:
GM_setValue ("nameVal", jNode.val() );
GM_openInTab ("https://Receiver.com/");
}
else if (location.hostname == "Receiver.com") {
var nameVal = GM_getValue ("nameVal");
if (nameVal) {
//-- Set the form value:
jNode.val (nameVal);
//-- Reset storage:
GM_deleteValue ("nameVal");
}
}
}