Guest User

Untitled

a guest
Nov 13th, 2015
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function () {
  2.  
  3.     function uploader() {
  4.         console.log("Object created!");
  5.         this._started = false;
  6.         uploader.prototype.start();
  7.  
  8.     }
  9.     uploader.prototype = {
  10.         /**
  11.          * Start to handle screenshot events.
  12.          * @memberof Screenshot.prototype
  13.          */
  14.         start: function () {
  15.             console.log("IMGUR Running");
  16.             if (this._started) {
  17.                 throw 'Instance should not be start()\'ed twice.';
  18.             }
  19.             this._started = true;
  20.  
  21.             window.addEventListener('mozChromeEvent', this);
  22.         },
  23.  
  24.         /**
  25.          * Stop handling screenshot events.
  26.          * @memberof Screenshot.prototype
  27.          */
  28.         stop: function () {
  29.             if (!this._started) {
  30.                 throw 'Instance was never start()\'ed but stop() is called.';
  31.             }
  32.             this._started = false;
  33.  
  34.             window.removeEventListener('mozChromeEvent', this.handleEvent);
  35.         },
  36.  
  37.         /**
  38.          * Handle screenshot events.
  39.          * @param  {DOMEvent} evt DOM Event to handle.
  40.          * @memberof Screenshot.prototype
  41.          */
  42.         handleEvent: function (evt) {
  43.             switch (evt.type) {
  44.                 case 'mozChromeEvent':
  45.                     if (evt.detail.type === 'take-screenshot-success') {
  46.                         console.log("There is an screenshot available.");
  47.                         if(navigator.onLine){
  48.                             this.handleTakeScreenshotSuccess(evt.detail.file);
  49.                         }
  50.                     } else if (evt.detail.type === 'take-screenshot-error') {
  51.                         this.notify('screenshotFailed', evt.detail.error);
  52.                     }
  53.                     break;
  54.  
  55.                 default:
  56.                     console.debug('Unhandled event: ' + evt.type);
  57.                     break;
  58.             }
  59.         },
  60.         /**
  61.          * Handle the take-screenshot-success mozChromeEvent.
  62.          * @param  {Blob} file Blob object received from the event.
  63.          * @memberof Screenshot.prototype
  64.          */
  65.         handleTakeScreenshotSuccess: function (file) {
  66.             try {
  67.                 var self = this;
  68.                 var fd = new FormData();
  69.                 fd.append("image", file);
  70.                 var xhr = new XMLHttpRequest();
  71.                 xhr.open('POST', 'https://api.imgur.com/3/image');
  72.                 xhr.setRequestHeader('Authorization', 'Client-ID 440d44a2a741339');
  73.                 xhr.onload = function() {
  74.                   var data = JSON.parse(xhr.responseText).data;
  75.                   var imgurURL = data.link;
  76.                   console.log(imgurURL);
  77.                     self.notify('Screenshot uploaded: ', imgurURL, null, true);
  78.                     //const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
  79.                     //gClipboardHelper.copyString(imgurURL);
  80.                 };
  81.                 xhr.send(fd);
  82.             } catch (e) {
  83.                 console.log('exception in screenshot handler', e);
  84.                 this.notify('screenshotFailed', e.toString());
  85.             }
  86.  
  87.         },
  88.         /**
  89.          * Display a screenshot success or failure notification.
  90.          * Localize the first argument, and localize the third if the second is null
  91.          * @param  {String} titleid  l10n ID of the string to show.
  92.          * @param  {String} body     Label to show as body, or null.
  93.          * @param  {String} bodyid   l10n ID of the label to show as body.
  94.          * @param  {String} onClick  Optional handler if the notification is clicked
  95.          * @memberof Screenshot.prototype
  96.          */
  97.         //TODO: l10n
  98.         notify: function (titleid, body, bodyid, onClick) {
  99.             console.log("A notification would be send: " + titleid);
  100.             var notification = new window.Notification(titleid, {
  101.                 body: body,
  102.                 icon: '/style/icons/Gallery.png'
  103.             });
  104.  
  105.             notification.onclick = function () {
  106.                 notification.close();
  107.                 if (onClick) {
  108.                     new MozActivity({
  109.                       name: "view",
  110.                       data: {
  111.                         type: "url",
  112.                         url: body
  113.                       }
  114.                     });
  115.                 }
  116.             };
  117.         }
  118.     };
  119.  
  120.     console.log("Lets start!");
  121.     var uploader = new uploader();
  122. }());
Advertisement
Add Comment
Please, Sign In to add comment