1
0
mirror of https://github.com/aureliendavid/rsspreview.git synced 2025-08-22 19:28:39 +00:00

replace document instead of redirect - still issues with html descs

This commit is contained in:
Aurelien David 2018-10-28 00:44:12 +02:00
parent 7e94715a05
commit 3acccca403
3 changed files with 195 additions and 22 deletions

View File

@ -4,8 +4,8 @@
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<link rel="stylesheet" href="preview.css"> <link rel="stylesheet" href="preview.css">
<link rel="shortcut icon" type="image/png" href="icons/rss-48.png"> <!--link rel="shortcut icon" type="image/png" href="icons/rss-48.png"-->
<script src="preview.js" ></script> <!--script src="preview.js" ></script-->
</head> </head>
<body> <body>

View File

@ -19,7 +19,7 @@
<div id="feedTitleContainer"> <div id="feedTitleContainer">
<h1 id="feedTitleText" > <h1 id="feedTitleText" >
<a href="{link | atom:link[@rel='alternate']/@href | rss1:link}" target="_blank"> <a href="{link | atom:link[@rel='alternate']/@href | rss1:link}" target="_blank">
<img src="icons/home.png" class="headerIcon" /> <!--img data-src="icons/home.png" class="extImg headerIcon" /-->
<xsl:value-of select="title | atom:title | rss1:title" /> <xsl:value-of select="title | atom:title | rss1:title" />
</a> </a>
</h1> </h1>
@ -60,7 +60,7 @@
<xsl:for-each select="enclosure | atom:link[@rel='enclosure'] | rss1:enclosure"> <xsl:for-each select="enclosure | atom:link[@rel='enclosure'] | rss1:enclosure">
<div class="enclosure"> <div class="enclosure">
<img src="icons/file.png" class="enclosureIcon" /> <img data-src="icons/file.png" class="extImg enclosureIcon" />
<a href="{@url | @href}" target="_blank" class="enclosureFilename"><xsl:value-of select="@url | @href" /></a> <a href="{@url | @href}" target="_blank" class="enclosureFilename"><xsl:value-of select="@url | @href" /></a>
(<xsl:value-of select="@type" />, <span class="enclosureSize"><xsl:value-of select="@length" /></span>) (<xsl:value-of select="@type" />, <span class="enclosureSize"><xsl:value-of select="@length" /></span>)
</div> </div>

View File

@ -11,36 +11,209 @@
window.hasRun = true; window.hasRun = true;
var rootNode = document.getRootNode().documentElement;
// for chrome function xhrdoc(url, type, cb) {
var d = document.getElementById("webkit-xml-viewer-source-xml");
if (d && d.firstChild)
rootNode = d.firstChild;
const rootName = rootNode.nodeName.toLowerCase(); var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'document';
xhr.overrideMimeType('text/' + type);
xhr.onload = function () {
if (xhr.readyState === xhr.DONE) {
if (xhr.status === 200) {
var resp = (type=="xml") ? xhr.responseXML : xhr.response;
cb(resp);
}
}
};
xhr.send(null);
}
var isRSS1 = false;
if (rootName == "rdf" || rootName == "rdf:rdf") { function applyxsl(xmlin, xsl, node, doc=document) {
if (rootNode.attributes['xmlns']) {
isRSS1 = (rootNode.attributes['xmlns'].nodeValue.search('rss') > 0) var xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
var fragment = xsltProcessor.transformToFragment(xmlin, doc);
node.appendChild(fragment);
}
function formatdescriptions() {
// unescapes descriptions to html
var tohtml = document.getElementsByClassName("feedEntryContent");
for (var i = 0; i<tohtml.length; i++) {
tohtml[i].innerHTML = tohtml[i].innerText;
}
var feed_desc = document.getElementById("feedSubtitleText");
feed_desc.innerHTML = feed_desc.innerText;
}
function removeemptyenclosures() {
var encs = document.getElementsByClassName("enclosures");
for (var i = 0; i<encs.length; i++) {
if (!encs[i].firstChild)
encs[i].style.display = "none";
}
}
function formatfilenames() {
var encfn = document.getElementsByClassName("enclosureFilename");
for (var i = 0; i<encfn.length; i++) {
var url = new URL(encfn[i].innerText);
if (url) {
var fn = url.pathname.split("/").pop();
if (fn != "") {
encfn[i].innerText = fn;
}
}
}
}
function formatfilesizes() {
function humanfilesize(size) {
var i = 0;
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];
};
var encsz = document.getElementsByClassName("enclosureSize");
for (var i = 0; i<encsz.length; i++) {
var hsize = humanfilesize(encsz[i].innerText);
if (hsize) {
encsz[i].innerText = hsize;
}
} }
} }
if ( rootName == "rss" || rootName == "channel" // rss2 function addfeedurl(url) {
|| rootName == "feed" // atom
|| isRSS1 ) {
var feed_url = window.location.href; var h1 = document.getElementById("feedTitleText");
h1.innerHTML += ' :: <a href="'+url+'"><img src="'+chrome.extension.getURL("icons/rss-32.png")+'" class="headerIcon" />Feed URL</a>';
}
var url = "preview.html?" + encodeURIComponent(feed_url); function detect() {
url = chrome.extension.getURL(url);
// redirect to preview page with feed url as query string var rootNode = document.getRootNode().documentElement;
window.location.replace(url);
// for chrome
var d = document.getElementById("webkit-xml-viewer-source-xml");
if (d && d.firstChild)
rootNode = d.firstChild;
const rootName = rootNode.nodeName.toLowerCase();
var isRSS1 = false;
if (rootName == "rdf" || rootName == "rdf:rdf") {
if (rootNode.attributes['xmlns']) {
isRSS1 = (rootNode.attributes['xmlns'].nodeValue.search('rss') > 0)
}
}
if ( rootName == "rss" || rootName == "channel" // rss2
|| rootName == "feed" // atom
|| isRSS1 ) {
return rootNode;
}
return null;
} }
function extensionimages() {
var extimgs = document.getElementsByClassName("extImg");
for (var i = 0; i<extimgs.length; i++) {
extimgs[i].src = chrome.extension.getURL(extimgs[i].attributes['data-src'].nodeValue);
}
}
function makepreviewhtml() {
var doc = document.implementation.createHTMLDocument("");
var feedBody = doc.createElement("div");
feedBody.id = "feedBody";
doc.body.appendChild(feedBody);
var css = doc.createElement('link');
css.setAttribute('rel', 'stylesheet');
css.setAttribute('href', chrome.extension.getURL("preview.css"));
doc.head.appendChild(css);
return doc;
}
function main(feedNode) {
var feed_url = window.location.href;
var preview = makepreviewhtml();
xhrdoc(chrome.extension.getURL("rss.xsl"), "xml", function(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);
window.document = preview;
console.log(preview);
console.log(window.document);
console.log(document);
removeemptyenclosures();
//formatdescriptions();
formatfilenames();
formatfilesizes();
extensionimages();
document.title = /*"RSSPreview: " + */document.getElementById("feedTitleText").innerText;
//addfeedurl(feed_url);
});
}
var feedRoot = detect();
if (feedRoot)
main(feedRoot);
})(); })();