Skip to content

Commit

Permalink
Add syntax highlight using speed-highlight.js
Browse files Browse the repository at this point in the history
  • Loading branch information
JingMatrix committed Dec 17, 2022
1 parent 9fcbafa commit 539024d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 32 deletions.
108 changes: 77 additions & 31 deletions app/src/main/java/org/matrix/chromext/script/LocalScripts.kt
Original file line number Diff line number Diff line change
@@ -1,34 +1,5 @@
package org.matrix.chromext.script

const val promptInstallUserScript: String =
"""
setTimeout(() => {
let old_script = "";
function installScript() {
let script = document.querySelector("body > pre").innerText;
if (script != old_script) {
old_script = script;
let install = confirm("Allow ChromeXt to install / update this userscript?");
if (install) {
console.debug(JSON.stringify({"action": "installScript", "payload": script}));
}
}
};
document.querySelector("body > pre").setAttribute("contenteditable", true);
installScript();
let asked = false;
addEventListener("contextmenu", (e) => {
addEventListener("click", (_event) => {
if (!asked) {
event.preventDefault();
setTimeout(()=>{asked=false;installScript();}, 100);
asked = true;
}
}, {once: true});
});
}, 100)
"""

const val GM_addStyle =
"""
function GM_addStyle(styles) {
Expand All @@ -50,7 +21,11 @@ const val GM_addElement =
function GM_addElement(parent_node, tag_name, attributes) {
const element = document.createElement(tag_name);
for (const [key, value] of Object.entries(attributes)) {
element.setAttribute(key, value);
if (key != "textContent") {
element.setAttribute(key, value);
} else {
element.textContent = value;
};
};
if (parent_node === undefined) {
document.documentElement.appendChild(element);
Expand Down Expand Up @@ -84,7 +59,7 @@ fun encodeScript(script: Script): String? {
when (script.runAt) {
RunAt.START -> {}
RunAt.END -> code = """document.addEventListener("DOMContentLoaded",()=>{${code}});"""
RunAt.IDLE -> code = """window.onload=()=>{setTimeout(()=>{${code}},1)};"""
RunAt.IDLE -> code = """window.onload=()=>{${code}};"""
}

val imports =
Expand Down Expand Up @@ -119,3 +94,74 @@ fun encodeScript(script: Script): String? {
}
return code
}

const val promptInstallUserScript: String =
"""
window.onload=()=>{
const js = document.createElement("script");
const meta = document.createElement("meta");
const style = document.createElement("style");
js.setAttribute("type", "module");
js.textContent = "import {highlightElement} from 'https://unpkg.com/@speed-highlight/core/dist/index.js';highlightElement(document.querySelector('body > pre'), 'js');";
style.setAttribute("type", "text/css");
meta.setAttribute("name", "viewport");
meta.setAttribute("content", "width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no");
style.textContent = `
@import url('https://unpkg.com/@speed-highlight/core/dist/themes/default.css');
body {
margin: 0;
}
[class*="shj-lang-"] {
border-radius: 0;
color: #abb2bf;
background: #161b22;
font: 1em monospace;
padding: 0;
margin: 0;
width:
}
[class*="shj-lang-"]:before {color: #6f9aff}
.shj-syn-deleted,
.shj-syn-err,
.shj-syn-var {color: #e06c75}
.shj-syn-section,
.shj-syn-oper,
.shj-syn-kwd {color: #c678dd}
.shj-syn-class {color: #e5c07b}
.shj-numbers,
.shj-syn-cmnt {color: #76839a}
.shj-syn-insert {color: #98c379}
.shj-syn-type {color: #56b6c2}
.shj-syn-num,
.shj-syn-bool {color: #d19a66}
.shj-syn-str,
.shj-syn-func {color: #61afef}
`;
document.head.appendChild(meta);
document.head.appendChild(style);
document.body.appendChild(js);
let old_script = "";
function installScript() {
let script = document.querySelector("body > pre").innerText;
if (script != old_script) {
old_script = script;
let install = confirm("Allow ChromeXt to install / update this userscript?");
if (install) {
console.debug(JSON.stringify({"action": "installScript", "payload": script}));
}
}
};
document.querySelector("body > pre").setAttribute("contenteditable", true);
installScript();
let asked = false;
addEventListener("contextmenu", () => {
addEventListener("click", (event) => {
if (!asked) {
event.preventDefault();
setTimeout(()=>{asked=false;installScript();}, 100);
asked = true;
}
}, {once: true});
});
};
"""
2 changes: 1 addition & 1 deletion app/src/main/java/org/matrix/chromext/script/Parser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fun parseScript(input: String): Script? {
"match" -> script.match.add(value)
"include" -> script.match.add(value)
"grant" ->
if (value.startsWith("GM_")) {
if (value != "none") {
script.grant.add(value)
}
"exclude" -> script.exclude.add(value)
Expand Down

0 comments on commit 539024d

Please sign in to comment.