-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
78 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 2 additions & 69 deletions
71
...s/WikiWebView/useStreamChunksToWebView.ts → ...WebView/useStreamChunksToWebView/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/pages/WikiWebView/useStreamChunksToWebView/webviewSideReceiver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
export const webviewSideReceiver = `// Initialize an empty string to start with | ||
let tiddlersStoreAccumulatedContent = ''; | ||
let wikiHTML = ''; | ||
window.onChunk = function (event) { | ||
const data = event.data; | ||
switch (event.type) { | ||
case 'TIDDLYWIKI_HTML': { | ||
wikiHTML += data; | ||
break; | ||
} | ||
case 'TIDDLER_STORE_SCRIPT_CHUNK': { | ||
tiddlersStoreAccumulatedContent += data; | ||
break; | ||
} | ||
case 'TIDDLER_STORE_SCRIPT_CHUNK_END': { | ||
/** | ||
* All information needed are collected. | ||
* Start using html and store. | ||
*/ | ||
/** | ||
* Use MutationObserver to watch if wikiHTML is loaded. | ||
* We execute the script tags after this. | ||
*/ | ||
const observer = new MutationObserver((mutationsList, observer) => { | ||
for (let mutation of mutationsList) { | ||
if (mutation.type === 'childList') { | ||
executeScripts(); | ||
observer.disconnect(); // Important: disconnect the observer once done. | ||
} | ||
} | ||
}); | ||
// Start observing the body with the configured parameters | ||
observer.observe(document.body, { childList: true }); | ||
// this ignores all script tags, so we need 'executeScripts()' later. | ||
document.body.innerHTML = wikiHTML; | ||
} | ||
} | ||
}; | ||
/** | ||
* Manually execute each of the script tags. | ||
* Delay the script execution slightly, until MutationObserver found document.body is ready. | ||
*/ | ||
function executeScripts() { | ||
// load tiddlers store | ||
const tiddlersStoreScript = document.createElement("script"); | ||
tiddlersStoreScript.type = 'application/json'; | ||
tiddlersStoreScript.class = 'tiddlywiki-tiddler-store'; | ||
tiddlersStoreScript.textContent = tiddlersStoreAccumulatedContent; | ||
document.body.appendChild(tiddlersStoreScript); | ||
// load other scripts | ||
const scriptElements = document.querySelectorAll("script"); | ||
for (let script of scriptElements) { | ||
const newScript = document.createElement("script"); | ||
if (script.src) { | ||
newScript.src = script.src; | ||
newScript.class = script.class; | ||
newScript.type = script.type; | ||
newScript.id = script.id; | ||
} else { | ||
newScript.textContent = script.textContent; | ||
} | ||
document.body.appendChild(newScript); | ||
script.parentNode.removeChild(script); // Remove the old script element | ||
} | ||
} | ||
`; |