Some more minor changes

This commit is contained in:
Sal Rahman 2023-08-21 21:02:36 -07:00
parent 34e2769c11
commit 4d84284356
5 changed files with 58 additions and 15 deletions

View file

@ -103,6 +103,8 @@ import { onUnmounted, ref } from "vue";
import { url as local, lang } from "@/config"; import { url as local, lang } from "@/config";
import { i18n } from "@/i18n"; import { i18n } from "@/i18n";
import { defaultStore } from "@/store"; import { defaultStore } from "@/store";
import { parseUri, parsedUriToString } from '@/scripts/parse-uri'
import { SCHEMA_BROWSERSAFE } from "../const";
const props = withDefaults( const props = withDefaults(
defineProps<{ defineProps<{
@ -134,30 +136,30 @@ let tweetExpanded = ref(props.detail);
const embedId = `embed${Math.random().toString().replace(/\D/, "")}`; const embedId = `embed${Math.random().toString().replace(/\D/, "")}`;
let tweetHeight = ref(150); let tweetHeight = ref(150);
const requestUrl = new URL(props.url); const requestUrl = parseUri(props.url);
if (!["http:", "https:", "gopher:", "gemini:", "matrix:", "ipfs:", "ipns:", "finger:"].includes(requestUrl.protocol) && !requestUrl.protocol.startsWith("web+")) if (!SCHEMA_BROWSERSAFE.includes(requestUrl.scheme))
throw new Error("invalid url"); throw new Error("invalid url");
if ( if (
["twitter.com", "mobile.twitter.com", "x.com"].includes(requestUrl.hostname) ["twitter.com", "mobile.twitter.com", "x.com"].includes(requestUrl.authority ?? "")
) { ) {
const m = requestUrl.pathname.match(/^\/.+\/status(?:es)?\/(\d+)/); const m = (requestUrl.path ?? "").match(/^\/.+\/status(?:es)?\/(\d+)/);
if (m) tweetId.value = m[1]; if (m) tweetId.value = m[1];
} }
if ( if (
requestUrl.hostname === "music.youtube.com" && requestUrl.authority === "music.youtube.com" &&
requestUrl.pathname.match("^/(?:watch|channel)") (requestUrl.path ?? "").match("^/(?:watch|channel)")
) { ) {
requestUrl.hostname = "www.youtube.com"; requestUrl.authority = "www.youtube.com";
} }
const requestLang = (lang || "ja-JP").replace("ja-KS", "ja-JP"); const requestLang = (lang || "ja-JP").replace("ja-KS", "ja-JP");
requestUrl.hash = ""; requestUrl.fragment = "";
fetch( fetch(
`/url?url=${encodeURIComponent(requestUrl.href)}&lang=${requestLang}`, `/url?url=${encodeURIComponent(parsedUriToString(requestUrl))}&lang=${requestLang}`,
).then((res) => { ).then((res) => {
res.json().then((info) => { res.json().then((info) => {
if (info.url == null) return; if (info.url == null) return;

View file

@ -11,7 +11,7 @@
@click.stop @click.stop
> >
<template v-if="!self"> <template v-if="!self">
<span class="schema">{{ schema }}{{ hostname.trim() != '' ? '//' : '' }}</span> <span class="schema">{{ schema }}{{ hostname.trim() != '' ? '://' : '' }}</span>
<span class="hostname">{{ hostname.trim() != '' ? hostname.trim() : pathname }}</span> <span class="hostname">{{ hostname.trim() != '' ? hostname.trim() : pathname }}</span>
<!-- <span v-if="port != ''" class="port">:{{ port }}</span> --> <!-- <span v-if="port != ''" class="port">:{{ port }}</span> -->
</template> </template>
@ -38,6 +38,7 @@ import * as os from "@/os";
import { useTooltip } from "@/scripts/use-tooltip"; import { useTooltip } from "@/scripts/use-tooltip";
import { safeURIDecode } from "@/scripts/safe-uri-decode"; import { safeURIDecode } from "@/scripts/safe-uri-decode";
import { parseUri } from "@/scripts/parse-uri" import { parseUri } from "@/scripts/parse-uri"
import { SCHEMA_BROWSERSAFE } from "../../const";
const props = defineProps<{ const props = defineProps<{
url: string; url: string;
@ -46,7 +47,10 @@ const props = defineProps<{
const self = props.url.startsWith(local); const self = props.url.startsWith(local);
const url = parseUri(props.url); const url = parseUri(props.url);
if (!["http", "https", "gopher", "gemini", "matrix", "ipfs", "ipns", "finger"].includes(url.scheme) && !url.scheme.startsWith("web+")) console.log(url);
console.log(props.url);
console.log(url.scheme);
if (!SCHEMA_BROWSERSAFE.includes(url.scheme))
throw new Error("invalid url"); throw new Error("invalid url");
const el = ref(); const el = ref();

View file

@ -38,6 +38,19 @@ export const FILE_TYPE_BROWSERSAFE = [
"audio/x-flac", "audio/x-flac",
"audio/vnd.wave", "audio/vnd.wave",
]; ];
export const SCHEMA_BROWSERSAFE = [
"http",
"https",
"gopher",
"gemini",
"matrix",
"ipfs",
"ipns",
"icmp",
"finger"
];
/* /*
https://github.com/sindresorhus/file-type/blob/main/supported.js https://github.com/sindresorhus/file-type/blob/main/supported.js
https://github.com/sindresorhus/file-type/blob/main/core.js https://github.com/sindresorhus/file-type/blob/main/core.js

View file

@ -1,10 +1,12 @@
export function parseUri(uri: string): { type URI = {
scheme: string; scheme: string;
authority: string | null; authority: string | null;
path: string | null; path: string | null;
query: string | null; query: string | null;
fragment: string | null; fragment: string | null;
} { }
export function parseUri(uri: string): URI {
const urlParts = uri.match( const urlParts = uri.match(
/^([\w+]+):(\/\/)?([^/?#]*)(\/([^?#]*))?(\?([^#]*))?(#(.*))?$/, /^([\w+]+):(\/\/)?([^/?#]*)(\/([^?#]*))?(\?([^#]*))?(#(.*))?$/,
); );
@ -24,3 +26,25 @@ export function parseUri(uri: string): {
fragment: fragment || null, fragment: fragment || null,
}; };
} }
export function parsedUriToString(uri: URI): string {
let result = `${uri.scheme}:`;
if (uri.authority) {
result += `//${uri.authority}`;
}
if (uri.path) {
result += uri.path;
}
if (uri.query) {
result += `?${uri.query}`;
}
if (uri.fragment) {
result += `#${uri.fragment}`;
}
return result;
}

View file

@ -674,7 +674,7 @@ export const language = P.createLanguage({
])); ]));
const parser = P.seq([ const parser = P.seq([
notLinkLabel, notLinkLabel,
P.regexp(/((https?)|(gemini)|(gopher)|(matrix)|(ipns)|(ipfs)|(finger)|(web\+\w+)):\/\//), P.regexp(/(?:https?|gemini|gopher|matrix|ipns|ipfs|icmp|finger|cool|web\+\w+):\/\//),
innerItem.many(1).text(), innerItem.many(1).text(),
]); ]);
return new P.Parser<M.MfmUrl | string>((input, index, state) => { return new P.Parser<M.MfmUrl | string>((input, index, state) => {
@ -706,7 +706,7 @@ export const language = P.createLanguage({
const parser = P.seq([ const parser = P.seq([
notLinkLabel, notLinkLabel,
open, open,
P.regexp(/((https?)|(gemini)|(gopher)|(matrix)|(ipns)|(ipfs)|(finger)|(web\+\w+)):\/\//), P.regexp(/(?:https?|gemini|gopher|matrix|ipns|ipfs|finger|cool|web\+\w+):\/\//),
P.seq([P.notMatch(P.alt([close, space])), P.char], 1).many(1), P.seq([P.notMatch(P.alt([close, space])), P.char], 1).many(1),
close, close,
]).text(); ]).text();