Skip to content

Commit

Permalink
feat: #340
Browse files Browse the repository at this point in the history
  • Loading branch information
livingflore committed Feb 12, 2024
1 parent aa4bcfe commit 525d915
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/features/shareShortener/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@ export async function enableShareShortener() {
} = optionsData;
if (!enable_share_shortener) return;

const regexp: RegExp = new RegExp("(\\?|&)(si|feature)=[^&]*", "g");
const regexp: RegExp = new RegExp("(\\?|&)(si|feature|pp)=[^&]*", "g");

function attachEventListener(): void {
const checkbox: HTMLElement | null = document.querySelector(".style-scope.tp-yt-paper-checkbox");
const tsInput: HTMLElement | null = document.querySelector(".style-scope.tp-yt-paper-input .input-element input");
const allElements: HTMLElement[] = Array.from(document.querySelectorAll("*"));
allElements.forEach((e) => {
const href: null | string = e.getAttribute("href");
if (href && href.match(/^\/watch\?v\=.+$/gm)) {
e.setAttribute("href", href.replace(regexp, ""));
}
});

if (checkbox && tsInput) {
checkbox.addEventListener("DOMAttrModified", function (this: HTMLInputElement) {
const shareUrlInput: HTMLInputElement | null = document.getElementById("share-url") as HTMLInputElement;
const shareUrlInput = document.getElementById("share-url") as HTMLInputElement;
if (shareUrlInput) {
setTimeout(() => {
shareUrlInput.value = shareUrlInput.value.replace(regexp, "");
Expand All @@ -29,7 +36,7 @@ export async function enableShareShortener() {
tsInput.addEventListener("keypress", function (event: KeyboardEvent) {
if (event.key === "Enter") {
setTimeout(() => {
const shareUrlInput: HTMLInputElement | null = document.getElementById("share-url") as HTMLInputElement;
const shareUrlInput = document.getElementById("share-url") as HTMLInputElement;
if (shareUrlInput) {
const cleanUrl: string = shareUrlInput.value.replace(regexp, "");
shareUrlInput.value = cleanUrl;
Expand All @@ -43,7 +50,7 @@ export async function enableShareShortener() {
function monitorUrl(mutationsList: MutationRecord[]): void {
for (const mutation of mutationsList) {
if (mutation.target !== document.getElementById("share-url")) {
const shareUrlInput: HTMLInputElement | null = document.getElementById("share-url") as HTMLInputElement;
const shareUrlInput = document.getElementById("share-url") as HTMLInputElement;
if (shareUrlInput) {
const cleanUrl: string = shareUrlInput.value.replace(regexp, "");
shareUrlInput.value = cleanUrl;
Expand Down

0 comments on commit 525d915

Please sign in to comment.