Browse Source

Refactor to avoid deprecation by Google.

Daniel Andersson 6 years ago
parent
commit
e38db918c2
5 changed files with 307 additions and 307 deletions
  1. 0 193
      background.html
  2. 179 0
      js/background.js
  3. 115 0
      js/options.js
  4. 9 2
      manifest.json
  5. 4 112
      options.html

+ 0 - 193
background.html

@@ -1,193 +0,0 @@
-<html>
-<script type="text/javascript">
-
-var last_updated = 0;
-var feeds_last_updated = 0;
-var prefs_last_updated = 0;
-
-function param_escape(arg) {
-	if (typeof encodeURIComponent != 'undefined')
-		return encodeURIComponent(arg);	
-	else
-		return escape(arg);
-}
-
-function update_feeds() {
-
-	console.log("feeds update");
-
-	var requestUrl = localStorage["site_url"] + "/public.php?op=globalUpdateFeeds";
-
-	var xhr = new XMLHttpRequest();
-
-	xhr.open("POST", requestUrl, true);
-	xhr.send();
-
-	var d = new Date();
-	localStorage["last_feeds_updated"] = d.getTime();
-}
-
-function update() {
-	console.log("update");
-
-	var d = new Date();
-	var login = localStorage["login"];
-	var single_user = localStorage["single_user"];
-
-	if (single_user == "1") login = "admin";
-
-	var requestUrl = localStorage["site_url"] + "/public.php";
-	var params = "op=getUnread&fresh=1&login=" + param_escape(login);
-
-	var xhr = new XMLHttpRequest();
-
-	xhr.open("POST", requestUrl, true);
-	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
-	xhr.send(params);
-
-	xhr.onreadystatechange = function() {
-		if (xhr.readyState == 4) {
-
-			var icon = new Object();
-			var title = new Object();
-			var badge = new Object();
-			var badge_color = new Object();
-
-			// init stuff
-			icon.path = "images/normal.png";
-			title.title = "";	
-			badge.text = "";
-			badge_color.color = [0, 0, 0, 0];
-
-			var show_badge = localStorage["show_badge"] == "1";
-			var show_fresh = localStorage["show_fresh"] == "1";
-		
-			if (xhr.status == 200) {
-				var response = xhr.responseText.split(";");
-
-				var unread = parseInt(response[0]);
-
-				if (isNaN(unread)) unread = 0;
-
-				var fresh;
-	
-				if (response.length == 2)
-					fresh = parseInt(response[1]);
-				else
-					fresh = 0;
-
-				if (isNaN(fresh)) fresh = 0;
-
-				if (unread > 0) {
-					icon.path = "images/alert.png";
-					title.title = "You have %s unread articles.".replace("%s", unread);
-
-					if (show_fresh && fresh > 0) {
-						badge.text = fresh + "";
-						badge_color.color = [0, 200, 0, 255];
-					} else {
-						badge.text = unread + "";
-						badge_color.color = [255, 0, 0, 255];
-					}
-
-				} else if (unread == -1) {
-					icon.path = "images/error.png";		
-
-					var errorMsg = xhr.responseText.split(";")[1];
-
-					title.title = "Error: %s.".replace("%s", errorMsg.trim());
-
-				} else {
-					title.title = "You have no unread articles.";
-				}
-
-				localStorage["last_updated"] = d.getTime();
-				localStorage["last_error"] = "";
-			} else {
-				localStorage["last_error"] = xhr.responseText;
-
-				icon.path = "images/error.png";
-				title.title = "Error (%s) while updating.".replace("%s", xhr.status);
-			}
-
-			if (!show_badge) badge.text = "";	
-
-			chrome.browserAction.setBadgeBackgroundColor(badge_color);
-			chrome.browserAction.setBadgeText(badge);
-			chrome.browserAction.setTitle(title);
-			chrome.browserAction.setIcon(icon);
-
-		}
-	};
-
-}
-
-function timeout() {
-
-	var update_interval;
-	var prefs_updated;
-	var feeds_update_interval = 30 * 60 * 1000;
-
-	if (localStorage["update_interval"])
-		update_interval = localStorage["update_interval"] * 60 * 1000;
-	else
-		update_interval = 15 * 60 * 1000;
-
-	if (localStorage["prefs_updated"])
-		prefs_updated = localStorage["prefs_updated"];
-	else
-		prefs_updated = -1;
-
-	var d = new Date();
-
-	if (d.getTime() > last_updated + update_interval || 
-			prefs_updated != prefs_last_updated) {
-
-		last_updated = d.getTime();
-		try {
-			update();
-		} catch (e) {
-			console.warn(e);
-		}
-	}
-
-	if (localStorage['update_feeds'] == 1 && (d.getTime() > feeds_last_updated + feeds_update_interval || prefs_updated != prefs_last_updated)) {
-
-		feeds_last_updated = d.getTime();
-	
-		try {
-			update_feeds();
-		} catch (e) {
-			console.warn(e);
-		}
-
-	}
-
-	prefs_last_updated = prefs_updated;
-
-	window.setTimeout("timeout()", 1000);
-}
-
-function init() {
-
-	chrome.browserAction.onClicked.addListener(function() { 
-		var site_url = localStorage['site_url'];
-
-		if (site_url) {
-			var cp = new Object();
-
-			cp.url = site_url;
-
-			chrome.tabs.create(cp);
-		}
-
-	});
-		
-	window.setTimeout("timeout()", 1000);
-
-}
-</script>
-
-<body onload="init()"> </body>
-
-</html>

+ 179 - 0
js/background.js

@@ -0,0 +1,179 @@
+/* Handle showing of new item count and polling the TT-RSS server. */
+
+var last_updated = 0;
+var feeds_last_updated = 0;
+var prefs_last_updated = 0;
+
+function param_escape(arg) {
+  if (typeof encodeURIComponent != 'undefined')
+    return encodeURIComponent(arg);
+  else
+    return escape(arg);
+}
+
+function update_feeds() {
+  console.log('feeds update');
+
+  var requestUrl = localStorage['site_url'] +
+    '/public.php?op=globalUpdateFeeds';
+
+  var xhr = new XMLHttpRequest();
+
+  xhr.open('POST', requestUrl, true);
+  xhr.send();
+
+  var d = new Date();
+  localStorage['last_feeds_updated'] = d.getTime();
+}
+
+function update() {
+  console.log('update');
+
+  var d = new Date();
+  var login = localStorage['login'];
+  var single_user = localStorage['single_user'];
+
+  if (single_user == '1') login = 'admin';
+
+  var requestUrl = localStorage['site_url'] + '/public.php';
+  var params = 'op=getUnread&fresh=1&login=' + param_escape(login);
+
+  var xhr = new XMLHttpRequest();
+
+  xhr.open('POST', requestUrl, true);
+  xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
+  xhr.send(params);
+
+  xhr.onreadystatechange = function() {
+    if (xhr.readyState == 4) {
+      var icon = new Object();
+      var title = new Object();
+      var badge = new Object();
+      var badge_color = new Object();
+
+      // init stuff
+      icon.path = 'images/normal.png';
+      title.title = '';
+      badge.text = '';
+      badge_color.color = [0, 0, 0, 0];
+
+      var show_badge = localStorage['show_badge'] == '1';
+      var show_fresh = localStorage['show_fresh'] == '1';
+
+      if (xhr.status == 200) {
+        var response = xhr.responseText.split(';');
+
+        var unread = parseInt(response[0]);
+
+        if (isNaN(unread)) unread = 0;
+
+        var fresh;
+
+        if (response.length == 2)
+          fresh = parseInt(response[1]);
+        else
+          fresh = 0;
+
+        if (isNaN(fresh)) fresh = 0;
+
+        if (unread > 0) {
+          icon.path = 'images/alert.png';
+          title.title = 'You have %s unread articles.'.replace('%s', unread);
+
+          if (show_fresh && fresh > 0) {
+            badge.text = fresh + '';
+            badge_color.color = [0, 200, 0, 255];
+          } else {
+            badge.text = unread + '';
+            badge_color.color = [255, 0, 0, 255];
+          }
+        } else if (unread == -1) {
+          icon.path = 'images/error.png';
+
+          var errorMsg = xhr.responseText.split(';')[1];
+
+          title.title = 'Error: %s.'.replace('%s', errorMsg.trim());
+        } else {
+          title.title = 'You have no unread articles.';
+        }
+
+        localStorage['last_updated'] = d.getTime();
+        localStorage['last_error'] = '';
+      } else {
+        localStorage['last_error'] = xhr.responseText;
+
+        icon.path = 'images/error.png';
+        title.title = 'Error (%s) while updating.'.replace('%s', xhr.status);
+      }
+
+      if (!show_badge) badge.text = '';
+
+      chrome.browserAction.setBadgeBackgroundColor(badge_color);
+      chrome.browserAction.setBadgeText(badge);
+      chrome.browserAction.setTitle(title);
+      chrome.browserAction.setIcon(icon);
+    }
+  };
+}
+
+function timeout() {
+  var update_interval;
+  var prefs_updated;
+  var feeds_update_interval = 30 * 60 * 1000;
+
+  if (localStorage['update_interval'])
+    update_interval = localStorage['update_interval'] * 60 * 1000;
+  else
+    update_interval = 15 * 60 * 1000;
+
+  if (localStorage['prefs_updated'])
+    prefs_updated = localStorage['prefs_updated'];
+  else
+    prefs_updated = -1;
+
+  var d = new Date();
+
+  if (d.getTime() > last_updated + update_interval ||
+      prefs_updated != prefs_last_updated) {
+    last_updated = d.getTime();
+    try {
+      update();
+    } catch (e) {
+      console.warn(e);
+    }
+  }
+
+  if (localStorage['update_feeds'] == 1 &&
+      (d.getTime() > feeds_last_updated + feeds_update_interval ||
+       prefs_updated != prefs_last_updated)) {
+    feeds_last_updated = d.getTime();
+
+    try {
+      update_feeds();
+    } catch (e) {
+      console.warn(e);
+    }
+  }
+
+  prefs_last_updated = prefs_updated;
+}
+
+function init() {
+  chrome.browserAction.onClicked.addListener(function() {
+    var site_url = localStorage['site_url'];
+
+    if (site_url) {
+      chrome.tabs.create({url: site_url});
+    }
+  });
+
+  // TODO: Create smarter algorithm that sets `periodInMinutes` to
+  // `feeds_update_interval` and updates the `alarm` object when extension
+  // preferences are saved.
+  timeout();
+  chrome.alarms.create({periodInMinutes: 1});
+  chrome.alarms.onAlarm.addListener(function() {timeout();});
+}
+
+init();
+

+ 115 - 0
js/options.js

@@ -0,0 +1,115 @@
+/* Option handling. */
+
+function save() {
+
+  var s = $('status');
+
+  s.innerHTML = 'Options saved successfully.';
+
+  var f = document.forms['options'];
+
+  if (f.site_url.value.length > 0)
+    localStorage['site_url'] = f.site_url.value;
+  else {
+    s.innerHTML = 'Error: Site url cannot be blank.';
+    new Effect.Highlight(f.site_url);
+  }
+
+  if (f.login.value.length > 0)
+    localStorage['login'] = f.login.value;
+  else {
+    s.innerHTML = 'Error: Login cannot be blank.';
+    new Effect.Highlight(f.login);
+  }
+
+  var update_interval = parseInt(f.update_interval.value);
+
+  if (update_interval > 0)
+    localStorage['update_interval'] = f.update_interval.value;
+  else {
+    s.innerHTML = 'Error: Update interval must be greater than zero.';
+    new Effect.Highlight(f.update_interval);
+  }
+
+  localStorage['show_badge'] = (f.show_badge.checked) ? '1' : '0';
+  localStorage['show_fresh'] = (f.show_fresh.checked) ? '1' : '0';
+  localStorage['single_user'] = (f.single_user.checked) ? '1' : '0';
+  localStorage['update_feeds'] = (f.update_feeds.checked) ? '1' : '0';
+
+  var d = new Date();
+
+  localStorage['prefs_updated'] = d.getTime();
+
+  Element.show(s);
+
+  return false;
+}
+
+function single_user_toggle() {
+  var f = document.forms['options'];
+
+  f.login.disabled = f.single_user.checked;
+}
+
+function init() {
+  var f = document.forms['options'];
+
+  if (localStorage['site_url'])
+    f.site_url.value = localStorage['site_url'];
+  else
+    f.site_url.value = 'http://example.dom/tt-rss/';
+
+  if (localStorage['login'])
+    f.login.value = localStorage['login'];
+  else
+    f.login.value = 'user';
+
+  if (localStorage['update_interval'])
+    f.update_interval.value = localStorage['update_interval'];
+  else
+    f.update_interval.value = '15';
+
+  if (localStorage['show_badge'])
+    f.show_badge.checked = localStorage['show_badge'] == '1';
+  else
+    f.show_badge.checked = true;
+
+  if (localStorage['show_fresh'])
+    f.show_fresh.checked = localStorage['show_fresh'] == '1';
+  else
+    f.show_fresh.checked = false;
+
+  if (localStorage['single_user'])
+    f.single_user.checked = localStorage['single_user'] == '1';
+  else
+    f.single_user.checked = false;
+
+  if (localStorage['update_feeds'])
+    f.update_feeds.checked = localStorage['update_feeds'] == '1';
+  else
+    f.update_feeds.checked = false;
+
+  single_user_toggle();
+
+  var last_updated = $('last_updated');
+
+  var d = new Date();
+
+  d.setTime(localStorage['last_updated']);
+
+  last_updated.innerHTML = d;
+
+  var feeds_last_updated = $('feeds-last-updated');
+
+  d.setTime(localStorage['last_feeds_updated']);
+
+  feeds_last_updated.innerHTML = d;
+}
+
+document.addEventListener('DOMContentLoaded', function() {
+  document.getElementById('options').addEventListener('submit', save);
+  document.querySelectorAll('input[name$="single_user"]')[0].addEventListener(
+    'change', single_user_toggle);
+  init();
+});
+

+ 9 - 2
manifest.json

@@ -1,7 +1,11 @@
 {
   "name": "Tiny Tiny RSS Notifier",
-  "background_page": "background.html",
+  "background": {
+    "scripts": ["js/background.js"],
+    "persistent": false
+  },
   "version": "0.5.1",
+  "manifest_version": 2,
   "description": "This extension displays the number of unread articles in your Tiny Tiny RSS installation",
   "options_page": "options.html",
   "icons": { "48": "images/icon_48.png", "128": "images/icon_128.png" },
@@ -9,5 +13,8 @@
     "default_icon": "images/normal.png",
     "default_title": "You have no unread articles."
   },
-  "permissions": [ "http://*/*", "https://*/*" ]
+  "permissions": [
+    "alarms",
+    "http://*/*",
+    "https://*/*"]
 }

+ 4 - 112
options.html

@@ -4,114 +4,7 @@
 <link rel="stylesheet" href="style.css" type="text/css"/>
 <script type="text/javascript" src="lib/prototype.js"></script>
 <script type="text/javascript" src="lib/scriptaculous/scriptaculous.js?load=effects,dragdrop,controls"></script>
-
-<script type="text/javascript">
-function save() {
-
-	var s = $("status");
-
-	s.innerHTML = "Options saved successfully.";
-
-	var f = document.forms['options'];
-
-	if (f.site_url.value.length > 0) 
-		localStorage['site_url'] = f.site_url.value;
-	else {
-		s.innerHTML = "Error: Site url cannot be blank.";
-		new Effect.Highlight(f.site_url);
-	}
-
-	if (f.login.value.length > 0) 
-		localStorage['login'] = f.login.value;
-	else {
-		s.innerHTML = "Error: Login cannot be blank.";
-		new Effect.Highlight(f.login);
-	}
-
-	var update_interval = parseInt(f.update_interval.value);
-
-	if (update_interval > 0) 
-		localStorage['update_interval'] = f.update_interval.value;
-	else {
-		s.innerHTML = "Error: Update interval must be greater than zero.";
-		new Effect.Highlight(f.update_interval);
-	}
-
-	localStorage['show_badge'] = (f.show_badge.checked) ? "1" : "0";
-	localStorage['show_fresh'] = (f.show_fresh.checked) ? "1" : "0";
-	localStorage['single_user'] = (f.single_user.checked) ? "1" : "0";
-	localStorage['update_feeds'] = (f.update_feeds.checked) ? "1" : "0";
-
-	var d = new Date();
-
-	localStorage["prefs_updated"] = d.getTime();
-
-	Element.show(s);
-		
-	return false;
-}
-
-function single_user_toggle() {
-	var f = document.forms['options'];
-
-	f.login.disabled = f.single_user.checked;
-}
-
-function init() {
-	var f = document.forms['options'];
-
-	if (localStorage['site_url'])	
-		f.site_url.value = localStorage['site_url'];
-	else 
-		f.site_url.value = "http://example.dom/tt-rss/";
-
-	if (localStorage['login'])
-		f.login.value = localStorage['login'];
-	else
-		f.login.value = "user";
-
-	if (localStorage['update_interval'])
-		f.update_interval.value = localStorage['update_interval'];
-	else
-		f.update_interval.value = '15';
-
-	if (localStorage['show_badge'])
-		f.show_badge.checked = localStorage['show_badge'] == "1";
-	else
-		f.show_badge.checked = true;
-
-	if (localStorage['show_fresh'])
-		f.show_fresh.checked = localStorage['show_fresh'] == "1";
-	else
-		f.show_fresh.checked = false;
-
-	if (localStorage['single_user'])
-		f.single_user.checked = localStorage['single_user'] == "1";
-	else
-		f.single_user.checked = false;
-
-	if (localStorage['update_feeds'])
-		f.update_feeds.checked = localStorage['update_feeds'] == "1";
-	else
-		f.update_feeds.checked = false;
-
-	single_user_toggle();
-
-	var last_updated = $('last_updated');
-
-	var d = new Date();
-
-	d.setTime(localStorage["last_updated"]);
-
-	last_updated.innerHTML = d;	
-
-	var feeds_last_updated = $('feeds-last-updated');
-
-	d.setTime(localStorage["last_feeds_updated"]);
-
-	feeds_last_updated.innerHTML = d;
-}
-</script>
+<script type="text/javascript" src="js/options.js"></script>
 
 <style type="text/css">
 fieldset {
@@ -144,7 +37,7 @@ fieldset span.note {
 
 </style>
 
-<body onload="init()">
+<body>
 	<div class="floatingLogo"><img src="images/icon_128.png"></div>
 
 	<h1>Tiny Tiny RSS Notifier</h1>
@@ -155,7 +48,7 @@ fieldset span.note {
 
 	<h2>Options</h2>
 
-	<form name="options" id="options" onsubmit="return save()">
+	<form name="options" id="options">
 		<fieldset>
 			<label>URL of your Tiny Tiny RSS installation:</label>
 			<input name="site_url" size="60" value=""/>
@@ -168,8 +61,7 @@ fieldset span.note {
 
 		<fieldset>
 			<label>Single-user mode:</label>
-			<input name="single_user" onchange="single_user_toggle()" 
-				type="checkbox"  value="1"/>
+			<input name="single_user" type="checkbox"  value="1"/>
 		</fieldset>
 
 		<fieldset>