1 module awebview.gui.scripthelper;
2 
3 import awebview.gui.activity;
4 import awebview.gui.methodhandler;
5 import awebview.wrapper;
6 import std.conv : to;
7 
8 struct ScriptHelper
9 {
10     this(Activity activity, string name)
11     {
12         _activity = activity;
13         _sh = activity.createObject(name);
14         activity.methodHandler.set(this);
15     }
16 
17 
18     WeakRef!JSObject jsobject() { return _sh; }
19 
20 
21     @JSMethodTag("callDLangFunc"w)
22     JSValue callDLangFunc(WeakRef!(const(JSArrayCpp)) args)
23     {
24         if(args.length < 1){
25             _lastValue = JSValue.undefined;
26             return _lastValue;
27         }
28 
29         switch(args[0].to!string)
30         {
31           case "std.net.curl.get":
32             if(args.length < 2)
33                 break;
34 
35             import std.stdio;
36             WebURL url = args[1].to!string;
37             if(url.scheme == "file"){
38                 import std.file : readText;
39                 string path = url.path.to!string;
40                 if(path[0] == '/')
41                     path = path[1 .. $];
42                 import std.stdio;
43                 writeln(path);
44                 return JSValue(readText(path));
45             }
46             else{
47                 import std.net.curl : get;
48                 _lastValue = get(args[1].to!string);
49                 return _lastValue;
50             }
51 
52           default:
53         }
54 
55         _lastValue = JSValue.undefined;
56         return _lastValue;
57     }
58 
59 
60   private:
61     JSValue _lastValue;
62     Activity _activity;
63     WeakRef!JSObject _sh;
64 }
65 
66 
67 ScriptHelper createScriptHelper(Activity activity)
68 {
69     auto sh = ScriptHelper(activity, "ScriptHelper");
70     return sh;
71 }