var base_reader = undefined function subscribe_link(reader, feed_url){ if(reader == "ttrss"){ return `${base_reader}/public.php?op=subscribe&feed_url=${encodeURIComponent(feed_url)}`; } else { return `${base_reader}/bookmarklet?uri=${encodeURIComponent(feed_url)}`; } } function insertElementsIntoRow(rowElement, elements){ let tdElement = document.createElement("td") elements.forEach( el => tdElement.append(el)) rowElement.append(tdElement) } function insertElementIntoRow(rowElement, element){ let tdElement = document.createElement("td") tdElement.append(element) rowElement.append(tdElement) } function wrapInDiv(element){ let wrapperDiv = document.createElement("div") wrapperDiv.append(element) return wrapperDiv } let itemMap = { "updated": "pubDate", "summary": "description", } function parseCollection(collection, entries){ for(const child of collection){ feedEntry = {"link": "", "title": "", "pubDate": "", "description": ""} for(const c of child.children){ if(c.tagName == "link" && c.getAttribute("href")){ feedEntry["link"] = c.getAttribute("href") } else { feedEntry[c.tagName] = c.innerHTML } } // Copy keys to normalized version for(const key in itemMap){ if(feedEntry[key]){ feedEntry[itemMap[key]] = feedEntry[key] } } entries.push(feedEntry) } } function previewOnclick(item){ return function(){ fetch(item.url) .then( r => r.text() ) .then(r => new window.DOMParser().parseFromString(r, "text/xml")) .then(p => { let entries = [] parseCollection(p.getElementsByTagName("entry"), entries) parseCollection(p.getElementsByTagName("item"), entries) let tableEl = document.createElement("table") tableEl.classList.add("preview") entries.forEach(entry => { let trEl = document.createElement("tr") tableEl.append(trEl) let titleEl = document.createElement("a") titleEl.href = entry["link"] titleEl.innerText = entry["title"] let wrapperDiv2 = document.createElement("div") wrapperDiv2.innerText = entry["pubDate"] insertElementsIntoRow(trEl, [ wrapInDiv(titleEl), wrapperDiv2 ]) let td3El = document.createElement("td") td3El.innerText = entry["description"] trEl.append(td3El) }) feeds.append(tableEl) }) } } window.onload = async function () { let settings = await browser.storage.sync.get(["rb", "reader", "instance"]); base_reader = settings.instance; let base_rb = settings.rb; let feedsElement = document.getElementById('feeds'); feedsElement.innerText = "Loading..." browser.tabs.query({ active: true, currentWindow: true }, function (tabs) { browser.tabs.sendMessage(tabs[0].id, {}).then( function (feeds) { feedsElement.innerText = "" if(!base_rb) { let newSettingsLink = document.createElement("a") newSettingsLink["href"] = "/options.html" newSettingsLink.innerText = "RSS-bridge instance not set! Click here to open options."; let newDiv = document.createElement('div'); newDiv.append(newSettingsLink); feedsElement.appendChild(newDiv); let newP = document.createElement('p') newP.innerText = `Found ${feeds.length} feeds` feedsElement.append(newP) return; } let table = document.createElement("table") table.classList.add("feeds") feedsElement.append(table) feeds.forEach(feed => { let newRow = document.createElement("tr") table.append(newRow) let newLink = document.createElement('a'); newLink["href"] = feed.url newLink.innerText = feed.name; insertElementIntoRow(newRow, newLink) let previewLink = document.createElement('a'); previewLink["onclick"] = previewOnclick(feed) previewLink["href"] = "#" previewLink.innerText = "preview"; let newPreviewData = document.createElement("td") // Preview only works for XML feeds if( feed.type.toLowerCase().includes("rss") || feed.type.toLowerCase().includes("xml") || feed.type.toLowerCase().includes("atom") ){ newPreviewData.append(previewLink) } newRow.append(newPreviewData) if(base_reader){ let subLink = document.createElement('a'); subLink["href"] = subscribe_link(settings.reader, feed.url) subLink.innerText = "subscribe"; let newSubData = document.createElement("td") newSubData.append(subLink) newRow.append(newSubData) } }) if(feeds.length == 0) { let newP = document.createElement('p') newP.innerText = "No feeds found" feedsElement.append(newP) } }); }); }