在单独的网站上将表单字段数据从一个表单复制到另一个表单?
Copy form field data from one form to another on a separate website?
所以我必须将信息输入到某个网站的表格中,我们将其称为 websiteA。我必须在该州的另一个网站上输入相同的信息,我们将其称为 websiteB。
我正在寻找一种方法来简化流程,并将 websiteA 中的信息自动放入 websiteB[=25= 上匹配的表单字段中].这仅供在我自己的计算机上本地使用。
我是这个过程的新手,一直在阅读有关执行此操作的不同方法。我目前正尝试在 Tampermonkey 中执行此操作,因为这似乎是我进行一些研究后的最佳选择。
到目前为止,下面是我所拥有的。作为示例,我只使用一个需要名称的表单域。元素的 ID 是 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");
}
这是我目前拥有的,但根本无法正常工作。我试图寻找更好的方法来完成这项工作,但没有任何运气。如果你们中的任何人能帮助我,我们将不胜感激。
几件事:
- 这不是如何使用
GM_setValue()
。参见 the documentation for GM_setValue
。
- 那些
@match
指令最后需要一个 /*
。 (除非你真的想要确切的主页,而不是其他。)
- 如果任一页面使用 javascript 技术,请使用
waitForKeyElements
(或类似方法)来处理计时问题。
- 为了避免失败,最好让 websiteB 在使用后删除存储的值。
将它们放在一起,脚本将是这样的:
// ==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");
}
}
}
所以我必须将信息输入到某个网站的表格中,我们将其称为 websiteA。我必须在该州的另一个网站上输入相同的信息,我们将其称为 websiteB。
我正在寻找一种方法来简化流程,并将 websiteA 中的信息自动放入 websiteB[=25= 上匹配的表单字段中].这仅供在我自己的计算机上本地使用。
我是这个过程的新手,一直在阅读有关执行此操作的不同方法。我目前正尝试在 Tampermonkey 中执行此操作,因为这似乎是我进行一些研究后的最佳选择。
到目前为止,下面是我所拥有的。作为示例,我只使用一个需要名称的表单域。元素的 ID 是 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");
}
这是我目前拥有的,但根本无法正常工作。我试图寻找更好的方法来完成这项工作,但没有任何运气。如果你们中的任何人能帮助我,我们将不胜感激。
几件事:
- 这不是如何使用
GM_setValue()
。参见 the documentation forGM_setValue
。 - 那些
@match
指令最后需要一个/*
。 (除非你真的想要确切的主页,而不是其他。) - 如果任一页面使用 javascript 技术,请使用
waitForKeyElements
(或类似方法)来处理计时问题。 - 为了避免失败,最好让 websiteB 在使用后删除存储的值。
将它们放在一起,脚本将是这样的:
// ==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");
}
}
}