Safari 扩展设置
我正在构建一个 safari 扩展...我需要获取一些设置,但是消息是异步的,因此,如果我查询全局页面的某些设置,它不会立即出现...我该如何解决这个问题??
全局页面(消息由注入的 JS 脚本接收)
<!DOCTYPE HTML>
<html>
<script language="javascript">
function messageHandler(event) {
var activeTab = event.target.browserWindow.activeTab
if (event.name === "getSettings") {
activeTab.page.dispatchMessage("returnedSettings", safari.extension.settings[event.message]);
} else if(event.name === 'openNewTab') {
safari.application.activeBrowserWindow.openTab().url = event.message;
activeTab.page.dispatchMessage("focus");
} else if(event.name === 'openNewVenster') {
safari.application.openBrowserWindow().activeTab.url = event.message;
//activeTab.page.dispatchMessage("focus");
}
}
safari.application.addEventListener("message", messageHandler, false);
</script>
</head>
</html>
main.js
var oi = 'tab';
// Message Event Handler
function handleMessage(e) {
if(e.name == 'returnedSettings') {
oi = e.message;
} else if(e.name == 'focus') {
window.focus();
}
}
// Message Event Listener
safari.self.addEventListener('message', handleMessage, false);
safari.self.tab.dispatchMessage('getSettings', 'open');
我需要在此处的代码中使用 var oi
...但我不知道如何将其获取到那里,因为它会如果执行代码,则为默认值,因为异步消息......
I'm building a safari extension... I need to get some settings, but, messages are asynchronous, so, if I query the global page for some settings, it isn't there right away... How can I solve this??
Global page (the message is received by the injected JS script)
<!DOCTYPE HTML>
<html>
<script language="javascript">
function messageHandler(event) {
var activeTab = event.target.browserWindow.activeTab
if (event.name === "getSettings") {
activeTab.page.dispatchMessage("returnedSettings", safari.extension.settings[event.message]);
} else if(event.name === 'openNewTab') {
safari.application.activeBrowserWindow.openTab().url = event.message;
activeTab.page.dispatchMessage("focus");
} else if(event.name === 'openNewVenster') {
safari.application.openBrowserWindow().activeTab.url = event.message;
//activeTab.page.dispatchMessage("focus");
}
}
safari.application.addEventListener("message", messageHandler, false);
</script>
</head>
</html>
main.js
var oi = 'tab';
// Message Event Handler
function handleMessage(e) {
if(e.name == 'returnedSettings') {
oi = e.message;
} else if(e.name == 'focus') {
window.focus();
}
}
// Message Event Listener
safari.self.addEventListener('message', handleMessage, false);
safari.self.tab.dispatchMessage('getSettings', 'open');
I need var oi
in code under here... But I don't know how I can get it there, because it will be default if the code is executed, because of asynchronous messages...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有过多说明您想要做什么,但消息是异步的,您是对的。如果您所做的事情无法直接在全局页面中完成,您可以使用专门的
canLoad
消息。当然,这有点像黑客,除非你实际上是在检查某些东西是否可以加载,但我已经相当有效地使用了它几次。You don't say much about what you're trying to do, but you're right that messages are asynchronous. If what you're doing can't be done directly in the global page, you can use the specialized
canLoad
message. It's kind of a hack unless, of course, you're actually checking to see whether something can load, but I've used it a few times rather effectively.