Bläddra i källkod

feat: sync to onedrive

Gerald 9 år sedan
förälder
incheckning
b9565e4830
2 ändrade filer med 95 tillägg och 0 borttagningar
  1. 93 0
      src/background/sync/onedrive.js
  2. 2 0
      src/common.js

+ 93 - 0
src/background/sync/onedrive.js

@@ -0,0 +1,93 @@
+setTimeout(function () {
+  var config = {
+    client_id: '000000004418358A',
+    redirect_uri: 'https://violentmonkey.github.io/auth_onedrive.html',
+  };
+
+  function authenticate() {
+    var params = {
+      response_type: 'token',
+      client_id: config.client_id,
+      redirect_uri: config.redirect_uri,
+      scope: 'onedrive.appfolder',
+    };
+    var url = 'https://login.live.com/oauth20_authorize.srf';
+    var qs = searchParams.dump(params);
+    url += '?' + qs;
+    chrome.tabs.create({url: url});
+  }
+  function checkAuthenticate(url) {
+    var redirect_uri = config.redirect_uri + '#';
+    if (url.slice(0, redirect_uri.length) === redirect_uri) {
+      authorized(url.slice(redirect_uri.length));
+      return true;
+    }
+  }
+  function authorized(raw) {
+    var data = searchParams.load(raw);
+    if (data.access_token) {
+      onedrive.config.set({
+        uid: data.user_id,
+        token: data.access_token,
+      });
+      onedrive.prepare();
+    }
+  }
+  function normalize(item) {
+    return {
+      size: item.size,
+      uri: sync.utils.getURI(item.name),
+      modified: new Date(item.lastModifiedDateTime).getTime(),
+    };
+  }
+
+  var OneDrive = sync.BaseService.extend({
+    name: 'onedrive',
+    displayName: 'OneDrive',
+    urlPrefix: 'https://api.onedrive.com/v1.0',
+    user: function () {
+      return this.request({
+        url: '/drive',
+      });
+    },
+    list: function () {
+      var _this = this;
+      return _this.request({
+        url: '/drive/special/approot/children',
+      }).then(function (text) {
+        return JSON.parse(text);
+      }).then(function (data) {
+        return data.value.filter(function (item) {
+          return item.file && sync.utils.isScriptFile(item.name);
+        }).map(normalize);
+      });
+    },
+    get: function (path) {
+      return this.request({
+        url: '/drive/special/approot:/' + encodeURIComponent(path) + ':/content',
+      });
+    },
+    put: function (path, data) {
+      return this.request({
+        method: 'PUT',
+        url: '/drive/special/approot:/' + encodeURIComponent(path) + ':/content',
+        headers: {
+          'Content-Type': 'application/octet-stream',
+        },
+        body: data,
+      }).then(function (text) {
+        return JSON.parse(text);
+      }).then(normalize);
+    },
+    remove: function (path) {
+      // return 204
+      return this.request({
+        method: 'DELETE',
+        url: '/drive/special/approot:/' + encodeURIComponent(path),
+      });
+    },
+    authenticate: authenticate,
+    checkAuthenticate: checkAuthenticate,
+  });
+  var onedrive = sync.service('onedrive', OneDrive);
+});

+ 2 - 0
src/common.js

@@ -14,6 +14,8 @@ _.options = function () {
     autoReload: false,
     dropbox: {},
     dropboxEnabled: false,
+    onedrive: {},
+    onedriveEnabled: false,
     features: null,
   };