我一直在做一个Safari的扩展,并且撞到了墙上。我不知道如何将多行数据从全局发送到注入。我已经在这个网站和其他网站上搜索了一段时间,只找到了一些零碎的东西,但是当组合失败时。
这是我离开环球公司需要做的
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);发布于 2012-01-10 18:03:47
您的脚本有一些问题。
在您的全局脚本中:
safari.self.addEventListener.sendCred()而不是safari.application.addEventListener函数,将safari.self.tab.dispatchMessage更改为event.target.page.dispatchMessage,因为您希望将消息发送到发送请求的页面。event.target是发送消息的选项卡;page是该选项卡中文档的代理。safari.self.tab只在注入脚本中工作。在注入的脚本中:
同样,事件侦听器需要在"message“上注册,而不是"nsaArray".
recieveCred(msgEvent),您已经将myNSAusername和myNSApassword定义为局部变量,因此函数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
}发布于 2012-01-07 00:36:59
您可以使用
const myGlobal = safari.extension.globalPage.contentWindow;
alert (myGlobal.my_variable);https://stackoverflow.com/questions/8739047
复制相似问题