Advertisement
blooglet

Untitled

Sep 11th, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // main.js - Main entry point
  2. const tabs = require('tabs');
  3. const widgets = require('widget');
  4. const data = require('self').data;
  5. const timers = require("timers");
  6. const Request = require("request").Request;
  7.  
  8. function refreshUnreadCount() {
  9.     // Put in Google Reader API request
  10.     Request({
  11.         url: "https://www.google.com/reader/api/0/unread-count?output=json",
  12.         onComplete: function(response) {
  13.             // Ignore response if we encountered a 404 (e.g. user isn't logged in)
  14.             // or a different HTTP error.
  15.             // TODO: Can I make this work when third-party cookies are disabled?
  16.             if (response.status == 200) {
  17.                 monitorWidget.postMessage(response.json);
  18.             } else {
  19.                 monitorWidget.postMessage(null);
  20.             }
  21.         }
  22.     }).get();
  23. }
  24.  
  25. var monitorWidget = widgets.Widget({
  26.     // Mandatory widget ID string
  27.     id: "greader-monitor",
  28.  
  29.     // A required string description of the widget used for
  30.     // accessibility, title bars, and error reporting.
  31.     label: "Google Reader Monitor",
  32.     contentURL: data.url("widget.html"),
  33.     contentScriptFile: [data.url("jquery-1.7.2.min.js"), data.url("widget.js")],
  34.  
  35.     onClick: function() {
  36.         // Open Google Reader when the widget is clicked.
  37.         tabs.open("https://www.google.com/reader/view/");
  38.     },
  39.    
  40.     onAttach: function(worker) {
  41.         // If the widget's inner width changes, reflect that in the GUI
  42.         worker.port.on("widthReported", function(newWidth) {
  43.             worker.width = newWidth;
  44.         });
  45.        
  46.         // If the monitor widget is destroyed, make sure the timer gets cancelled.
  47.         worker.on("detach", function() {
  48.             timers.clearInterval(refreshTimer, worker);
  49.         });
  50.        
  51.         refreshUnreadCount(worker);
  52.     }
  53. });
  54.  
  55. var refreshTimer = timers.setInterval(refreshUnreadCount, 60000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement