Last active
October 30, 2024 08:05
-
-
Save molhanec/730d92ef449fa7b39c32eb198a146cd7 to your computer and use it in GitHub Desktop.
Okoun: Zobrazí předchozí příspěvek
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Okoun: zobraz predchozi | |
// @namespace http://molhanec.net/lopuch/?n=Main.Okoun | |
// @description Zobrazi na co je reagovano | |
// @version 1.6 | |
// @match https://*.okoun.cz/* | |
// @grant none | |
// ==/UserScript== | |
// Author huh based on Johny_G code | |
// v 1.1 - cachovani (huhlal) | |
// v 1.2 - lepsi podpora smazanych prispevku (huhlal) | |
// v 1.3 - oprava OberSturmKlippFuhrer https://www.okoun.cz/boards/grease_monkey?contextId=1072015522#article-1072015522 | |
// v 1.4 - rewritten (huhlal) | |
// v 1.5 - fix flicker (huhlal) | |
// v 1.6 - uprava @match | |
// konstanty urcujici odstup citace od odkazu | |
const offX = 20 | |
const offY = 20 | |
// sirka scrollbaru | |
const scrBarWidth = 17 | |
// cache pro prispevky z predchozich stranek | |
const cache = {} | |
try { | |
// vytvori okno pro citaci | |
const floatTip = document.createElement("div") | |
floatTip.style = ` | |
position: fixed; | |
width: 640px; | |
text-align: left; | |
background-color: white; | |
border: 2px solid black; | |
overflow: hidden; | |
` | |
function moveReplied(target){ | |
try { | |
const offset = target.getBoundingClientRect() | |
let moveLeft = offset.left + offX | |
let moveTop = offset.top + offY | |
// kdyby prispevek presahoval dolu, posunu ho vys; nikdy ho vsak nenecham pretect nahoru | |
if ((moveTop + floatTip.offsetHeight) > window.innerHeight) moveTop = window.innerHeight - floatTip.offsetHeight | |
if (moveTop < 0) moveTop = 0 | |
// ekvivalent pro preteceni vpravo | |
if ((moveLeft + floatTip.offsetWidth + scrBarWidth) > window.innerWidth) moveLeft = window.innerWidth - floatTip.offsetWidth - scrBarWidth | |
if (moveLeft < 0) moveLeft = 0 | |
// presunuti citace | |
floatTip.style.left = moveLeft + "px" | |
floatTip.style.top = moveTop + "px" | |
} | |
catch (exception) { | |
alert(exception.message) | |
} | |
} | |
function hideReplied(event) { | |
var offset = floatTip.getBoundingClientRect(); | |
if (event.clientX < offset.left || event.clientX > offset.right || event.clientY < offset.top || event.clientY > offset.bottom) { | |
floatTip.removeEventListener("mouseout", hideReplied) | |
try { | |
document.body.removeChild(floatTip) | |
} | |
catch(exception) { | |
// pass | |
} | |
} | |
} | |
function findPostInPage(url, documentInstance = document) { | |
const links = documentInstance.querySelectorAll('a.date') | |
const link = Array.from(links).find(link => url.endsWith(link.href)) | |
if (!link) return | |
const parent = link.parentNode.parentNode.parentNode | |
return parent.querySelector('.content') | |
} | |
async function getPost(target) { | |
let message = "Příspěvek nebyl nalezen. Buď byl smazán nebo stávkuje Okoun, zkuste najet na odkaz znovu." | |
try { | |
const response = await fetch(target.href) | |
if (response.ok) { | |
const html = await response.text() | |
const content = findPostInPage(target.href, Document.parseHTMLUnsafe(html)) | |
if (content) { | |
message = content.innerHTML | |
cache[target.href] = content | |
} | |
} | |
} | |
catch { | |
// pass | |
} | |
floatTip.innerHTML = message | |
moveReplied(target) | |
} | |
function showReplied(event) { | |
const url = event.currentTarget.href | |
try { | |
const content = findPostInPage(url) ?? cache[url] | |
if (content) { | |
floatTip.innerHTML = content.innerHTML | |
} else { | |
floatTip.innerHTML = "Stahuji post..." | |
getPost(event.currentTarget) | |
} | |
floatTip.addEventListener("mouseout", hideReplied) | |
document.body.appendChild(floatTip) | |
moveReplied(event.currentTarget) | |
} | |
catch (exception) { | |
alert(exception.message) | |
} | |
} | |
for (const reply of document.querySelectorAll('a.prev')) { | |
reply.addEventListener("mouseover", showReplied) | |
reply.addEventListener("mouseout", hideReplied) | |
} | |
} | |
catch (exception) { | |
alert(exception.message) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment