diff --git a/src/assets/manifest.json b/src/assets/manifest.json index 61e36a6..ade2f0b 100644 --- a/src/assets/manifest.json +++ b/src/assets/manifest.json @@ -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" + ] } diff --git a/src/assets/rsshub-radar.html b/src/assets/popup.html similarity index 53% rename from src/assets/rsshub-radar.html rename to src/assets/popup.html index 8f50ba4..71154d2 100644 --- a/src/assets/rsshub-radar.html +++ b/src/assets/popup.html @@ -1,10 +1,10 @@ RSSHub radar - +

RSSHub radar

- + \ No newline at end of file diff --git a/src/js/background.js b/src/js/background.js new file mode 100644 index 0000000..b61ae6e --- /dev/null +++ b/src/js/background.js @@ -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); + } +}); diff --git a/src/js/content.js b/src/js/content.js new file mode 100644 index 0000000..3d5d23b --- /dev/null +++ b/src/js/content.js @@ -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); + } + } +}); \ No newline at end of file diff --git a/src/js/index.js b/src/js/index.js deleted file mode 100644 index 513db48..0000000 --- a/src/js/index.js +++ /dev/null @@ -1 +0,0 @@ -import '../css/index.less'; \ No newline at end of file diff --git a/src/js/popup.js b/src/js/popup.js new file mode 100644 index 0000000..dd04969 --- /dev/null +++ b/src/js/popup.js @@ -0,0 +1,2 @@ +import '../css/index.less'; + diff --git a/webpack.config.js b/webpack.config.js index 3b21a44..033a6c2 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -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: {