2012-01-05 194 views
2

我一直在研究Safari擴展並遇到困難。我無法弄清楚如何從全局向注入發送多行數據。 我一直在這個網站和其他人尋找一段時間,只發現了點點滴滴,但當合並失敗。Safari擴展消息傳遞

繼承人什麼,我需要走出全球
safari.extension.secureSettings.username;
safari.extension.secureSettings.password;
我已經試過puting成全局變量,但注射沒有看到這些。

注入代碼

document.getElementById('local_login').style.display=''; 
document.getElementById('local_login_link').style.display = 'none'; 
document.loginForm.username.value = /*Safari Secure Settings Username*/ 
document.loginForm.password.value = /*Safari Secure Settings Password*/ 
document.getElementById('localsubmit').click(); 

我試圖從蘋果文檔的代碼,但它不會運行任何代碼注入的。

編輯 這是我到目前爲止。我只是不確定它爲什麼沒有收到或發送。

Global.html

function sendCred() { 
    myUsername = safari.extension.secureSettings.username; 
    myPassword = safari.extension.secureSettings.password; 
    var arrayNSA = [myUsername, myPassword]; 
    safari.self.tab.dispatchMessage("nsaArray", arrayNSA); 
} 

safari.application.addEventListener("messageFromNSA", sendCred, false); 

Inject.js

function showForm() { 
    document.getElementById('local_login').style.display=''; 
    document.getElementById('local_login_link').style.display = 'none'; 
    document.loginForm.username.value = myNSAusername; 
    document.loginForm.password.value = myNSApassword; 
    document.getElementById('localsubmit').click(); 
} 

function recieveCred(msgEvent) { 
    var nsaMessageName = msgEvent.name; 
    var nsaMessageData = msgEvent.message; 
    if (nsaMessageName === "nsaArray") { 
     var myNSAusername = nsaMessageData[0]; 
     var myNSApassword = nsaMessageData[1]; 
     showForm(); 
    } 
} 

function disbatchData() { 
    var nflksnfll = "Give me my data"; 
} 

safari.self.addEventListener("nsaArray", recieveCred, false); 
safari.self.tab.dispatchMessage("msgFromNSA", disbatchData); 
+1

您需要從全局頁發送消息給注入腳本。如果您仍然需要幫助,請閱讀以下頁面,然後在此發佈評論。 http://developer.apple.com/library/safari/ipad/#documentation/Tools/Conceptual/SafariExtensionGuide/MessagesandProxies/MessagesandProxies.html – canisbos 2012-01-06 02:03:53

+0

我仍然無法得到它。我可以添加我迄今爲止所寫的內容。我只是不確定發送或接收是否是問題,看起來好像兩者都不起作用。 – WatsonN 2012-01-06 21:30:15

+0

對於最近的回覆,我很抱歉。當你評論時,我認爲Stack Overflow會給我發電子郵件,但事實並非如此。我在下面添加了一個答案。 – canisbos 2012-01-10 18:14:46

回答

7

有你的腳本的幾個問題。

在全局腳本:

  1. 您需要在「消息」事件註冊事件偵聽器; 「messageFromNSA」不是有效的事件類型。此外,您需要使用safari.application.addEventListener而不是safari.self.addEventListener
  2. 在函數sendCred()中,將safari.self.tab.dispatchMessage更改爲event.target.page.dispatchMessage,因爲您要將消息分派到發送請求的頁面。 event.target是發送消息的選項卡; page是該選項卡中文檔的代理。 safari.self.tab只適用於注入腳本。

在你注入腳本:

  1. 再次,事件監聽器需要在 「消息」,而不是 「nsaArray」 註冊。
  2. 在函數recieveCred(msgEvent)中,您已將myNSAusernamemyNSApassword定義爲局部變量,因此函數showForm()無法看到它們。刪除關鍵字var以使它們成爲全局變量。

這裏是修改後的全局和注入腳本,應該可以工作,並附加註釋。

全局腳本:

function handleMessage(event) { 
    // use a switch statement and a more generic function name 
    // so you can use it to handle other messages in the future 
    switch (event.name) { 
     case 'sendNsaArray': { 
      // I changed the name of the message sent from the 
      // injected script to 'sendNsaArray' 
      var myUsername = safari.extension.secureSettings.username; 
      var myPassword = safari.extension.secureSettings.password; 
      var arrayNSA = [myUsername, myPassword]; 
      event.target.page.dispatchMessage('nsaArray', arrayNSA); 
      break; 
     } 
    } 
} 

safari.application.addEventListener("message", handleMessage, false); 

注入腳本:

function showForm(username, password) { 
    // why not pass the values to this function instead of using globals 
    document.getElementById('local_login').style.display = ''; 
    document.getElementById('local_login_link').style.display = 'none'; 
    document.loginForm.username.value = username; 
    document.loginForm.password.value = password; 
    document.getElementById('localsubmit').click(); 
} 

function handleMessage(event) { 
    // again using a more generic function name 
    switch (event.name) { 
     case 'nsaArray': { 
      showForm(event.message[0], event.message[1]); 
      // passing the username and password to showForm() 
      // instead of storing them in global variables 
      break; 
     } 
    } 
} 

if (window === window.top) { 
    // this conditional prevents the injected script from 
    // working inside iframes 
    safari.self.addEventListener('message', handleMessage, false); 
    safari.self.tab.dispatchMessage('sendNsaArray'); 
    // not necessary to send any data with this message 
} 
+0

非常感謝您的支持!唯一的是我試圖運行它,但它失敗了。我告訴它每次運行時都要登錄到控制檯,我能得到的唯一日誌就是說窗口是最高的,我沒有看到全局或消息處理程序中的任何內容。 – WatsonN 2012-01-11 05:53:01

+0

如果您將我的.safariextz文件發送給我,我會很高興看一看,看看我能否找到問題所在。 [email protected] – canisbos 2012-01-11 07:57:14

+0

我很抱歉,我的印象是,你是在通知電子郵件。我發送了它,非常感謝! – WatsonN 2012-01-11 19:40:12

1

您可以訪問全球頁面

const myGlobal = safari.extension.globalPage.contentWindow; 
alert (myGlobal.my_variable); 
+0

我嘗試過使用它,但它會停止整個腳本而不做任何事情。只要我注意到這條線,它再次運行。 – WatsonN 2012-01-07 05:04:32