-
-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get JavaScript working (table.js, plugins and more) #8
Comments
The SQL editor JavaScript on this page doesn't load either (well, none of the JavaScript loads): https://simonw.github.io/datasette-lite/#/fixtures?sql=select+sqlite_version%28%29 |
A few ideas in https://stackoverflow.com/questions/13390588/script-tag-create-with-innerhtml-of-a-div-doesnt-work I'm going to try scanning the inserted HTML for |
Potential timing bug here - what if I fire off a message asking for a script, then the user navigates to another page, then I execute the JavaScript that they asked for on that new page? I can avoid that by attaching some kind of ID attribute to the |
There's another way I could approach this: the code that runs in the web worker could parse the HTML before sending it back to the client and - if it finds any script elements - could fetch that JavaScript and send it in a The This is a kind of filthy hack and I like it. Let's see if it works! |
Sadly "Error: DOMParser is not defined" - |
I think I need to scan through each A couple of things that worry me:
Maybe I should render these things in an iframe purely to give them a fresh JavaScript context on each page load? |
Here's how to run an HTML parser against the content returned by the Datasette server in the web worker: } else if (/^text\/html/.exec(event.data.contentType)) {
+ // Check for any script tags
+ let parser = new DOMParser();
+ let dom = parser.parseFromString(event.data.text, 'text/html');
+ console.log(dom);
html = event.data.text; |
Tip from @Jonty: https://twitter.com/jonty/status/1521947838300762112
I didn't know service workers could talk to web workers - maybe this could help me solve the asset loading challenge? |
Another idea I just had: register a service worker for That way the service worker doesn't have to run Pyodide and doesn't need to ask any questions itself - it just gets primed with the content it will need to serve when Datasette first starts running. |
I should definitely explore the idea of running the injected |
Relevant: the Code for that is https://github.com/jquery/jquery/blob/main/src/core/parseHTML.js but I don't really understand what it's doing yet. This bit is interesting: https://github.com/jquery/jquery/blob/2525cffc42934c0d5c7aa085bc45dd6a8282e840/src/core/parseHTML.js#L24-L33 // Stop scripts or inline event handlers from being executed immediately
// by using document.implementation
context = document.implementation.createHTMLDocument( "" );
// Set the base href for the created document
// so any parsed elements with URLs
// are based on the document's URL (gh-2965)
base = context.createElement( "base" );
base.href = document.location.href;
context.head.appendChild( base ); Here's the issue that references: |
I saw your tweet/blog post, and loved what you're doing. This kind of thing is my favourite. Forgive me if you already know about all this, but in case it's helpful, I've done this in the past a few ways:
const js = new Blob(
['alert("hello world")'],
{ type: 'application/json' }
);
const url = URL.createObjectURL(js);
const script = document.createElement('script');
script.src = url;
document.body.appendChild(script);
You could probably put a service worker in front of your database, and pull the web assets from there, which would be fun (web site as db). |
This is great, thanks! Really useful example code - I'm leaning service worker at the moment but that blob trick looks like a great backup for if I can't get SWs to work. |
Now that I've added |
I've been running a fork for the past few months that gets JS (and other static assets served by datasette, like CSS or SQL query results) working in datasette-lite. Initial support was added here: hydrosquall/datasette-nteract-data-explorer#26 So far, I've tested it successfully with
|
Relates to plugins challenge:
The
table.js
script used by the table page doesn't load at the moment, which means no cog icons on the columns:The text was updated successfully, but these errors were encountered: