文本与代码预览
code_20260614.txt
txt · 6.19 KB
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WhateverOrigin proxy preview</title>
<style>
body { font-family: sans-serif; padding: 20px; background: #f4f6f9; margin:0; }
.container { max-width: 1000px; margin: 0 auto; background: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); }
.control-group { margin-bottom: 15px; display: flex; gap: 10px; align-items: center; flex-wrap: wrap;}
input[type="text"] { flex: 1; padding: 10px; border: 1px solid #ddd; border-radius: 4px; font-size: 14px; min-width: 260px;}
button { padding: 10px 14px; background: #007bff; color: #fff; border: none; border-radius: 4px; cursor: pointer; }
.btn-proxy { background:#28a745; }
.btn-download { background:#fd7e14; }
.btn-clear { background:#dc3545; }
.preview-box { width: 100%; height: 600px; border: 1px solid #ddd; border-radius: 4px; overflow: hidden; background: #fff; }
iframe { width: 100%; height: 100%; border: none; display: none; }
.alert { padding: 10px; margin-bottom: 10px; border-radius: 4px; display: none;}
.alert-error { background: #f8d7da; color: #721c24; }
.alert-success { background: #d4edda; color:#155724; display:none; }
.tip { font-size:13px; color:#666; margin:8px 0; }
</style>
</head>
<body>
<div class="container">
<h2>WhateverOrigin proxy preview tool</h2>
<div class="control-group">
<input type="text" id="fileUrl" placeholder="Input TXT / MD / JSON / HTML link">
<button onclick="directLoad()">Direct load</button>
<button class="btn-proxy" onclick="proxyLoad()">Load via WhateverOrigin proxy</button>
<button class="btn-download" onclick="downloadCache()">Download cached file</button>
<button class="btn-clear" onclick="clearCache()">Clear cache</button>
</div>
<div class="tip">
Fix: HTML renders as full page without escaping; only plain text files (txt/md/json) are wrapped and escaped.
</div>
<div id="errorMsg" class="alert alert-error"></div>
<div id="successMsg" class="alert alert-success"></div>
<div class="preview-box">
<iframe id="htmlFrame"></iframe>
</div>
</div>
<script>
const CACHE_KEY = "whatever_origin_all_render_cache_v2";
const PROXY_URL = "https://www.whateverorigin.org/get?url=";
const fileUrlInput = document.getElementById("fileUrl");
const htmlFrame = document.getElementById("htmlFrame");
const errorMsg = document.getElementById("errorMsg");
const successMsg = document.getElementById("successMsg");
window.onload = () => {
const raw = localStorage.getItem(CACHE_KEY);
if(raw){
const data = JSON.parse(raw);
fileUrlInput.value = data.url;
renderToIframe(data.content, data.isText);
showSuccess("Loaded cache");
}
}
function resetView(){
errorMsg.style.display = "none";
successMsg.style.display = "none";
htmlFrame.style.display = "none";
}
function isTextFile(url){
return /\.(txt|md|log|json|csv|ini)$/i.test(url);
}
function escapeHtml(str) {
return str
.replace(/&/g,"&")
.replace(/</g,"<")
.replace(/>/g,">")
.replace(/"/g,""")
.replace(/'/g,"'");
}
function renderToIframe(rawContent, isText){
let outputHtml;
if(isText){
outputHtml = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
body{background:#fff;color:#222;padding:16px;font-family:monospace;white-space:pre-wrap;word-wrap:break-word;margin:0;}
</style>
</head>
<body>${escapeHtml(rawContent)}</body>
</html>`;
}else{
outputHtml = rawContent;
}
htmlFrame.srcdoc = outputHtml;
htmlFrame.style.display = "block";
}
function directLoad(){
resetView();
const url = fileUrlInput.value.trim();
if(!url) return showError("Please input link");
const textFlag = isTextFile(url);
if(!textFlag){
htmlFrame.src = url;
htmlFrame.style.display = "block";
saveCache(url, "", false);
showSuccess("HTML preview ok");
}else{
fetch(url)
.then(r=>{
if(!r.ok) throw new Error("CORS blocked, use proxy");
return r.text();
})
.then(text=>{
renderToIframe(text, true);
saveCache(url, text, true);
showSuccess("Text converted to page view, cached");
})
.catch(e=>showError(e.message));
}
}
async function proxyLoad(){
resetView();
const url = fileUrlInput.value.trim();
if(!url) return showError("Please input link");
const textFlag = isTextFile(url);
try{
showSuccess("Fetching via WhateverOrigin proxy...");
const target = PROXY_URL + encodeURIComponent(url);
const res = await fetch(target);
if(!res.ok) throw new Error("Proxy unavailable");
const rawText = await res.text();
let content;
try {
const jsonData = JSON.parse(rawText);
content = jsonData.contents;
} catch (e) {
content = rawText;
}
renderToIframe(content, textFlag);
saveCache(url, content, textFlag);
showSuccess("Rendered as page and cached");
}catch(e){
showError(e.message);
}
}
function saveCache(url, content, isText){
localStorage.setItem(CACHE_KEY, JSON.stringify({url, content, isText, time:Date.now()}));
}
function downloadCache(){
resetView();
const raw = localStorage.getItem(CACHE_KEY);
if(!raw) return showError("No cached content");
const data = JSON.parse(raw);
const url = data.url;
const content = data.content || "";
const ext = data.isText ? "txt" : "html";
const blob = new Blob([content], {type:`text/${ext}`});
const a = document.createElement("a");
a.href = URL.createObjectURL(blob);
a.download = `preview.${ext}`;
a.click();
URL.revokeObjectURL(a.href);
showSuccess("File downloaded");
}
function clearCache(){
resetView();
localStorage.removeItem(CACHE_KEY);
fileUrlInput.value = "";
showSuccess("Cache cleared");
}
function showError(msg){
errorMsg.textContent = msg;
errorMsg.style.display = "block";
}
function showSuccess(msg){
successMsg.textContent = msg;
successMsg.style.display = "block";
}
</script>
</body>
</html>