2015-01-12 77 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"); 
     } 
    } 
} 
相關問題