From aa70671c62ea9fabe0f07605bfa260124b8e0c2d Mon Sep 17 00:00:00 2001 From: Aurelien David Date: Sat, 19 Jan 2019 17:44:24 +0100 Subject: [PATCH] add basic feed detection (#11): * disabled by default for now * works on link rel=alternate declarations --- background.js | 14 +++++++++++ icons/rss-gray-19.png | Bin 0 -> 405 bytes icons/rss-gray-38.png | Bin 0 -> 820 bytes manifest.json | 13 ++++++++-- popup/popup.html | 19 +++++++++++++++ popup/popup.js | 24 ++++++++++++++++++ rsspreview.js | 55 ++++++++++++++++++++++++++++++++++++++++++ settings/options.html | 1 + settings/options.js | 7 ++++-- 9 files changed, 129 insertions(+), 4 deletions(-) create mode 100644 icons/rss-gray-19.png create mode 100644 icons/rss-gray-38.png create mode 100644 popup/popup.html create mode 100644 popup/popup.js diff --git a/background.js b/background.js index 1354717..67631a6 100644 --- a/background.js +++ b/background.js @@ -39,3 +39,17 @@ browser.webRequest.onHeadersReceived.addListener( { urls: [''], types: ['main_frame'] }, ['blocking', 'responseHeaders'] ); + + +function handleMessage(request, sender, sendResponse) { + + let popup = new URL(browser.runtime.getURL('popup/popup.html')); + popup.searchParams.set('feeds', JSON.stringify(request)); + + browser.pageAction.setPopup( {tabId: sender.tab.id, popup: popup.toString() }); + browser.pageAction.show(sender.tab.id); + + //sendResponse({response: "Response from background script to tab " + sender.tab.url , id: sender.tab.id }); +} + +browser.runtime.onMessage.addListener(handleMessage); diff --git a/icons/rss-gray-19.png b/icons/rss-gray-19.png new file mode 100644 index 0000000000000000000000000000000000000000..80894c49e8af95ac9f7dd9908c030c877ed2c6bc GIT binary patch literal 405 zcmV;G0c!q>~W^G%}75)lX%yk9t;*Q30Ilx?fTESa90gtJTUq)jAS^VHO%Q4IcdWlgR&(Jaw zv#paaY*UH91$?CP{*K1-2op^=pEW3#af#F2bBo!yHE|xp8lnWetS!Q9kPu-aM2HeI7VX!x>IB`gyNLJN`*?Z(ZwvIfCgIs7pz*S)xjb*Wi2*gH;(2uzM_z}{{*WO>UA)m z)sem*f-s3KID^lqOBxms0>fdVNG7unFH)NnEF^?RgDG+X`|}x1W5U7~wuG%%l}QN0 zfm6AFf-ztbA)~C-zFx~z0knU|VUnU;`1srPL?+6#gg>0+Z*|ht^ znuuuH&SyWfj}M)CgKAq*JQ!vJy@d}slzlmsY3`2xW?kLK!EWJyS=DKi_+~IHVumj> z73sIl_}AUtb-It$Tv-gTw=QspO+hv}ROeKq0*b3G$ z-nF0Ga~r4W-VSyC_JS(A(_qtfz`Wk326hmQPJcq&amRR`e_LSj{PGvDB728)3Cv$;PbBHauyvLN;5o8mqG!8?#`r!X_+2i?Fr_ z>Ku72r<4w(HM4~I{kD+UV1-4*ZF^8ccZ#e`n4U7&OyL6JDQfmE5zJ literal 0 HcmV?d00001 diff --git a/manifest.json b/manifest.json index fc7b050..5bb74d1 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 2, "name": "RSSPreview", - "version": "2.8", + "version": "3.0", "author": "Aurelien David", "homepage_url": "https://github.com/aureliendavid/rsspreview", @@ -36,12 +36,21 @@ "page": "settings/options.html" }, + "page_action": { + "browser_style": true, + "default_icon": { + "19": "icons/rss-gray-19.png", + "38": "icons/rss-gray-38.png" + }, + "default_title": "Feeds in page" + }, + "applications": { "gecko": { "id": "{7799824a-30fe-4c67-8b3e-7094ea203c94}" } }, - "permissions": ["", "webRequest", "webRequestBlocking", "storage"] + "permissions": ["", "webRequest", "webRequestBlocking", "storage", "tabs"] } diff --git a/popup/popup.html b/popup/popup.html new file mode 100644 index 0000000..84eafac --- /dev/null +++ b/popup/popup.html @@ -0,0 +1,19 @@ + + + + + + + + + + + +
    + +
+ + + + + diff --git a/popup/popup.js b/popup/popup.js new file mode 100644 index 0000000..7f6d6d7 --- /dev/null +++ b/popup/popup.js @@ -0,0 +1,24 @@ +document.addEventListener("DOMContentLoaded", function(event) { + + + const feedList = document.getElementById('feedList'); + + const url = new URL(location.href); + const feeds = JSON.parse(url.searchParams.get('feeds')); + + for (feed_url in feeds) { + if (feeds.hasOwnProperty(feed_url)) { + let li = document.createElement("li"); + let a = document.createElement("a"); + + a.href = feed_url; + a.target = "_blank"; + a.innerText = feeds[feed_url]; + + li.appendChild(a); + feedList.appendChild(li); + } + } + +}); + diff --git a/rsspreview.js b/rsspreview.js index d0bcddf..39440e5 100644 --- a/rsspreview.js +++ b/rsspreview.js @@ -311,4 +311,59 @@ let feedRoot = detect(); if (feedRoot) main(feedRoot); + else { + + function onResult(options) { + if (options.doDetect) + findFeeds(); + } + + function onError(error) { + console.log(`Error: ${error}`); + } + + let getting = browser.storage.sync.get({ + doDetect: false + }); + getting.then(onResult, onError); + } + + + + function findFeeds() { + + let feeds = {}; + + document.querySelectorAll("link[rel='alternate']").forEach( (elem) => { + + let type = elem.getAttribute('type').toLowerCase(); + if (type.includes('rss') || type.includes('atom') || type.includes('feed')) { + + let title = elem.getAttribute('title'); + let url = elem.href; + + if (url) { + + feeds[url] = (title ? title : url); + + //console.log("Feed: " + (title ? (title + " - ") : "") + url); + } + } + }) + + if (Object.keys(feeds).length > 0) { + + function handleResponse(message) { + } + + function handleError(error) { + //console.log(error); + } + + browser.runtime.sendMessage(feeds).then(handleResponse, handleError); + } + + } + + })(); diff --git a/settings/options.html b/settings/options.html index dc57cfd..2bf2fd7 100644 --- a/settings/options.html +++ b/settings/options.html @@ -14,6 +14,7 @@ + diff --git a/settings/options.js b/settings/options.js index 345c1eb..9d88120 100644 --- a/settings/options.js +++ b/settings/options.js @@ -5,7 +5,8 @@ function saveOptions(e) { browser.storage.sync.set({ doThumb: document.querySelector("#doThumb").checked, doMaxWidth: document.querySelector("#doMaxWidth").checked, - valMaxWidth: document.querySelector("#valMaxWidth").value + valMaxWidth: document.querySelector("#valMaxWidth").value, + doDetect: document.querySelector("#doDetect").checked }); } @@ -17,6 +18,7 @@ function restoreOptions() { document.querySelector("#doThumb").checked = result.doThumb; document.querySelector("#doMaxWidth").checked = result.doMaxWidth; document.querySelector("#valMaxWidth").value = result.valMaxWidth; + document.querySelector("#doDetect").checked = result.doDetect; } function onError(error) { @@ -26,7 +28,8 @@ function restoreOptions() { var getting = browser.storage.sync.get({ doThumb: false, doMaxWidth: true, - valMaxWidth: "900px" + valMaxWidth: "900px", + doDetect: false }); getting.then(onResult, onError);