Safari扩展消息 [英] Safari extension messaging
问题描述
我一直在开发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.
在您的全局脚本中:
- 您需要在上注册事件监听器消息事件; messageFromNSA不是有效的事件类型。此外,您需要使用
safari.application.addEventListener
而不是safari.self.addEventListener
。 - 在函数
sendCred()
中,将safari.self.tab.dispatchMessage
更改为event.target.page.dispatchMessage
,因为您要将邮件分派给发送请求的页面。event.target
是发送消息的标签;page
是该选项卡中文档的代理。safari.self.tab
仅适用于注入的脚本。
- 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 thansafari.self.addEventListener
. - In function
sendCred()
, changesafari.self.tab.dispatchMessage
toevent.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.
注入脚本:
- 同样,事件监听器需要在message上注册,而不是nsaArray。
- 在函数
recieveCred(msgEvent)
中,您已定义myNSAusername
和myNSApassword
作为局部变量,因此函数showForm()
无法看到它们。删除关键字var
以使其成为全局变量。
- Again, the event listener needs to be registered on "message", not "nsaArray".
- In function
recieveCred(msgEvent)
, you have definedmyNSAusername
andmyNSApassword
as local variables, so functionshowForm()
cannot see them. Remove the keywordvar
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屋!