diff --git a/js/app.js b/js/app.js index 3c11305..1e81b28 100644 --- a/js/app.js +++ b/js/app.js @@ -10,8 +10,9 @@ return value && JSON.parse(value); } - var app = angular.module('listenone', ['angularSoundManager', 'ui-notification', 'loWebManager', 'cfp.hotkeys', 'lastfmClient']) - .config( [ + var app = angular.module('listenone', ['angularSoundManager', 'ui-notification', 'loWebManager', 'cfp.hotkeys', 'lastfmClient', 'githubClient']) + + app.config( [ '$compileProvider', function( $compileProvider ) { @@ -43,6 +44,7 @@ }); }); + app.run(['angularPlayer', 'Notification', 'loWeb', function(angularPlayer, Notification, loWeb) { angularPlayer.setBootstrapTrack( loWeb.bootstrapTrack( @@ -85,10 +87,10 @@ app.controller('NavigationController', ['$scope', '$http', '$httpParamSerializerJQLike', '$timeout', 'angularPlayer', 'Notification', '$rootScope', 'loWeb', - 'hotkeys', 'lastfm', + 'hotkeys', 'lastfm', 'github', function($scope, $http, $httpParamSerializerJQLike, $timeout, angularPlayer, Notification, $rootScope, - loWeb, hotkeys, lastfm) { + loWeb, hotkeys, lastfm, github) { $rootScope.page_title = "Listen 1"; $scope.window_url_stack = []; @@ -106,6 +108,7 @@ $scope.isDoubanLogin = false; $scope.lastfm = lastfm; + $scope.github = github; $scope.$on('isdoubanlogin:update', function(event, data) { $scope.isDoubanLogin = data; @@ -237,14 +240,18 @@ $scope.dialog_title = '打开歌单'; $scope.dialog_type = 5; } - if (dialog_type == 6) { + if (dialog_type == 6) { $scope.dialog_title = '歌单导入合并'; - var url = '/show_myplaylist'; + var url = '/show_myplaylist'; loWeb.get(url).success(function(data) { $scope.myplaylist = data.result; }); $scope.dialog_type = 6; } + if (dialog_type == 7) { + $scope.dialog_title = '连接到Github.com'; + $scope.dialog_type = 7; + } }; $scope.chooseDialogOption = function(option_id) { @@ -643,6 +650,12 @@ }); }); + $scope.$on('github:status', function(event, data) { + $scope.$apply(function() { + $scope.githubStatus = data; + }); + }); + $scope.$on('angularPlayer:ready', function(event, data) { $log.debug('cleared, ok now add to playlist'); if (angularPlayer.getRepeatStatus() == false) { diff --git a/js/background.js b/js/background.js index bae3747..eb865fe 100644 --- a/js/background.js +++ b/js/background.js @@ -58,4 +58,17 @@ function hack_referer_header(details) { chrome.webRequest.onBeforeSendHeaders.addListener(hack_referer_header, { urls: ["*://music.163.com/*", "*://*.xiami.com/*", "*://*.qq.com/*"] -}, ['requestHeaders', 'blocking']); \ No newline at end of file +}, ['requestHeaders', 'blocking']); + + +/** + * Get tokens. + */ + +chrome.runtime.onMessage.addListener( + function(request, sender, sendResponse) { + var code = request.query.split('=')[1]; + Github.handleCallback(code); + sendResponse(); + } +); \ No newline at end of file diff --git a/js/github.js b/js/github.js new file mode 100644 index 0000000..bc7b2fd --- /dev/null +++ b/js/github.js @@ -0,0 +1,27 @@ +var ngGithub = angular.module('githubClient', []); + +ngGithub.factory('github', ['$rootScope', +function($rootScope) { + return { + openAuthUrl: function(){ + console.log('openAuthUrl'); + window.open(Github.getOAuthUrl(), '_blank'); + }, + getStatusText: function(){ + console.log('getStatusText'); + return Github.getStatusText(); + }, + getStatus: function(){ + return Github.getStatus(); + }, + updateStatus: function(){ + console.log('github update status'); + Github.updateStatus(function(newStatus){ + $rootScope.$broadcast('github:status', newStatus); + }); + }, + logout: function(){ + Github.logout(); + } + }; +}]); diff --git a/js/github_api.js b/js/github_api.js new file mode 100644 index 0000000..5c9e1d9 --- /dev/null +++ b/js/github_api.js @@ -0,0 +1,127 @@ +(function() { + var OAUTH_URL = 'https://github.com/login/oauth'; + var API_URL = 'https://api.github.com'; + + var client_id = 'e099a4803bb1e2e773a3'; + var client_secret = '81fbfc45c65af8c0fbf2b4dae6f23f22e656cfb8'; + + Storage.prototype.setObject = function(key, value) { + this.setItem(key, JSON.stringify(value)); + } + + Storage.prototype.getObject = function(key) { + var value = this.getItem(key); + return value && JSON.parse(value); + } + + var Github = { + status: 0, + username: '', + getOAuthUrl: function(){ + this.status = 1; + return OAUTH_URL + '/authorize?client_id=' + client_id + '&scope=gist'; + }, + + updateStatus: function(callback){ + var access_token = localStorage.getObject('githubOauthAccessKey'); + if (access_token == null) { + this.status = 0; + return; + } + var self = this; + this.api('/user', function(data){ + console.log(data.login); + if (data.login == undefined) { + console.log('before setting:', self.status); + self.status = 1; + console.log('after setting:', self.status); + } + else { + console.log('before setting:', self.status); + self.status = 2; + self.username = data.login; + console.log('after setting:', self.status); + } + if(callback != null) { + callback(self.status); + } + }); + }, + + getStatus: function(){ + return this.status; + }, + + getStatusText: function(){ + console.log(this.status); + if(this.status == 0) { + return '未连接'; + } + if(this.status == 1) { + return '连接中'; + } + if(this.status == 2) { + return '已连接: ' + this.username; + } + return '???' + }, + + setStatus: function(newStatus){ + this.status = newStatus; + }, + + + handleCallback: function(code, cb){ + var url = OAUTH_URL + '/access_token'; + var data = { + client_id: client_id, + client_secret: client_secret, + code: code + }; + $.ajax({ + url: url, + headers: { + Accept: 'application/json' + }, + dataType: "json", + data: data, + success: function(response) { + console.log(response); + var ak = response.access_token; + localStorage.setObject('githubOauthAccessKey', ak); + if(cb != undefined) { + cb(ak); + } + } + }); + }, + + api: function(apiPath, cb){ + var access_token = localStorage.getObject('githubOauthAccessKey') || ''; + var url = API_URL + apiPath + '?access_token=' + access_token; + $.get(url, function(response){ + cb(response); + }) + }, + + logout: function() { + localStorage.removeItem('githubOauthAccessKey'); + this.status = 0; + }, + + isLoggedIn: function() { + return localStorage.getObject('githubOauthAccessKey') != null; + }, + + deparam: function(params) { + var obj = {}; + $.each(params.split('&'), function() { + var item = this.split('='); + obj[item[0]] = item[1]; + }); + return obj; + } + }; + + window.Github = Github; +})(); \ No newline at end of file diff --git a/js/oauth_callback.js b/js/oauth_callback.js new file mode 100644 index 0000000..2a018ff --- /dev/null +++ b/js/oauth_callback.js @@ -0,0 +1,9 @@ + +/** + * Get and send oauth tokens from query string. + */ + +chrome.runtime.sendMessage({type: 'code', query: window.location.search.substr(1)}, function(response) { + // window.open('', '_self', ''); + // window.close(); +}); \ No newline at end of file diff --git a/listen1.html b/listen1.html index 2366fba..9b429de 100644 --- a/listen1.html +++ b/listen1.html @@ -27,6 +27,8 @@ + + @@ -123,13 +125,22 @@ -
正在打开Github.com页面...
+请在打开的页面点击"Authencate", 允许Listen 1访问你的账户。
+ +