2015-01-12 76 views
1

所以我必须把信息输入到某个网站的表格中,我们将其称为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"); 
} 

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

回答

0

有几件事情:

  1. 这倒不怎么使用GM_setValue()。见the documentation for GM_setValue
  2. 这些@match指令最后需要/*。 (除非您确实需要确切的主页,并且没有其他人。)
  3. 如果任一页使用javascript技术,请使用waitForKeyElements(或类似的)来处理计时问题。
  4. 为避免失火,可能最好有网站B在使用它之后删除存储的值。

全部放在一起,脚本会是这样:

// ==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"); 
     } 
    } 
} 
相关问题