1
0
mirror of https://github.com/aureliendavid/rsspreview.git synced 2025-08-29 06:45:37 +00:00

add basic feed detection (#11):

* disabled by default for now
* works on link rel=alternate declarations
This commit is contained in:
Aurelien David 2019-01-19 17:44:24 +01:00
parent 0320efc780
commit aa70671c62
9 changed files with 129 additions and 4 deletions

View File

@ -39,3 +39,17 @@ browser.webRequest.onHeadersReceived.addListener(
{ urls: ['<all_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);

BIN
icons/rss-gray-19.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 405 B

BIN
icons/rss-gray-38.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 820 B

View File

@ -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": ["<all_urls>", "webRequest", "webRequestBlocking", "storage"]
"permissions": ["<all_urls>", "webRequest", "webRequestBlocking", "storage", "tabs"]
}

19
popup/popup.html Normal file
View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="popup.js"></script>
</head>
<body>
<ul id='feedList' style='margin-right: 20px;'>
</ul>
</body>
</html>

24
popup/popup.js Normal file
View File

@ -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);
}
}
});

View File

@ -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);
}
}
})();

View File

@ -14,6 +14,7 @@
<input type="text" id="valMaxWidth" class="validate" >
</label>
<label class="setting browser-style"><input type="checkbox" id="doDetect" class="validate" > Enable feed detection</label>
<script src="options.js"></script>

View File

@ -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);