Skip to content

Commit

Permalink
add basic feed detection (#11):
Browse files Browse the repository at this point in the history
* disabled by default for now
* works on link rel=alternate declarations
  • Loading branch information
aureliendavid committed Jan 19, 2019
1 parent 0320efc commit aa70671
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 4 deletions.
14 changes: 14 additions & 0 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Binary file added icons/rss-gray-19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/rss-gray-38.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 11 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",

Expand Down Expand Up @@ -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 changes: 19 additions & 0 deletions popup/popup.html
Original file line number Diff line number Diff line change
@@ -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 changes: 24 additions & 0 deletions popup/popup.js
Original file line number Diff line number Diff line change
@@ -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);
}
}

});

55 changes: 55 additions & 0 deletions rsspreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}


})();
1 change: 1 addition & 0 deletions settings/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
7 changes: 5 additions & 2 deletions settings/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}

Expand All @@ -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) {
Expand All @@ -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);

Expand Down

0 comments on commit aa70671

Please sign in to comment.