diff options
Diffstat (limited to 'app/src/web/assets/js/context_a.ts')
-rw-r--r-- | app/src/web/assets/js/context_a.ts | 108 |
1 files changed, 72 insertions, 36 deletions
diff --git a/app/src/web/assets/js/context_a.ts b/app/src/web/assets/js/context_a.ts index 16ed33a9..06d2f4a2 100644 --- a/app/src/web/assets/js/context_a.ts +++ b/app/src/web/assets/js/context_a.ts @@ -2,8 +2,78 @@ * Context menu for links * Largely mimics click_a.js */ + (function () { let longClick = false; + + /** + * Given event and target, return true if handled and false otherwise. + */ + type EventHandler = (e: Event, target: Element) => Boolean + + /** + * Posts should click a tag, with two parents up being div.story_body_container + */ + const _frostCopyPost: EventHandler = (e, target) => { + if (target.tagName != 'A') { + return false; + } + const parent1 = target.parentElement; + if (!parent1 || parent1.tagName != 'DIV') { + return false; + } + const parent2 = parent1.parentElement; + if (!parent2 || !parent2.classList.contains('story_body_container')) { + return false; + } + const url = target.getAttribute('href')!; + const text = parent1.innerText; + Frost.contextMenu(url, text); + return true; + }; + + const _frostImage: EventHandler = (e, target) => { + let element: Element = target; + // Notifications are two layers under + for (let i = 0; i < 2; i++) { + if (element.tagName != 'A') { + element = <Element>element.parentElement; + } + } + if (element.tagName != 'A') { + return false + } + const url = element.getAttribute('href'); + if (!url || url == '#') { + return false + } + const text = (<HTMLElement>element.parentElement).innerText; + // Check if image item exists, first in children and then in parent + let image = element.querySelector("[style*=\"background-image: url(\"]"); + if (!image) { + image = (<Element>element.parentElement).querySelector("[style*=\"background-image: url(\"]") + } + if (image) { + const imageUrl = (<String>window.getComputedStyle(image, null).backgroundImage).trim().slice(4, -1); + console.log(`Context image: ${imageUrl}`); + Frost.loadImage(imageUrl, text); + return true + } + // Check if true img exists + const img = element.querySelector("img[src*=scontent]"); + if (img instanceof HTMLMediaElement) { + const imgUrl = img.src; + console.log(`Context img: ${imgUrl}`); + Frost.loadImage(imgUrl, text); + return true + } + console.log(`Context content ${url} ${text}`); + Frost.contextMenu(url, text); + return true + }; + + const handlers = [_frostCopyPost, _frostImage]; + const _frostAContext = (e: Event) => { Frost.longClick(true); longClick = true; @@ -16,46 +86,12 @@ console.log("No element found"); return } - let element: Element = target; - // Notifications are two layers under - for (let i = 0; i < 2; i++) { - if (element.tagName != 'A') { - element = <Element>element.parentElement; - } - } - if (element.tagName == 'A') { - const url = element.getAttribute('href'); - if (!url || url == '#') { - return - } - const text = (<HTMLElement>element.parentElement).innerText; - // Check if image item exists, first in children and then in parent - let image = element.querySelector("[style*=\"background-image: url(\"]"); - if (!image) { - image = (<Element>element.parentElement).querySelector("[style*=\"background-image: url(\"]") - } - if (image) { - const imageUrl = (<String>window.getComputedStyle(image, null).backgroundImage).trim().slice(4, -1); - console.log(`Context image: ${imageUrl}`); - Frost.loadImage(imageUrl, text); - e.stopPropagation(); - e.preventDefault(); - return - } - // Check if true img exists - const img = element.querySelector("img[src*=scontent]"); - if (img instanceof HTMLMediaElement) { - const imgUrl = img.src; - console.log(`Context img: ${imgUrl}`); - Frost.loadImage(imgUrl, text); + for (const h of handlers) { + if (h(e, target)) { e.stopPropagation(); e.preventDefault(); return } - console.log(`Context content ${url} ${text}`); - Frost.contextMenu(url, text); - e.stopPropagation(); - e.preventDefault(); } }; |