Safari Extension Questions

前端 未结 4 1189
萌比男神i
萌比男神i 2021-02-05 17:43

I\'m in the process of building my first Safari extension--a very simple one--but I\'ve run into a couple of problems. The extension boils down to a single, injected script that

4条回答
  •  -上瘾入骨i
    2021-02-05 18:27

    Like everyone else at this point, I'm still climbing the learning curve, but here's how I've handled this problem:

    I have a simple extension with no chrome and one injected end script (script.js). For the purpose of loading settings I've added a simple global page (proxy.html). When script.js is injected, it sends a getSettings message to proxy.html. proxy.html responds with a setSettings message, and script.js continues initialization.

    The most helpful page I've found in the docs on this topic is Messages and Proxies.

    proxy.html:

    
    
    
      
    
    
    
    

    script.js:

    ( function() {
      var settings, init = function() {
        // do extension stuff
      };
    
      // listen for an incoming setSettings message
      safari.self.addEventListener( "message", function( e ) {
        if( e.name === "setSettings" ) {
          settings = e.message;
          init();
        }
      }, false );
    
      // ask proxy.html for settings
      safari.self.tab.dispatchMessage( "getSettings" );
    }() )
    

提交回复
热议问题