Last active
June 27, 2025 19:06
-
-
Save DerGoogler/5880e0a9cf8ea3fdcb95618f258e4874 to your computer and use it in GitHub Desktop.
WebUI X API to open files nativly
This file contains hidden or 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Text File Viewer</title> | |
<style> | |
body { | |
font-family: sans-serif; | |
padding: 1rem; | |
background: #f0f0f0; | |
} | |
h1 { | |
font-size: 1.5rem; | |
} | |
button { | |
padding: 0.5rem 1rem; | |
font-size: 1rem; | |
margin-bottom: 1rem; | |
cursor: pointer; | |
} | |
.file-content { | |
margin-bottom: 2rem; | |
background: white; | |
padding: 1rem; | |
border: 1px solid #ccc; | |
white-space: pre-wrap; | |
font-family: monospace; | |
} | |
.file-name { | |
font-weight: bold; | |
margin-bottom: 0.5rem; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Text File Viewer</h1> | |
<button id="openFile">Pick Text Files</button> | |
<button id="source">Source</button> | |
<div id="output"></div> | |
<script type="module"> | |
import { WXEventHandler, Intent, WebUI } from "https://esm.sh/[email protected]/es2022/webuix.mjs" | |
const web = new WebUI() | |
const wx = new WXEventHandler() | |
wx.on(window, "filepicked", async (event) => { | |
const data = event.wx | |
const output = document.getElementById('output'); | |
output.innerHTML = ''; | |
const path = data.path | |
if (!fs.exists(path)) { | |
ksu.toast("File does not exists!") | |
return | |
} | |
const text = fs.read(path); | |
const container = document.createElement('div'); | |
container.className = 'file-content'; | |
container.innerHTML = ` | |
<div class="file-name">${path}</div> | |
<div>${escapeHtml(text)}</div> | |
`; | |
output.appendChild(container); | |
}); | |
const o = document.getElementById("openFile") | |
const s = document.getElementById("source") | |
const i = new Intent(Intent.ACTION_VIEW) | |
i.setData("https://google.com") | |
s.addEventListener("click", () => { | |
web.startActivity(i) | |
}) | |
const myIntent = new Intent(Intent.ACTION_GET_CONTENT) | |
myIntent.setType("text/plain") | |
myIntent.addCategory(Intent.CATEGORY_OPENABLE) | |
myIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, true) | |
myIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true) | |
o.addEventListener("click", () => { | |
web.openFile(myIntent); | |
}) | |
function escapeHtml(text) { | |
return text | |
.replace(/&/g, "&") | |
.replace(/</g, "<") | |
.replace(/>/g, ">"); | |
} | |
const fileInputToken = Object.keys(window).find(key => key.match(/^\$(\w{2})File$/m)); | |
window.fs = window[fileInputToken] | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment