Skip to content

Commit

Permalink
Merge pull request #4 from phocks/new-story-count
Browse files Browse the repository at this point in the history
New story count
  • Loading branch information
phocks authored May 16, 2019
2 parents dceb53f + 1b67f71 commit 89bc02f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 13 deletions.
5 changes: 5 additions & 0 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
<html>
<head>
<meta charset="utf-8" />
<style>

</style>
</head>
<body>
<form>
<label>Open articles in new tab: <input type="text" id="newtab"/></label>
<br />
<label>Show new article count: <input type="text" id="count"/></label>
<button type="submit">Save</button>
</form>
<script src="browser-polyfill.js"></script>
Expand Down
19 changes: 13 additions & 6 deletions options.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
function saveOptions(e) {
e.preventDefault();
browser.storage.sync.set({
newtab: document.querySelector("#newtab").value
newtab: document.querySelector("#newtab").value,
count: document.querySelector("#count").value
});
}

function restoreOptions() {

function setCurrentChoice(result) {
function setNewTab(result) {
document.querySelector("#newtab").value = result.newtab || "false";
}

function setCount(result) {
document.querySelector("#count").value = result.count || "true";
}

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

var getting = browser.storage.sync.get("newtab");
getting.then(setCurrentChoice, onError);
const newtab = browser.storage.sync.get("newtab");
newtab.then(setNewTab, onError);

const count = browser.storage.sync.get("count");
count.then(setCount, onError);
}

document.addEventListener("DOMContentLoaded", restoreOptions);
document.querySelector("form").addEventListener("submit", saveOptions);
document.querySelector("form").addEventListener("submit", saveOptions);
58 changes: 51 additions & 7 deletions src/background.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,55 @@
const parser = require('fast-xml-parser');
const parser = require("fast-xml-parser");

let stories;
let openedStories = [];
let newStoryCount = 1;

let options = {};

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

// Wait till we get the stories otherwise it doesn't
// seem to get storage
const storage = browser.storage.sync.get();
storage.then(
gotOptions => {
options = gotOptions;

if (options.count === "true") {
updateStoryCount();
} else {
browser.browserAction.setBadgeText({ text: "" });
}
},
() => console.error("Error getting options")
);
});
};

// Counts how many new stories since last story
function updateStoryCount() {
if (openedStories.length > 0) {
newStoryCount = 0;
for (let story of stories) {
if (openedStories.includes(story.link)) break;
else newStoryCount++;
}
}

if (newStoryCount === 0) browser.browserAction.setBadgeText({ text: "" });
else browser.browserAction.setBadgeText({ text: newStoryCount.toString() });
}

function onCreated(tab) {}

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

browser.browserAction.onClicked.addListener(function() {
Expand All @@ -41,7 +73,7 @@ browser.browserAction.onClicked.addListener(function() {
// Don't let array get too big
if (openedStories.length > 256) openedStories.shift();

if (options.newtab === 'true') {
if (options.newtab === "true") {
browser.tabs
.create({
url: nextStory
Expand All @@ -51,22 +83,34 @@ browser.browserAction.onClicked.addListener(function() {
var updating = browser.tabs.update({ url: nextStory });
updating.then(() => {}, () => {});
}

// Only update if we want to show article count
if (options.count === "true") {
newStoryCount--;
if (newStoryCount < 0) newStoryCount = 0;
if (newStoryCount === 0) browser.browserAction.setBadgeText({ text: "" });
else browser.browserAction.setBadgeText({ text: newStoryCount.toString() });
}
});

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

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

const getOptions = (changes, area) => {
const getChangedOptions = (changes, area) => {
var changedItems = Object.keys(changes);

for (var item of changedItems) {
options = { ...options, [item]: changes[item].newValue };
}

getLatest();
};

// Initial grab of stories on load
getLatest();
browser.storage.onChanged.addListener(getOptions);

// Set listener for changed options
browser.storage.onChanged.addListener(getChangedOptions);

0 comments on commit 89bc02f

Please sign in to comment.