Safari扩展消息 [英] Safari extension messaging

查看:69
本文介绍了Safari扩展消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been working on a Safari extension and have hit a wall. I cannot figure out how to send multiple lines of data from global to the inject. I have been searching for a while on this site and others and only have found bits and pieces, but when combined fail.

继续我需要得到的东西out of Global

safari.extension.secureSettings.username;

safari.extension。 secureSettings.password;

我已经尝试将它们放入全局变量但是注入没有看到它们。

Heres what I need to get out of Global
safari.extension.secureSettings.username;
safari.extension.secureSettings.password;
I've tried puting them into global variables but the inject doesn't see those.

注入代码

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文档中的代码,但它不会运行任何注入代码。

I tried the code from the Apple documentation but it wouldn't run any of the inject code.

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

Edit Here is what I have so far. I'm just not sure why it isn't receiving, or sending.

Global.html

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

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);


推荐答案

您的脚本存在一些问题。

There are a few problems with your scripts.

在您的全局脚本中:


  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. You need to register the event listener on the "message" event; "messageFromNSA" is not a valid event type. Also, you need to use safari.application.addEventListener rather than safari.self.addEventListener.
  2. In function sendCred(), change safari.self.tab.dispatchMessage to event.target.page.dispatchMessage, because you want to dispatch the message to the page that sent the request. event.target is the tab that sent the message; page is the proxy for the document in that tab. safari.self.tab only works inside injected scripts.

注入脚本:


  1. 同样,事件监听器需要在message上注册,而不是nsaArray。

  2. 在函数 recieveCred(msgEvent)中,您已定义 myNSAusername myNSApassword 作为局部变量,因此函数 showForm()无法看到它们。删除关键字 var 以使其成为全局变量。

  1. Again, the event listener needs to be registered on "message", not "nsaArray".
  2. In function recieveCred(msgEvent), you have defined myNSAusername and myNSApassword as local variables, so function showForm() cannot see them. Remove the keyword var to make them global variables.

以下是全球修订版并注入了应该有效的脚本以及附加注释。

Here are revised global and injected scripts that should work, with additional comments.

全局脚本:

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
}

这篇关于Safari扩展消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆