fixed quotes and made a safelink function

This commit is contained in:
MathMan05 2024-09-05 12:00:05 -05:00
parent e3185a06c6
commit 04779b6c6c
7 changed files with 156 additions and 14 deletions

View file

@ -225,7 +225,7 @@ class Dialog {
return table;
}
default:
console.error("can't find element:" + array[0], " full element:" + array);
console.error("can't find element:" + array[0], " full element:", array);
}
}
show() {

View file

@ -60,7 +60,7 @@ class Embed {
const a = document.createElement("a");
a.textContent = this.json.author.name;
if (this.json.author.url) {
a.href = this.json.author.url;
MarkDown.safeLink(a, this.json.author.url);
}
a.classList.add("username");
authorline.append(a);
@ -70,7 +70,7 @@ class Embed {
const title = document.createElement("a");
title.append(new MarkDown(this.json.title, this.channel).makeHTML());
if (this.json.url) {
title.href = this.json.url;
MarkDown.safeLink(title, this.json.url);
}
title.classList.add("embedtitle");
embed.append(title);
@ -155,7 +155,7 @@ class Embed {
if (this.json.url && this.json.title) {
const td = document.createElement("td");
const a = document.createElement("a");
a.href = this.json.url;
MarkDown.safeLink(a, this.json.url);
a.textContent = this.json.title;
td.append(a);
trtop.append(td);
@ -199,7 +199,7 @@ class Embed {
}
const a = document.createElement("a");
if (this.json.url && this.json.url) {
a.href = this.json.url;
MarkDown.safeLink(a, this.json.url);
a.textContent = this.json.url;
div.append(a);
}

View file

@ -1,4 +1,5 @@
import { Channel } from "./channel.js";
import { Dialog } from "./dialog.js";
import { Emoji } from "./emoji.js";
class MarkDown {
txt;
@ -102,6 +103,7 @@ class MarkDown {
}
if (keep) {
element.append(keepys);
//span.appendChild(document.createElement("br"));
}
element.appendChild(this.markdown(build, { keep, stdsize }));
span.append(element);
@ -394,6 +396,29 @@ class MarkDown {
continue;
}
}
if (txt[i] === "h" && txt[i + 1] === "t" && txt[i + 2] === "t" && txt[i + 3] === "p") {
let build = "http";
let j = i + 4;
const endchars = new Set(["\\", "<", ">", "|", "]", " "]);
for (; txt[j] !== undefined; j++) {
const char = txt[j];
if (endchars.has(char)) {
break;
}
build += char;
}
console.log("checking:" + build);
if (URL.canParse(build)) {
const a = document.createElement("a");
//a.href=build;
MarkDown.safeLink(a, build);
a.textContent = build;
a.target = "_blank";
i = j;
span.appendChild(a);
}
continue;
}
if (txt[i] === "<" && txt[i + 1] === "t" && txt[i + 2] === ":") {
let found = false;
const build = ["<", "t", ":"];
@ -489,7 +514,7 @@ class MarkDown {
const parts = build.join("").match(/^\[(.+)\]\((https?:.+?)( ('|").+('|"))?\)$/);
if (parts) {
const linkElem = document.createElement("a");
linkElem.href = parts[2];
MarkDown.safeLink(linkElem, parts[2]);
linkElem.textContent = parts[1];
linkElem.target = "_blank";
linkElem.rel = "noopener noreferrer";
@ -562,6 +587,54 @@ class MarkDown {
}
return build;
}
static trustedDomains = new Set([location.host]);
static safeLink(elm, url) {
if (URL.canParse(url)) {
const Url = new URL(url);
if (elm instanceof HTMLAnchorElement && this.trustedDomains.has(Url.host)) {
elm.href = url;
elm.target = "_blank";
return;
}
elm.onmouseup = _ => {
if (_.button === 2)
return;
console.log(":3");
function open() {
const proxy = window.open(url, '_blank');
if (proxy && _.button === 1) {
proxy.focus();
}
else if (proxy) {
window.focus();
}
}
if (this.trustedDomains.has(Url.host)) {
open();
}
else {
const full = new Dialog([
"vdiv",
["title", "You're leaving spacebar"],
["text", "You're going to " + Url.host + " are you sure you want to go there?"],
["hdiv",
["button", "", "Nevermind", _ => full.hide()],
["button", "", "Go there", _ => { open(); full.hide(); }],
["button", "", "Go there and trust in the future", _ => {
open();
full.hide();
this.trustedDomains.add(Url.host);
}]
]
]);
full.show();
}
};
}
else {
throw Error(url + " is not a valid URL");
}
}
}
//solution from https://stackoverflow.com/questions/4576694/saving-and-restoring-caret-position-for-contenteditable-div
function saveCaretPosition(context) {