Safari 扩展设置

发布于 2024-09-15 05:34:27 字数 1403 浏览 6 评论 0原文

我正在构建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丘比特射中我 2024-09-22 05:34:27

您没有过多说明您想要做什么,但消息是异步的,您是对的。如果您所做的事情无法直接在全局页面中完成,您可以使用专门的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文