Javascript Safari扩展消息传递

Javascript Safari扩展消息传递,javascript,safari,safari-extension,Javascript,Safari,Safari Extension,我一直在开发一个Safari扩展,但遇到了麻烦。我不知道如何将多行数据从全局发送到注入。 我已经在这个网站和其他网站上搜索了一段时间,只找到了一些零碎的东西,但当它们结合在一起时就失败了 以下是我需要从Global获得的信息 safari.extension.secureSettings.username safari.extension.secureSettings.password 我尝试将它们放入全局变量中,但注入没有看到这些 注入代码 document.getElementById('l

我一直在开发一个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();
我尝试了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);
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);

您可以使用访问全局页面

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

您的脚本有一些问题

在全局脚本中:

  • 您需要在“message”事件上注册事件侦听器;“messageFromNSA”不是有效的事件类型。另外,您需要使用
    safari.application.addEventListener
    ,而不是
    safari.self.addEventListener
  • 在函数
    sendCred()
    中,将
    safari.self.tab.dispatchMessage
    更改为
    event.target.page.dispatchMessage
    ,因为您希望将消息发送到发送请求的页面<代码>事件。目标
  • 是发送消息的选项卡<代码>页面
    是该选项卡中文档的代理
    safari.self.tab
    仅在注入脚本中工作 在注入的脚本中:

  • 同样,事件侦听器需要在“消息”上注册,而不是在“数组”上注册
  • 在函数
    receivecred(msgEvent)
    中,您已将
    myNSAusername
    mynsacpassword
    定义为局部变量,因此函数
    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
    }
    
    注入脚本:

    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
    }
    

    您需要从全局页面向注入的脚本发送消息。阅读下一页,如果您仍然需要帮助,请在此处发表评论。我还是不明白。我可以补充到目前为止我写的东西。我只是不确定发送或接收是否有问题,看起来两者都不起作用。很抱歉,回复太晚。当你发表评论时,我以为Stack Overflow会给我发一封电子邮件,但事实并非如此。我在下面添加了一个答案。我尝试过使用它,但它会停止整个脚本,并且什么也不做。当我把这行评论出来后,它又开始运行了。非常感谢你,这非常有帮助!唯一的问题是我试图运行它,但失败了。我告诉它每次运行时都要登录到控制台,我能得到的唯一日志是说窗口在顶部,我在inject中没有看到全局或消息处理程序中的任何内容。如果您将.safariextz文件发送给我,我将很高兴看一看,看看是否能找到问题所在。canisbos@gmail.comI抱歉,我的印象和你一样,都是关于一封通知电子邮件。我送的,非常感谢!