feat: get page rss

This commit is contained in:
DIYgod 2019-07-18 00:45:13 +08:00
parent 0b5f2f62a6
commit 8f0b814741
No known key found for this signature in database
GPG Key ID: EC0B76A252D3EF67
7 changed files with 102 additions and 15 deletions

View File

@ -1,15 +1,23 @@
{
"manifest_version": 2,
"manifest_version": 2,
"name": "RSSHub radar",
"description": "Detects available RSS and RSSHub",
"version": "1.0",
"name": "RSSHub radar",
"description": "Detect available RSS and RSSHub",
"version": "0.1",
"browser_action": {
"default_icon": "rsshub.png",
"default_popup": "rsshub-radar.html"
},
"permissions": [
"activeTab"
]
"browser_action": {
"default_icon": "rsshub.png",
"default_popup": "popup.html"
},
"content_scripts": [{
"matches": ["https://*/*", "http://*/*"],
"js": ["content.js"]
}],
"background" : {
"scripts": ["background.js"],
"persistent": false
},
"permissions": [
"activeTab"
]
}

View File

@ -1,10 +1,10 @@
<html>
<head>
<title>RSSHub radar</title>
<style href="./dist/rsshub-radar.css"></style>
<style href="./popup.css"></style>
</head>
<body>
<h1>RSSHub radar</h1>
<script src="./dist/rsshub-radar.js"></script>
<script src="./popup.js"></script>
</body>
</html>

23
src/js/background.js Normal file
View File

@ -0,0 +1,23 @@
function handlePageRSS (feeds) {
if (feeds && feeds.length) {
chrome.browserAction.setBadgeText({
text: feeds.length + '',
});
} else {
chrome.browserAction.setBadgeText({
text: '',
});
}
}
chrome.tabs.onActivated.addListener(function (tab) {
chrome.tabs.sendMessage(tab.tabId, {
text: 'getPageRSS',
}, handlePageRSS);
});
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
if (msg.text === 'setPageRSS') {
handlePageRSS(msg.feeds);
}
});

53
src/js/content.js Normal file
View File

@ -0,0 +1,53 @@
function getPageRSS () {
let types = [
'application/rss+xml',
'application/atom+xml',
'application/rdf+xml',
'application/rss',
'application/atom',
'application/rdf',
'text/rss+xml',
'text/atom+xml',
'text/rdf+xml',
'text/rss',
'text/atom',
'text/rdf'
];
let links = document.querySelectorAll('link[type]');
let feeds = [];
for (let i = 0; i < links.length; i++) {
if (links[i].hasAttribute('type') && types.indexOf(links[i].getAttribute('type')) !== -1) {
let feed_url = links[i].getAttribute('href');
if (feed_url.startsWith('//')) {
feed_url = document.location.protocol + feed_url;
} else if (feed_url.startsWith('/')) {
feed_url = document.location.origin + feed_url;
} else if (!(/^(http|https):\/\//i.test(feed_url))) {
feed_url = document.location.href + '/' + feed_url.replace(/^\//g, '');
}
let feed = {
url: feed_url,
title: links[i].getAttribute('title') || feed_url
};
feeds.push(feed);
}
}
return feeds;
}
chrome.runtime.sendMessage(null, {
text: 'setPageRSS',
feeds: getPageRSS(),
});
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
if (msg.text === 'getPageRSS') {
if (document.readyState === 'complete') {
sendResponse(getPageRSS());
} else {
sendResponse(null);
}
}
});

View File

@ -1 +0,0 @@
import '../css/index.less';

2
src/js/popup.js Normal file
View File

@ -0,0 +1,2 @@
import '../css/index.less';

View File

@ -11,7 +11,9 @@ module.exports = {
devtool: 'source-map',
entry: {
'rsshub-radar': './src/js/index.js',
'popup': './src/js/popup.js',
'content': './src/js/content.js',
'background': './src/js/background.js',
},
output: {