Skip to content

Commit

Permalink
ABC Stumble!
Browse files Browse the repository at this point in the history
  • Loading branch information
phocks committed Apr 30, 2019
0 parents commit f9e3492
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.cache
dist
node_modules
.DS_Store
8 changes: 8 additions & 0 deletions browser-polyfill.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added icons/abc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"manifest_version": 2,
"name": "ABC Stumble",
"version": "1.0.0",

"description": "Loads latest ABC News",

"icons": {
"48": "icons/abc.png"
},
"browser_action": {
"default_icon": "icons/abc.png",
"default_title": "ABC Stumble"
},
"background": {
"scripts": ["browser-polyfill.js", "dist/background.js"]
},
"permissions": ["alarms"]
}
134 changes: 134 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "abc-stumble",
"version": "1.0.0",
"description": "Stumble the ABC",
"main": "dist/background.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "parcel build src/background.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"@abcnews/capi-fetch": "^1.3.0",
"fast-xml-parser": "^3.12.16",
"regenerator-runtime": "^0.13.2"
}
}
48 changes: 48 additions & 0 deletions src/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const parser = require('fast-xml-parser');

let stories;
let openedStories = [];

const getLatest = () => {
// Fetches latest story and sets it in global
fetch('https://newsy.glitch.me/api/rss')
.then(res => res.text())
.then(text => {
const jsonObj = parser.parse(text);
stories = jsonObj.rss.channel.item;
});
};

function onCreated(tab) {}

function onError(error) {
console.log(`Error: ${error}`);
}

browser.browserAction.onClicked.addListener(function() {
const getNextStoryLink = (stories, alreadyOpened) => {
for (let story of stories) {
if (alreadyOpened.includes(story.link)) continue;
else return story.link;
}
};

const nextStory = getNextStoryLink(stories, openedStories);

openedStories.push(nextStory);

browser.tabs
.create({
url: nextStory
})
.then(onCreated, onError);
});

browser.alarms.create('get-stories', { periodInMinutes: 5 });

browser.alarms.onAlarm.addListener(alarmInfo => {
getLatest();
});

// Initial grab of stories on load
getLatest();

0 comments on commit f9e3492

Please sign in to comment.