TiddlyWiki

Some vibe-coding with TiddlyWiki and Livecode. > When LiveCode executes JavaScript via the `do ... in widget` command, the result of the script is not directly returned via `the result`. Instead, you'll need to work around this limitation by using a combination of callbacks and communication mechanisms within the browser widget. Here’s a revised approach to handle this issue effectively: ### Using `document.location` for Communication You can use JavaScript to modify `document.location` in the browser widget to send data back to LiveCode. LiveCode can then intercept the URL via the `browserUnhandledLoadRequest` message. ### Revised `getprop` Handler with Callback ```livecode getprop tiddler_Titles local tScript -- JavaScript to fetch the tiddler titles and send them back to LiveCode put "document.location = 'livecode://titles/' + encodeURIComponent($tw.wiki.filterTiddlers('[all[tiddlers]]').join(';'));" into tScript -- Execute the JavaScript in the browser widget do tScript in widget "yourBrowserWidget" -- Set up a handler to capture the data wait until sTiddlerTitles is not empty with messages return sTiddlerTitles end tiddler_Titles ``` ### Handling `browserUnhandledLoadRequest` To capture the data from the custom URL (`livecode://titles/`), implement the `browserUnhandledLoadRequest` handler: ```livecode on browserUnhandledLoadRequest pUrl if pUrl begins with "livecode://titles/" then -- Extract the data put replaceText(pUrl, "livecode://titles/", "") into tEncodedTitles put urlDecode(tEncodedTitles) into sTiddlerTitles else pass browserUnhandledLoadRequest end if end browserUnhandledLoadRequest ``` ### Key Concepts 1. **Custom URL Scheme**: - The JavaScript modifies the `document.location` to a `livecode://` URL that carries the data (`titles` in this case). 2. **Global Variable for State**: - A global variable (`sTiddlerTitles`) is used to hold the fetched data and communicate it between the handlers. 3. **Wait for Data**: - The `wait until` statement ensures the handler pauses execution until the data is available. 4. **URL Decoding**: - JavaScript encodes the data for safe transmission via `encodeURIComponent`. LiveCode decodes it with `urlDecode`. ### Debugging Tips 1. **JavaScript Console**: - Test the JavaScript directly in TiddlyWiki's developer console to ensure it generates the desired result. 2. **Inspect `pUrl`**: - Log or inspect `pUrl` in the `browserUnhandledLoadRequest` handler to verify the data is being passed correctly. 3. **Error Handling**: - If `sTiddlerTitles` remains empty, add debug statements to identify where the process is failing. ### Example Usage Once set up, you can fetch the tiddler titles simply by accessing the `tiddler_Titles` property: ```livecode put the tiddler_Titles of widget "yourBrowserWidget" into tTitles answer "Tiddler titles: " & tTitles ``` This approach leverages LiveCode's messaging system to bridge the gap between JavaScript execution and LiveCode data handling. Let me know how it works for you!

Here is a demo of TiddlyWiki inside wiki.

http://livecode.wiki/assets/tiddlywiki/noteself.html HEIGHT 600 Plain TiddlyWiki

tiddlywiki