【问题标题】:Copy form field data from one form to another on a separate website?将表单字段数据从一个表单复制到另一个网站上的另一个?
【发布时间】:2015-01-12 17:22:11
【问题描述】:

所以我必须在某个网站上将信息输入到一个表单中,我们称之为websiteA。我必须在另一个网站上为该州输入相同的信息,我们将其称为 websiteB

我正在寻找一种方法来简化流程并将websiteA 中的信息自动放入websiteB 上的匹配表单字段中。这只是为了在我自己的计算机上本地使用。

我是该流程的新手,并且一直在阅读有关执行此操作的不同方法的信息。我目前正在尝试在 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");
}

这是我目前所拥有的,但它根本无法正常工作。我试图寻找更好的方法来完成这项工作,但没有任何运气。如果有人能帮助我,将不胜感激。

【问题讨论】:

    标签: javascript cross-domain tampermonkey


    【解决方案1】:

    一些事情:

    1. 这不是如何使用GM_setValue()。见the documentation for GM_setValue
    2. 那些@match 指令最后需要/*。 (除非您真的想要确切的主页,而不需要其他主页。)
    3. 如果任一页面使用 javascript 技术,请使用 waitForKeyElements(或类似的)来处理时间问题。
    4. 为避免出错,最好让 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");
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多