Skip to content

Commit aa56711

Browse files
committed
Disabled icon update and counting function (some errors on websites #19)
1 parent 92dbca8 commit aa56711

File tree

3 files changed

+155
-90
lines changed

3 files changed

+155
-90
lines changed

js/background.js

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11

2+
chrome.tabs.onActivated.addListener(function(activeInfo) {
3+
updateIcon(activeInfo.tabId);
4+
});
5+
6+
//listen for current tab to be changed
27
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
3-
/* chrome.browserAction.setIcon({path: {"48": "/img/icon_grey-48.png"}, tabId: tabId});
4-
chrome.browserAction.setBadgeText({text: "", tabId: tabId}); */
8+
updateIcon(tabId);
9+
});
510

6-
var nbFeeds = 0;
11+
function updateIcon(tabId) {
12+
chrome.tabs.get(tabId, function(change){
713

8-
if (changeInfo.status == 'complete' && tab.active) {
14+
chrome.tabs.get(tabId, function(tab){
15+
var url = tab.url;
916

10-
getFeedsURLs(tab.url, function(feeds){
17+
/*getFeedsURLs(url, function(feeds){
1118
12-
nbFeeds = feeds.length;
19+
nbFeeds = feeds.length;
1320
14-
// console.log(nbFeeds);
21+
// console.log('nbFeeds (bg) : '+nbFeeds);
1522
16-
if (nbFeeds == 0) {
17-
chrome.browserAction.setIcon({path: {"48": "/img/icon_grey-48.png"}, tabId: tabId});
18-
chrome.browserAction.setBadgeText({text: "", tabId: tabId});
19-
}
20-
else {
21-
chrome.browserAction.setIcon({path: {"48": "/img/icon_default-48.png"}, tabId: tabId});
22-
chrome.browserAction.setBadgeText({text: nbFeeds.toString(), tabId: tabId});
23-
}
23+
if (nbFeeds == 0) {
24+
chrome.browserAction.setIcon({path: {"48": "/img/icon_grey-48.png"}, tabId: tabId});
25+
chrome.browserAction.setBadgeText({text: "", tabId: tabId});
26+
}
27+
else {
28+
chrome.browserAction.setIcon({path: {"48": "/img/icon_default-48.png"}, tabId: tabId});
29+
chrome.browserAction.setBadgeText({text: nbFeeds.toString(), tabId: tabId});
30+
}
2431
32+
});*/
2533
});
26-
27-
}
28-
})
34+
});
35+
};

js/functions.js

Lines changed: 127 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ function getFeedsURLs(url, callback) {
99
// Check if it's a Reddit URL
1010
var rdRss = getRedditRss(url);
1111

12+
// Check if it's a Kickstarter URL
13+
var ksRss = getKickstarterRss(url);
14+
1215
if (ytRss !== false)
1316
{
1417
var feeds_urls = [];
@@ -37,83 +40,108 @@ function getFeedsURLs(url, callback) {
3740

3841
callback(feeds_urls);
3942
}
43+
else if (ksRss !== false)
44+
{
45+
var feeds_urls = [];
46+
47+
var feed = {
48+
type: '',
49+
url: ksRss,
50+
title: ksRss,
51+
};
52+
53+
feeds_urls.push(feed);
54+
55+
callback(feeds_urls);
56+
}
4057
else
4158
{
42-
var x = new XMLHttpRequest();
43-
x.open('GET', url);
44-
45-
x.responseType = '';
46-
x.onload = function() {
47-
var response = x.response;
48-
49-
var feeds_urls = [];
50-
var types = [
51-
'application/rss+xml',
52-
'application/atom+xml',
53-
'application/rdf+xml',
54-
'application/rss',
55-
'application/atom',
56-
'application/rdf',
57-
'text/rss+xml',
58-
'text/atom+xml',
59-
'text/rdf+xml',
60-
'text/rss',
61-
'text/atom',
62-
'text/rdf'
63-
];
64-
65-
document.getElementById('rss-feed-url_response').innerHTML = response;
66-
67-
var links = document.getElementById('rss-feed-url_response').querySelectorAll("#rss-feed-url_response link[type]");
68-
69-
document.getElementById('rss-feed-url_response').innerHTML = '';
70-
71-
for (var i = 0; i < links.length; i++)
72-
{
73-
if (links[i].hasAttribute('type') && types.indexOf(links[i].getAttribute('type')) !== -1)
74-
{
75-
feed_url = links[i].getAttribute('href');
76-
77-
// If feed's url starts with "//"
78-
if (feed_url.indexOf("//") == 0)
79-
feed_url = "http:" + feed_url;
80-
// If feed's url starts with "/"
81-
else if (feed_url.startsWith('/'))
82-
feed_url = url.split('/')[0] + '//' + url.split('/')[2] + feed_url;
83-
// If feed's url starts with http or https
84-
else if (/^(http|https):\/\//i.test(feed_url))
85-
feed_url = feed_url;
86-
// If feed's has no slash
87-
else if (!feed_url.match(/\//))
88-
feed_url = url.substr(0, url.lastIndexOf("/")) + '/' + feed_url;
89-
else
90-
feed_url = url + "/" + feed_url.replace(/^\//g, '');
91-
92-
var feed = {
93-
type: links[i].getAttribute('type'),
94-
url: feed_url,
95-
title: links[i].getAttribute('title') || feed_url
96-
};
97-
98-
feeds_urls.push(feed);
99-
}
59+
var xhr = new XMLHttpRequest();
60+
xhr.onreadystatechange = function() {
61+
if (xhr.readyState == XMLHttpRequest.DONE) {
62+
var urlContent = xhr.responseText;
63+
64+
if (urlContent != '')
65+
document.getElementById('rss-feed-url_response').innerHTML = urlContent;
66+
67+
searchFeed(url, callback);
10068
}
69+
}
70+
xhr.open('GET', url, true);
71+
xhr.send();
72+
}
73+
}
10174

102-
if (feeds_urls.length === 0)
75+
/**
76+
* Search RSS Feed in source code
77+
*/
78+
function searchFeed(url, callback)
79+
{
80+
if (document.getElementById('rss-feed-url_response').innerHTML != '')
81+
{
82+
var feeds_urls = [];
83+
var types = [
84+
'application/rss+xml',
85+
'application/atom+xml',
86+
'application/rdf+xml',
87+
'application/rss',
88+
'application/atom',
89+
'application/rdf',
90+
'text/rss+xml',
91+
'text/atom+xml',
92+
'text/rdf+xml',
93+
'text/rss',
94+
'text/atom',
95+
'text/rdf'
96+
];
97+
98+
var links = document.getElementById('rss-feed-url_response').querySelectorAll("#rss-feed-url_response link[type]");
99+
100+
document.getElementById('rss-feed-url_response').innerHTML = '';
101+
102+
for (var i = 0; i < links.length; i++)
103+
{
104+
if (links[i].hasAttribute('type') && types.indexOf(links[i].getAttribute('type')) !== -1)
103105
{
104-
var test_feed = tryToGetFeedURL(url);
105-
if (test_feed !== null)
106-
feeds_urls.push(test_feed);
106+
feed_url = links[i].getAttribute('href');
107+
108+
// If feed's url starts with "//"
109+
if (feed_url.indexOf("//") == 0)
110+
feed_url = "http:" + feed_url;
111+
// If feed's url starts with "/"
112+
else if (feed_url.startsWith('/'))
113+
feed_url = url.split('/')[0] + '//' + url.split('/')[2] + feed_url;
114+
// If feed's url starts with http or https
115+
else if (/^(http|https):\/\//i.test(feed_url))
116+
feed_url = feed_url;
117+
// If feed's has no slash
118+
else if (!feed_url.match(/\//))
119+
feed_url = url.substr(0, url.lastIndexOf("/")) + '/' + feed_url;
120+
else
121+
feed_url = url + "/" + feed_url.replace(/^\//g, '');
122+
123+
var feed = {
124+
type: links[i].getAttribute('type'),
125+
url: feed_url,
126+
title: links[i].getAttribute('title') || feed_url
127+
};
128+
129+
feeds_urls.push(feed);
107130
}
131+
}
108132

109-
callback(feeds_urls);
110-
};
111-
112-
x.onerror = function() {
113-
render('Unable to find feed');
114-
};
133+
if (feeds_urls.length === 0)
134+
{
135+
var test_feed = tryToGetFeedURL(url);
136+
if (test_feed !== null)
137+
feeds_urls.push(test_feed);
138+
}
115139

116-
x.send();
140+
callback(feeds_urls);
141+
}
142+
else
143+
{
144+
render('Unable to find feed');
117145
}
118146
}
119147

@@ -187,6 +215,34 @@ function getRedditRss(tabUrl)
187215
}
188216

189217

218+
/*
219+
* Get RSS feed URL of kickstarter
220+
*/
221+
function getKickstarterRss(tabUrl)
222+
{
223+
// Check if subreddit URL
224+
var regex = RegExp(/^(http(s)?:\/\/)?((w){3}.)?kickstarter\.com/gm);
225+
226+
var isKickstarterUrl = regex.test(tabUrl);
227+
228+
if (isKickstarterUrl)
229+
{
230+
// Remove last "/" if presents
231+
var feedUrl = tabUrl.replace(/\/$/, '');
232+
feedUrl = feedUrl + '.atom';
233+
234+
if (feedUrl != tabUrl)
235+
return feedUrl;
236+
else
237+
return false;
238+
}
239+
else
240+
{
241+
return false;
242+
}
243+
}
244+
245+
190246
/**
191247
* Prints message in #feeds
192248
*/
@@ -263,14 +319,15 @@ function tryToGetFeedURL(tabUrl) {
263319

264320
var xhr = new XMLHttpRequest();
265321
xhr.onreadystatechange = function(){
266-
if (xhr.readyState == 4 && xhr.status == 200) {
322+
if (xhr.readyState == XMLHttpRequest.DONE) {
267323
return xhr.responseText;
268324
}
269325
};
270326
xhr.open("GET", feed_url, false);
271327
xhr.send();
272328

273329
var urlContent = xhr.responseText;
330+
274331
if (xhr.status != 404 && urlContent != '')
275332
{
276333
var oParser = new DOMParser();

manifest.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifest_version": 2,
33
"name": "Get RSS Feed URL",
44
"description": "Retreive RSS feeds URLs from WebSite",
5-
"version": "1.4.0",
5+
"version": "1.4.1",
66
"author": "@shevabam",
77
"homepage_url": "https://github.com/shevabam/get-rss-feed-url-extension",
88
"permissions": [
@@ -17,7 +17,8 @@
1717
"default_title": "Click to view RSS feeds for this page"
1818
},
1919
"background": {
20-
"page": "background.html"
20+
"page": "background.html",
21+
"persistent": false
2122
},
2223
"icons": {
2324
"128" : "img/icon_128.png"

0 commit comments

Comments
 (0)