Advertisement
blooglet

Untitled

Aug 13th, 2012
52
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(worker) {
  9.     var widgetAttached = true;
  10.     function onDetach() {
  11.         widgetAttached = false;
  12.     }
  13.     worker.on("detach", onDetach);
  14.  
  15.     // Put in Google Reader API request
  16.     Request({
  17.         url: "https://www.google.com/reader/api/0/unread-count?output=json",
  18.         onComplete: function(response) {
  19.             worker.removeListener("detach", onDetach);
  20.        
  21.             // Only update the widget if it's still attached.
  22.             if (!widgetAttached)
  23.                 return;
  24.                
  25.             // Ignore response if we encountered a 404 (e.g. user isn't logged in)
  26.             // or a different HTTP error.
  27.             // TODO: Can I make this work when third-party cookies are disabled?
  28.             if (response.status == 200) {
  29.                 monitorWidget.postMessage(response.json);
  30.             } else {
  31.                 monitorWidget.postMessage(null);
  32.             }
  33.         }
  34.     }).get();
  35. }
  36.  
  37. var monitorWidget = widgets.Widget({
  38.     // Mandatory widget ID string
  39.     id: "greader-monitor",
  40.  
  41.     // A required string description of the widget used for
  42.     // accessibility, title bars, and error reporting.
  43.     label: "Google Reader Monitor",
  44.     contentURL: data.url("widget.html"),
  45.     contentScriptFile: [data.url("jquery-1.7.2.min.js"), data.url("widget.js")],
  46.    
  47.     onClick: function() {
  48.         // Open Google Reader when the widget is clicked.
  49.         tabs.open("https://www.google.com/reader/view/");
  50.     },
  51.    
  52.     onAttach: function(worker) {
  53.         // If the widget's inner width changes, reflect that in the GUI
  54.         worker.port.on("widthReported", function(newWidth) {
  55.             worker.width = newWidth;
  56.         });
  57.    
  58.         var refreshTimer = timers.setInterval(refreshUnreadCount, 60000);
  59.        
  60.         // If the monitor widget is destroyed, make sure the timer gets cancelled.
  61.         worker.on("detach", function() {
  62.             timers.clearInterval(refreshTimer, worker);
  63.         });
  64.        
  65.         refreshUnreadCount(worker);
  66.     }
  67. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement