Stratify a Chrome extension

21 views
Skip to first unread message

Jeremy Gregorio

unread,
Nov 29, 2015, 9:25:23 PM11/29/15
to StratifiedJS
Hi all,
       I've got a little Chrome extension I'm working on and want to use StratifiedJS. I didn't have any trouble running my code in the HTML using <script type="text/sjs"></script> tags, but I haven't been able to get <script src="stratjs/stratified.js" main="./init"></script> syntax to work. I keep getting "Don't know how to load module at..." errors. I found this post:

https://groups.google.com/forum/#!topic/stratifiedjs/dDx24GQw6Bg


which suggested using stratified-aot.js, but that didn't work either. If feels like I'm just missing something obvious (e.g. some syntax to get sjs to load from a file). Anyone know what I'm doing wrong?

Thanks :)!

Here's my code:

<html>

   
<head>
       
<script src="stratjs/stratified.js" main="init"></script> <!--This blows up with a Don't know how to load module at... error -->
       
<script src="install.js"></script>

       
       
<script type="text/sjs">
           
            chrome
.storage.local.set({'userid': "foo"}, function (result) {
                console
.log("Settings Saved");
           
});

           
//This works, but I can't figure out any way to move it to it's own file.
           
//  Chrome doesn't want to work with
           
              waitfor
(var testVal) {
           
                    chrome
.storage.local.get('userid', function (result) {
                       
                       
                        console
.log("First Call to Chrome.storage.local.get with " + result.userid);
                        testVal
= result.userid;
                        resume
(result.userid);
                   
});
             
                   
               
};
           
                chrome
.storage.local.get('userid', function (result) {
                    console
.log("Second Call to Chrome.storage.local.get " + result.userid);
               
});
           
           
            console
.log ("After Calls to Chrome.storage.local.get testVal is " + testVal);// this fails because it runs before the call to chrome.storage.local.get finishes.

           
var mymodule = require("stratified-testsjs.sjs"); // this blows up too :P.
           
       
</script>
       
       
<script  src="prefService.js" ></script>
       
   
</head>

   
<body>
   
       
   
</body>

</html>


Tim Cuthbertson

unread,
Nov 30, 2015, 5:12:48 AM11/30/15
to StratifiedJS
Hi Jeremy,

There are two different things that could be a problem here, and you may be hitting one or more of them. Unfortunately I haven't touched chrome extensions for a long time, but maybe I can point you in the right direction:

The first issue is module URIs. In normal web use of stratifiedjs, each module lives at a canonical URI, say "http://example.com/__sjs/sequence.sjs". Module shorthands like "sjs:sequence" are expanded under the hood. And relative paths are also expanded, relative to the path of the current module. But the "current module" of a <script> tag is a bit of a unique case, I'm pretty sure it uses `document.location.href`. But to remove one source of indirection, it might be useful to specify an absolute URI for your script's "main" attribute, at least to get it working.

The second issue is resource loading - SJS knows how to load http / https / file URIs, but I can't remember how chrome extensions access bundled content. And even if SJS _does_ know how to load your resource, it may be unable to execute it as code due to chrome's content security policy. This is what the thread you linked was concerned with - using the "-aot" version of the runtime _combined_ with the output of the `sjs:bundle` tool is a way for SJS to precompile all the modules you need, which should make them work in a chrome extension. Note that this all sits on top of the same URI scheme as above - so you'll need to first figure out the appropriate URIs for your modules, and _then_ you can use this mechanism to load precompiled versions of those modules.

Sorry I couldn't pinpoint exactly what's going wrong, but hopefully the above can point you in the right direction,

Cheers,
 - Tim.
Reply all
Reply to author
Forward
0 new messages