Safari扩展消息

Wat*_*onN 2 javascript safari safari-extension

我一直在开发Safari扩展程序并且已经碰壁了.我无法弄清楚如何从全局向注入发送多行数据.我一直在这个网站和其他人搜索一段时间,只发现了点点滴滴,但是当它们合并失败时.

下面我需要摆脱Global
safari.extension.secureSettings.username;
safari.extension.secureSettings.password;
我已经尝试将它们放入全局变量但是注入没有看到那些.

注入代码

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();
Run Code Online (Sandbox Code Playgroud)

我尝试了Apple文档中的代码,但它不会运行任何注入代码.

编辑 这是我到目前为止所拥有的.我只是不确定它为什么没有接收或发送.

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);
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

chu*_*ter 7

您的脚本存在一些问题.

在您的全局脚本中:

  1. 您需要在"message"事件上注册事件监听器; "messageFromNSA"不是有效的事件类型.此外,你需要使用safari.application.addEventListener而不是safari.self.addEventListener.
  2. 在功能中sendCred(),更改safari.self.tab.dispatchMessageevent.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);
Run Code Online (Sandbox Code Playgroud)

注入脚本:

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
}
Run Code Online (Sandbox Code Playgroud)