From be9551749104181fca166dbf9b59040e28730d0b Mon Sep 17 00:00:00 2001 From: glhrmv Date: Sun, 16 Dec 2018 23:39:31 +0000 Subject: [PATCH] Format using Prettier --- background.js | 20 ++++-- rsspreview.js | 184 ++++++++++++++++++++++++++------------------------ 2 files changed, 110 insertions(+), 94 deletions(-) diff --git a/background.js b/background.js index 4795eff..1354717 100644 --- a/background.js +++ b/background.js @@ -3,8 +3,11 @@ function detectFeed(event) { let isfeed = false; for (let header of event.responseHeaders) { - if (header.name.toLowerCase() == "content-type") { - header.value = header.value.replace(/application\/(rss|atom)\+xml/,'text/xml'); + if (header.name.toLowerCase() == 'content-type') { + header.value = header.value.replace( + /application\/(rss|atom)\+xml/, + 'text/xml' + ); isfeed = true; break; } @@ -12,7 +15,7 @@ function detectFeed(event) { if (isfeed) { for (let i = 0; i < event.responseHeaders.length; i++) { - if (event.responseHeaders[i].name.toLowerCase() == "cache-control") { + if (event.responseHeaders[i].name.toLowerCase() == 'cache-control') { event.responseHeaders.splice(i, 1); break; } @@ -20,7 +23,10 @@ function detectFeed(event) { // don't cache requests we modified // otherwise on reload the content-type won't be modified again - event.responseHeaders.push({ name : "Cache-Control", value : "no-cache, no-store, must-revalidate" }); + event.responseHeaders.push({ + name: 'Cache-Control', + value: 'no-cache, no-store, must-revalidate', + }); } return { responseHeaders: event.responseHeaders }; @@ -30,6 +36,6 @@ const browser = window.browser || window.chrome; browser.webRequest.onHeadersReceived.addListener( detectFeed, - { urls: [""], types: ["main_frame"] }, - ["blocking", "responseHeaders"] -) + { urls: [''], types: ['main_frame'] }, + ['blocking', 'responseHeaders'] +); diff --git a/rsspreview.js b/rsspreview.js index b998202..f7008cb 100644 --- a/rsspreview.js +++ b/rsspreview.js @@ -1,11 +1,11 @@ (function() { /** - * Check and set a global guard letiable. + * Check and set a global guard variable. * If this content script is injected into the same page again, * it will do nothing next time. */ if (window.hasRun) { - console.log("already run"); + console.log('already run'); return; } @@ -15,7 +15,6 @@ let html_parser = new DOMParser(); function xhrdoc(url, type, cb) { - let xhr = new XMLHttpRequest(); xhr.open('GET', url, true); @@ -25,7 +24,7 @@ xhr.onload = () => { if (xhr.readyState === xhr.DONE) { if (xhr.status === 200) { - let resp = (type=="xml") ? xhr.responseXML : xhr.response; + let resp = type == 'xml' ? xhr.responseXML : xhr.response; cb(resp); } } @@ -34,7 +33,7 @@ xhr.send(null); } - function applyxsl(xmlin, xsl, node, doc=document) { + function applyxsl(xmlin, xsl, node, doc = document) { let xsltProcessor = new XSLTProcessor(); xsltProcessor.importStylesheet(xsl); @@ -45,117 +44,120 @@ function getlang() { if (navigator.languages && navigator.languages[0]) return navigator.languages[0]; - else if (navigator.language) - return navigator.language; - else - return null; + else if (navigator.language) return navigator.language; + else return null; } function formatsubtitle() { try { - let feed_desc = document.getElementById("feedSubtitleRaw"); + let feed_desc = document.getElementById('feedSubtitleRaw'); - let html_desc = html_parser.parseFromString('

'+feed_desc.innerText+'

', "text/html"); + let html_desc = html_parser.parseFromString( + '

' + feed_desc.innerText + '

', + 'text/html' + ); let xml_desc = xml_parser.serializeToString(html_desc.body.firstChild); feed_desc.insertAdjacentHTML('afterend', xml_desc); feed_desc.parentNode.removeChild(feed_desc); - } - catch (e) { + } catch (e) { console.error(e); console.log(feed_desc.innerText); } } - function formatdescriptions(el=document) { + function formatdescriptions(el = document) { // unescapes descriptions to html then to xml - let tohtml = el.getElementsByClassName("feedRawContent"); + let tohtml = el.getElementsByClassName('feedRawContent'); - for (let i = 0; i'+tohtml[i].innerText+'', "text/html"); - let xml_desc = xml_parser.serializeToString(html_desc.body.firstChild); + let html_desc = html_parser.parseFromString( + '
' + tohtml[i].innerText + '
', + 'text/html' + ); + let xml_desc = xml_parser.serializeToString( + html_desc.body.firstChild + ); tohtml[i].insertAdjacentHTML('afterend', xml_desc); - tohtml[i].setAttribute("todel", 1); - } - catch (e) { + tohtml[i].setAttribute('todel', 1); + } catch (e) { console.error(e); console.log(tohtml[i].innerHTML); } } } - el.querySelectorAll('.feedRawContent').forEach((a) => { - if (a.getAttribute("todel") == "1") { + el.querySelectorAll('.feedRawContent').forEach(a => { + if (a.getAttribute('todel') == '1') { a.remove(); + } else if (a.getAttribute('desctype') == 'xhtml') { + a.classList.add('feedEntryContent'); + a.classList.remove('feedRawContent'); } - else if (a.getAttribute("desctype") == "xhtml") { - a.classList.add("feedEntryContent"); - a.classList.remove("feedRawContent"); - } - }) + }); } - function removeemptyenclosures(el=document) { - let encs = el.getElementsByClassName("enclosures"); + function removeemptyenclosures(el = document) { + let encs = el.getElementsByClassName('enclosures'); for (let i = 0; i < encs.length; i++) - if (!encs[i].firstChild) - encs[i].style.display = "none"; + if (!encs[i].firstChild) encs[i].style.display = 'none'; } - function formatfilenames(el=document) { - let encfn = el.getElementsByClassName("enclosureFilename"); + function formatfilenames(el = document) { + let encfn = el.getElementsByClassName('enclosureFilename'); - for (let i = 0; i 0) - i = Math.floor( Math.log(size) / Math.log(1024) ); + if (size && size != '' && size > 0) + i = Math.floor(Math.log(size) / Math.log(1024)); - return ( size / Math.pow(1024, i) ).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i]; - }; + return ( + (size / Math.pow(1024, i)).toFixed(2) * 1 + + ' ' + + ['B', 'kB', 'MB', 'GB', 'TB'][i] + ); + } - let encsz = el.getElementsByClassName("enclosureSize"); - for (let i = 0; i= 0) { - let tmp = document.createElement("span"); + let tmp = document.createElement('span'); try { tmp.innerHTML = et[i].innerText; et[i].innerText = tmp.textContent; - } - catch (e) { + } catch (e) { console.error(e); console.log(et[i].innerText); } @@ -163,67 +165,74 @@ } } - function formatdates(el=document) { + function formatdates(el = document) { let lang = getlang(); - if (!lang) - return; + if (!lang) return; - let opts = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }; + let opts = { + weekday: 'long', + year: 'numeric', + month: 'long', + day: 'numeric', + }; - let ed = el.getElementsByClassName("lastUpdated"); - for (let i = 0; i 0) + isRSS1 = rootNode.attributes['xmlns'].nodeValue.search('rss') > 0; - if ( rootName == "rss" || rootName == "channel" // rss2 - || rootName == "feed" // atom - || isRSS1 ) + if ( + rootName == 'rss' || + rootName == 'channel' || // rss2 + rootName == 'feed' || // atom + isRSS1 + ) return rootNode; return null; @@ -233,11 +242,14 @@ let feed_url = window.location.href; let preview = makepreviewhtml(); - xhrdoc(chrome.extension.getURL("rss.xsl"), "xml", (xsl_xml) => { - applyxsl(feedNode, xsl_xml, preview.getElementById("feedBody"), preview); + xhrdoc(chrome.extension.getURL('rss.xsl'), 'xml', xsl_xml => { + applyxsl(feedNode, xsl_xml, preview.getElementById('feedBody'), preview); // replace the content with the preview document - document.replaceChild(document.importNode(preview.documentElement, true), document.documentElement); + document.replaceChild( + document.importNode(preview.documentElement, true), + document.documentElement + ); let t0 = performance.now(); @@ -254,13 +266,11 @@ let t1 = performance.now(); //console.log("exec in: " + (t1 - t0) + "ms"); - document.title = document.getElementById("feedTitleText").innerText; + document.title = document.getElementById('feedTitleText').innerText; }); } let feedRoot = detect(); - if (feedRoot) - main(feedRoot); - + if (feedRoot) main(feedRoot); })();