Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// holhashtags.js
- (function() {
- const HHT_POPUP_EFTER = 1000;
- let hht_popup_timer = null;
- let hht_popup_hash = null;
- let hht_popup_indlaeg = null;
- let hht_touch = false;
- let hht_traad = [];
- const hht_find_hash = (find) => {
- if (find === '#0') return hht_traad[0];
- for (let t of hht_traad) if (t.hash === find) return t;
- return null;
- };
- const hht_fjern_referencer = (node) => {
- let links = [];
- for (let n of node.childNodes)
- {
- if (n.nodeType === 1)
- {
- if ((n.tagName === 'A') && (n.getAttribute('hht_ref') === '1')) links.push(n);
- else hht_fjern_referencer(n);
- }
- }
- for (let l of links) l.parentElement.replaceChild(document.createTextNode(l.innerText), l);
- };
- const hht_gaa_til_hash = (hash) => {
- let h = hash || location.hash;
- if (h === '#top') window.scrollTo(0, 0);
- else if (h === '#bund') window.scrollTo(0, document.body.scrollHeight);
- else if ((h === '#seneste') && (hht_traad.length > 0)) hht_traad[hht_traad.length - 1].ydre.scrollIntoView();
- else {
- let t = hht_find_hash(h);
- if (t) t.ydre.scrollIntoView();
- }
- };
- const hht_indlaes = () => {
- let start = (new Date()).getTime();
- let cts = document.getElementsByClassName('custom-thread');
- for (let ct of cts)
- {
- let ms = ct.getElementsByClassName('main-svar');
- if (ms.length === 1)
- {
- ms = ms[0];
- let tx = ms.getElementsByClassName('svar-text');
- if (tx.length === 1)
- {
- hht_traad.push({ydre:ms, hash:'#0', tekst:tx[0], ref:null, refs:'' });
- let ss = ct.getElementsByClassName('svar-super-container');
- for (let s of ss)
- {
- let h = s.getElementsByClassName('svar-postcount-styling');
- tx = s.getElementsByClassName('svar-text');
- if ((h.length === 1) && (tx.length === 1))
- {
- hht_traad.push({ydre:s, hash:h[0].innerText.trim(), tekst:tx[0], ref:null, refs:''});
- } else console.error('HHT ugyldigt svar indhold');
- }
- } else console.error('HHT ugyldig main svar text');
- break;
- }
- }
- if (hht_traad.length > 0)
- {
- hht_indsaet_html();
- let idx = 0;
- for (let t of hht_traad)
- {
- hht_lav_referencer(t.tekst, t, idx++);
- }
- console.log('HHT tråd længde: ' + hht_traad.length + ', indlæst på ' + ((new Date()).getTime() - start) + 'ms');
- hht_gaa_til_hash();
- window.addEventListener('hashchange', () => { hht_gaa_til_hash((new URL(event.newURL)).hash) });
- }
- else console.log('HHT ingen tråd fundet');
- };
- const hht_indsaet_html = () => {
- let bg = getComputedStyle(document.body).getPropertyValue('background-color');
- let fg = getComputedStyle(document.getElementsByClassName('main-svar-title')[0]).getPropertyValue('color');
- let fs = getComputedStyle(hht_traad[hht_traad.length > 1 ? 1 : 0].tekst).getPropertyValue('font-size');
- let m = hht_moerkt_tema(bg);
- let bdy = m ? '0,0,0,.1' : '255,255,255,.8';
- let hdr = m ? '0,0,0,.4' : '255,255,255,.4';
- let ref = m ? '255,255,255,.5' : '0,0,0,.5';
- let bdr = m ? '255,255,255,.25' : '0,0,0,.25';
- let e = document.createElement('style');
- e.setAttribute('type', 'text/css');
- e.innerText = 'div.hht_refs { font-size:smaller; margin-top:.5em; padding-top:.5em; border-top:.1em solid '+bg+'; }\n' +
- 'span.hht_hdr { color:rgba('+ref+'); font-weight:bold; margin-right:.5em; }\n' +
- 'a.hht_lnk { }\n' +
- 'a.hht_ref { margin-right:.25em; }\n' +
- '#hht_pop_links { position:absolute; margin:0px; background-color:'+bg+'; display:none; border:.1em solid rgba('+bdr+'); padding:.5em 1em; border-radius:5px; z-index:10000; }\n' +
- '#hht_indlaeg { font-size:'+fs+'; background-color:'+bg+'; border:.1em solid rgba('+bdr+'); color:'+fg+'; border-radius:5px; display:none; position:absolute; z-index:10000; animation-duration:.25s; }\n' +
- '#hht_indlaeg_top { background-color:rgba('+hdr+'); padding:5px; border-radius:5px 5px 0px 0px; }\n' +
- '#hht_indlaeg_af { margin-right: .25em; }\n' +
- '#hht_indlaeg_rang { margin-right: .25em; }\n' +
- '#hht_indlaeg_navn { margin-right: 1em; }\n' +
- '#hht_indlaeg_tid { float:right; }\n' +
- '#hht_indlaeg_bund { background-color:rgba('+bdy+'); clear:both; left:0px; max-height:25vh; overflow-y:scroll; padding:5px; border-radius:0px 0px 5px 5px; margin:0px; white-space:pre-wrap; }\n' +
- '@keyframes anim_vis { from:{ opacity:0 } to { opacity:1 } }\n@keyframes anim_gem { from:{ opacity:1 } to { opacity:0 } }\n' +
- '@media screen and (max-width: 600px) { #hht_indlaeg_rang { display:none; } }';
- document.head.appendChild(e);
- e = document.createElement('div');
- e.id = 'hht_pop_links';
- let a = document.createElement('a');
- a.href = 'javascript:void(0)';
- a.innerText = 'Vis';
- a.style.marginRight = '.5em';
- a.addEventListener('click', hht_vis_indlaeg);
- e.appendChild(a);
- a = document.createElement('a');
- a.addEventListener('click', ()=>{ hht_skjul_popup(true); hht_klik_hash(); });
- e.appendChild(a);
- e.addEventListener('mouseleave', hht_skjul_popup);
- document.body.appendChild(e);
- hht_popup_indlaeg = { ydre:null, top:null, rang:null, navn:null, tid:null, bund:null };
- hht_popup_indlaeg.ydre = document.createElement('div');
- hht_popup_indlaeg.ydre.id = 'hht_indlaeg';
- hht_popup_indlaeg.ydre.addEventListener('animationend', () => {
- if (hht_popup_indlaeg.ydre.style.animationName === 'anim_gem') hht_popup_indlaeg.ydre.style.display = 'none';
- else hht_popup_indlaeg.ydre.style.opacity = '1';
- hht_popup_indlaeg.ydre.style.animationName = '';
- });
- hht_popup_indlaeg.top = document.createElement('div');
- hht_popup_indlaeg.top.id = 'hht_indlaeg_top';
- hht_popup_indlaeg.ydre.appendChild(hht_popup_indlaeg.top);
- e = document.createElement('span');
- e.appendChild(document.createTextNode('Af'));
- e.id = 'hht_indlaeg_af';
- hht_popup_indlaeg.top.appendChild(e);
- hht_popup_indlaeg.rang = document.createElement('span');
- hht_popup_indlaeg.rang.id = 'hht_indlaeg_rang';
- hht_popup_indlaeg.top.appendChild(hht_popup_indlaeg.rang);
- hht_popup_indlaeg.navn = document.createElement('span');
- hht_popup_indlaeg.navn.id = 'hht_indlaeg_navn';
- hht_popup_indlaeg.top.appendChild(hht_popup_indlaeg.navn);
- hht_popup_indlaeg.tid = document.createElement('span');
- hht_popup_indlaeg.tid.id = 'hht_indlaeg_tid';
- hht_popup_indlaeg.top.appendChild(hht_popup_indlaeg.tid);
- hht_popup_indlaeg.bund = document.createElement('div');
- hht_popup_indlaeg.bund.id = 'hht_indlaeg_bund';
- hht_popup_indlaeg.ydre.appendChild(hht_popup_indlaeg.bund);
- document.body.appendChild(hht_popup_indlaeg.ydre);
- document.body.addEventListener('mousedown', hht_skjul_indlaeg);
- document.body.addEventListener('touchstart', hht_skjul_indlaeg);
- window.addEventListener('resize', hht_skjul_indlaeg);
- };
- const hht_klik_hash = () => {
- if (hht_touch)
- {
- hht_touch = false;
- hht_vis_popup();
- }
- else hht_gaa_til_hash(event.target.innerText);
- };
- const hht_lav_ref_link = (baseurl, hash) => {
- const start = () => { if (hht_touch) return; stop(); hht_popup_hash = event.target; hht_popup_timer = setTimeout(hht_vis_indlaeg, HHT_POPUP_EFTER); };
- const stop = () => { if (hht_popup_timer) clearTimeout(hht_popup_timer); hht_popup_timer = null; };
- l = document.createElement('a');
- l.appendChild(document.createTextNode(hash));
- l.href = baseurl + hash;
- l.setAttribute('hht_ref', '1');
- l.addEventListener('click', hht_klik_hash);
- l.addEventListener('mouseover', start);
- l.addEventListener('mouseout', stop);
- l.addEventListener('touchstart', () => { hht_touch = true; hht_popup_hash = event.currentTarget; });
- return l;
- };
- const hht_lav_referencer = (node, svar, index) => {
- let baseurl = location.href;
- if (location.hash && (location.hash.length > 0)) baseurl = baseurl.substr(0, baseurl.length - location.hash.length);
- for (let n of node.childNodes)
- {
- if (n.nodeType === 3)
- {
- const re = /\#[0-9]+/g
- let m, h, ref, ne;
- while (n && (m = re.exec(n.nodeValue)))
- {
- h = m[0];
- ref = hht_find_hash(h);
- if (ref == null) continue;
- if (ref.refs.indexOf(h + ',') < 0)
- {
- ref.refs += h + ',';
- if (ref.ref == null)
- {
- ref.ref = document.createElement('div');
- ref.ref.className = 'hht_refs';
- ne = document.createElement('span');
- ne.className = 'hht_hdr';
- ne.innerText = 'Refereret:';
- ref.ref.appendChild(ne);
- ref.tekst.parentElement.appendChild(ref.ref);
- }
- else ref.ref.appendChild(document.createTextNode(' '));
- ref.ref.appendChild(hht_lav_ref_link(baseurl, svar.hash));
- }
- if (m.index > 0) n.parentElement.insertBefore(document.createTextNode(n.nodeValue.substring(0, m.index)), n);
- n.parentElement.insertBefore(hht_lav_ref_link(baseurl, h), n);
- if (re.lastIndex >= n.nodeValue.length)
- {
- n.parentElement.removeChild(n);
- n = null;
- }
- else n.nodeValue = n.nodeValue.substring(re.lastIndex, n.nodeValue.length);
- }
- }
- else if ((n.nodeType === 1) && (n.tagName != 'A')) hht_lav_referencer(n, svar, index);
- }
- };
- const hht_moerkt_tema = (rgb) => {
- let cval;
- if (rgb.indexOf('rgb') >= 0)
- {
- cval = rgb.match(/\d+/g); //rgb.match(/\s*\d+(,|\s)*\d+(,|\s)*\d+(,|\s)*/g);
- for (let i = 0; i < cval.length; i++) cval[i] = parseInt(cval[i]);
- while (cval.length < 3) cval.push(0);
- }
- else if (rgb.indexOf('#') == 0)
- {
- let c = rgb.substr(1);
- while (c.length < 6) c = '0' + c;
- cval = [];
- cval.push(parseInt('0x' + c.substr(0, 2)));
- cval.push(parseInt('0x' + c.substr(2, 2)));
- cval.push(parseInt('0x' + c.substr(4, 2)));
- }
- else return false;
- let r = Math.pow(cval[0] / 255.0, 2.218);
- let g = Math.pow(cval[1] / 255.0, 2.218);
- let b = Math.pow(cval[2] / 255.0, 2.218);
- let l = Math.pow((r * 0.2126) + (g * 0.7156) + (b * 0.0722), 0.68) * 100;
- return l < 40;
- };
- const hht_skjul_indlaeg = () => {
- let et = event.target;
- while (et && et.tagName && (et.tagName.toLowerCase() != 'a'))
- {
- if (et.id === hht_popup_indlaeg.ydre.id) return;
- et = et.parentElement;
- }
- hht_popup_indlaeg.ydre.style.animationName = 'anim_gem';
- };
- const hht_skjul_popup = (tving) => {
- let pl = document.getElementById('hht_pop_links');
- if (tving || (event.target === pl))
- {
- pl.children[0].blur();
- pl.children[1].blur();
- pl.style.display = 'none';
- }
- };
- const hht_vis_indlaeg = () => {
- hht_skjul_popup(true);
- let h = hht_popup_hash.innerText;
- let t = hht_find_hash(h);
- if (t == null) console.log('HHT ukendt indlæg: ' + h);
- let svar = { rang:'', navn:'', tid:'' };
- if (h === '#0')
- {
- let e = t.ydre.getElementsByClassName('main-svar-info-bar');
- if (e && (e.length > 0)) svar.tid = e[0].children[0].innerText;
- e = t.ydre.getElementsByClassName('main-svar-sub-content');
- if (e && (e.length > 0))
- {
- svar.navn = e[0].children[0].innerText;
- svar.rang = e[0].innerText;
- let p = svar.rang.search(svar.navn) + svar.navn.length;
- svar.rang = svar.rang.substring(p, svar.rang.length).trim();
- }
- }
- else
- {
- let e = t.ydre.getElementsByClassName('svar-username');
- if (e && (e.length > 0)) svar.navn = e[0].children[0].innerText;
- e = t.ydre.getElementsByClassName('svar-dato');
- if (e && (e.length > 0)) svar.tid = e[0].children[0].innerText;
- e = t.ydre.getElementsByClassName('svar-rank');
- if (e && (e.length > 0)) svar.rang = e[0].innerText;
- }
- if (svar.navn.length > 0)
- {
- hht_popup_indlaeg.ydre.style.opacity = '0';
- hht_popup_indlaeg.ydre.style.display = 'inline-block';
- hht_popup_indlaeg.rang.innerText = svar.rang;
- hht_popup_indlaeg.navn.innerText = svar.navn;
- hht_popup_indlaeg.tid.innerText = svar.tid;
- hht_popup_indlaeg.bund.innerHTML = t.tekst.innerHTML;
- hht_fjern_referencer(hht_popup_indlaeg.bund);
- hht_popup_indlaeg.bund.scrollTo(0,0);
- let rb = document.body.getBoundingClientRect();
- let rd = hht_traad[0].ydre.getBoundingClientRect();
- let r = hht_popup_hash.getBoundingClientRect();
- let offs = hht_popup_hash.parentElement.className === 'hht_refs' ? -r.height : hht_popup_indlaeg.ydre.offsetHeight;
- let w = rd.width * 0.9;
- hht_popup_indlaeg.ydre.style.width = w + 'px';
- hht_popup_indlaeg.ydre.style.left = (rd.left - rb.left + ((rd.width - w) * 0.5)) + 'px';
- hht_popup_indlaeg.ydre.style.top = (r.top - rb.top - offs) + 'px';
- hht_popup_indlaeg.ydre.style.animationName = 'anim_vis';
- }
- };
- const hht_vis_popup = () => {
- hht_popup_timer = null;
- let rb = document.body.getBoundingClientRect();
- let re = hht_popup_hash.getBoundingClientRect();
- let pl = document.getElementById('hht_pop_links');
- pl.style.top = '-100px';
- pl.style.display = 'inline-block';
- pl.style.fontSize = getComputedStyle(hht_popup_hash).getPropertyValue('font-size');
- let pa = pl.children[1];
- pa.href = hht_popup_hash.href;
- pa.innerText = hht_popup_hash.innerText;
- let pls = getComputedStyle(pl);
- pl.style.left = (re.left - rb.left - pa.offsetLeft - parseFloat(pls.getPropertyValue('border-left-width'))) + 'px';
- pl.style.top = (re.top - rb.top - pa.offsetTop - parseFloat(pls.getPropertyValue('border-top-width'))) + 'px';
- };
- if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', hht_indlaes);
- else hht_indlaes();
- })();
Advertisement
Add Comment
Please, Sign In to add comment