Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: alexeykuzmin/benua-helper
base: 15df7287de
...
head fork: alexeykuzmin/benua-helper
compare: 3d6695d623
  • 10 commits
  • 4 files changed
  • 0 commit comments
  • 2 contributors
Showing with 73 additions and 16 deletions.
  1. +2 −0  README.md
  2. +59 −12 src/background.js
  3. +1 −1  src/manifest.json
  4. +11 −3 src/popup.js
View
2  README.md
@@ -2,3 +2,5 @@ benua-helper
============
Chromium Extension about autobus schedules for business-center Benua
+
+https://chrome.google.com/webstore/detail/benua-helper/fnegkdpbolaklgedhapmecddppgbonph
View
71 src/background.js
@@ -136,10 +136,6 @@
return (h < 10 ? "0" + h : h) + (d.getSeconds() % 2 == 0 ? ":" : ":") + (m < 10 ? "0" + m : m);
}
- function save (found) {
- window.localStorage.setItem("schedule", JSON.stringify(found));
- }
-
function distance (h1, m1, h2, m2) {
var dh = h2 - h1, dm = m2 - m1;
if (dh < 0) {
@@ -152,20 +148,71 @@
return dist;
}
- function refreshSchedule() {
+ function getScheduleName () {
+ return window.localStorage.getItem("rasp");
+ }
+
+ function handleMessage(message, sender, sendResponse) {
+ if ('getSchedule' === message) {
+ var schedule = getNextBusesList();
+ sendResponse(schedule);
+ } else if ('updateBadge' === message) {
+ updateBadge();
+ }
+ }
+
+ function getNextBusesList() {
+ var d = new Date(), h = d.getHours(), m = d.getMinutes();
var name = getScheduleName();
+ var nextBuses = reorganized[name].after(h, m);
+ return nextBuses;
+ }
+ function updateBadge() {
var d = new Date(), h = d.getHours(), m = d.getMinutes();
- var found = reorganized[name].after(h, m);
- var dist = distance(h, m, found[0].h, found[0].m);
+ var nextBuses = getNextBusesList();
+ var nextBus = nextBuses[0];
+ var minutesTillNextBus = distance(h, m, nextBus.h, nextBus.m);
+ badge(minutesTillNextBus);
+ }
- badge(dist);
- save(found);
+ /**
+ * @return {number} Integer number of seconds till next minute [1; 60].
+ */
+ function getNumberOfSecondsTillNextMinute() {
+ var nowDate = new Date();
+ return 60 - nowDate.getSeconds();
}
- function getScheduleName () {
- return window.localStorage.getItem("rasp");
+ function cleanUpLocalStorage() {
+ localStorage.removeItem('schedule');
+ }
+
+ function init() {
+ // Listen for request from popup.
+ chrome.runtime.onMessage.addListener(handleMessage);
+
+ // Clean up localStorage on extension update.
+ chrome.runtime.onInstalled.addListener(function(details) {
+ if (details.reason === 'update') {
+ cleanUpLocalStorage();
+ }
+ });
+
+ // Update now.
+ updateBadge();
+
+ // Update on next minute start.
+ setTimeout(function() {
+ updateBadge();
+
+ // And every minute after.
+ setInterval(updateBadge, 60 * 1000);
+
+ // XXX(alexeykuzmin): `chrome.alarms` fires events not very precisely.
+ }, getNumberOfSecondsTillNextMinute() * 1000);
}
- window.setInterval(refreshSchedule, 10000);
+ init();
+
})(window, chrome);
View
2  src/manifest.json
@@ -1,6 +1,6 @@
{
"name": "Benua Helper",
- "version": "2.3.8",
+ "version": "2.4",
"description": "Autobus schedules for business-center Benua",
"manifest_version": 2,
View
14 src/popup.js
@@ -17,9 +17,15 @@
return dist;
}
+ function updateBadge() {
+ chrome.runtime.sendMessage('updateBadge');
+ }
+
function refresh () {
- var raw = window.localStorage.getItem("schedule");
- var found = JSON.parse(raw);
+ chrome.runtime.sendMessage('getSchedule', renderSchedule);
+ }
+
+ function renderSchedule(found) {
for (var i = 0; i < 4; ++i) {
var td = document.getElementById('autobus_' + i);
var d = new Date(), h = d.getHours(), m = d.getMinutes();
@@ -50,6 +56,8 @@
document.getElementsByClassName('selected')[0].className = '';
td.className = 'selected';
window.localStorage.setItem("rasp", key);
+ updateBadge();
+ refresh();
});
})(td, key);
}
@@ -60,6 +68,6 @@
}
refresh();
- window.setInterval(refresh, 100);
+ window.setInterval(refresh, 1e4);
})(window, document, chrome);

No commit comments for this range

Something went wrong with that request. Please try again.