diff --git a/README.md b/README.md index 1f7f667793a1b7da16e36708acf7587cc9d21d40..c85c5886e7d9648b261a18f3dbef959148b96fef 100644 --- a/README.md +++ b/README.md @@ -22,22 +22,28 @@ > 为确定万无一失,在使用本书签工具前请先前往chrome浏览器书签管理器中导出所有书签并妥善保管! > > 本书签工具不会收集个人用户任何信息和书签内容,请放心使用!所有数据将托管于用户自建的代码服务器中。 +> +> 私人令牌存储与本地缓存数据,不会进行上传操作。`下载请认准商标[CLCoding],别被不法分子利用了` #### 安装步骤 -步骤: -- 创建一个书签项目[Github私有仓库](https://github.com/new),项目名称不限 -- 获取Github项目[私有访问Token](https://github.com/settings/tokens)(如何获取:Github -> Settings -> Developer settings -> Personal access tokens -> Generate new token) +> `一句话安装:` 准备gitee的私人令牌,选择·[projects]、[user_info]权限列表即可。 + +详细步骤: +- 创建一个书签项目[Gitee私有仓库](https://gitee.com/projects/new),项目名称不限。`注意:请选择私有项目,否则全世界人民都可以看见你在浏览什么网页了` +- 获取Gitee项目[私有访问Token](https://gitee.com/profile/personal_access_tokens)(`如何获取:Gitee -> 设置 -> 私人令牌 -> 新建令牌 -> 选取[projects]权限 -> 提交`) + +![](./assets/token.jpg) + - 下载当前书签项目完整源代码 - 直接在浏览器地址栏输入:`chrome://extensions/`,进入扩展程序管理界面 - 点击【加载已解压的扩展程序】-> 选择项目根目录,导入源码文件夹 - 完成后书签工具会显示在浏览器地址栏旁 - 点击书签工具配置书签 - - 在配置界面输入第一、二步创建的仓库地址(如:https://github.com/xxx/chrome-bookmark)及访问Token + - 在配置界面输入第一、二步创建的仓库地址(如:https://gitee.com/xxx/chrome-bookmark)及访问Token - 点击【保存配置】后完成配置退出工具界面 - 重新点击书签图标工具,回到主界面进行书签管理 - #### 工作原理 > 本项目采用github私有仓库作为远程存储仓库,利用github api进行书签全量文件的备份和版本管理工作。采用github作为后端存储有以下好处: @@ -48,3 +54,5 @@ - 全世界可达,不受网络影响 - 无运营成本 - 全程https协议传输数据 +- 摆脱google服务器无法访问 +- 优化github访问慢,国内访问无障碍 diff --git a/assets/main.png b/assets/main.png index ae89de52cf30b81f7e5358c2d5259a56030526e6..841c94da09a217c98aa29db68a9d93e8e799ff87 100644 Binary files a/assets/main.png and b/assets/main.png differ diff --git a/assets/settings.png b/assets/settings.png index b329fa8ec37ac7a65a82e6d9e476e8b5b76b620d..a2417c04d1c6748387751307d41c5e1f513d8a8a 100644 Binary files a/assets/settings.png and b/assets/settings.png differ diff --git a/assets/token.jpg b/assets/token.jpg new file mode 100644 index 0000000000000000000000000000000000000000..07793e322ca6dd789ba45a4f0d5270ba2ed24fff Binary files /dev/null and b/assets/token.jpg differ diff --git a/js/bookmark.js b/js/bookmark.js index ab6d70e361801515614a6ecc2d08c6b51d75eb3d..94c2b0d8b3478896ea399a6a650ae65f25efd45c 100644 --- a/js/bookmark.js +++ b/js/bookmark.js @@ -84,3 +84,68 @@ function addAll(data, parentname) { } }) } + +function getDate() { + let myDate = new Date() + let year = myDate.getFullYear() + let month = myDate.getMonth() + 1 + let date = myDate.getDate() + let h = myDate.getHours() + let m = myDate.getMinutes() + let s = myDate.getSeconds() + let now = year + '-' + conver(month) + '-' + conver(date) + ' ' + conver(h) + ':' + conver(m) + ':' + conver(s) + return now +} + +//日期时间处理 +function conver(s) { + return s < 10 ? '0' + s : s +} + +// onChanged自动提交 +chrome.bookmarks.onChanged.addListener(function(id, changeInfo) { + // alert('onChnage ' + id); + chrome.storage.local.get(['reponame','repostatus'], (result) =>{ + // alert('status '+result.reponame+' ' + result.repostatus) + if (result.reponame !== undefined && result.repostatus !== false) { + let github = new Gitee() + github.updateTags('bookmarks/' + result.reponame, getDate(),'master') + } + }) + }); + + // onCreated自动提交 + chrome.bookmarks.onCreated.addListener(function(id, bookmark) { + // alert('onCreated ' + id); + chrome.storage.local.get(['reponame','repostatus'], (result) =>{ + // alert('status '+result.reponame+' ' + result.repostatus) + if (result.reponame !== undefined && result.repostatus !== false) { + let github = new Gitee() + github.updateTags('bookmarks/' + result.reponame, getDate(),'master') + } + }) + }); + + // onChildrenReordered自动提交 + chrome.bookmarks.onChildrenReordered.addListener(function(id, reorderInfo) { + // alert('onChildrenReordered ' + id); + chrome.storage.local.get(['reponame','repostatus'], (result) =>{ + // alert('status '+result.reponame+' ' + result.repostatus) + if (result.reponame !== undefined && result.repostatus !== false) { + let github = new Gitee() + github.updateTags('bookmarks/' + result.reponame, getDate(),'master') + } + }) + }); + + // onRemoved自动提交 + chrome.bookmarks.onRemoved.addListener(function(id, removeInfo) { + // alert('onRemoved ' + id); + chrome.storage.local.get(['reponame','repostatus'], (result) =>{ + // alert('status '+result.reponame+' ' + result.repostatus) + if (result.reponame !== undefined && result.repostatus !== false) { + let github = new Gitee() + github.updateTags('bookmarks/' + result.reponame, getDate(),'master') + } + }) + }); \ No newline at end of file diff --git a/js/gitee.js b/js/gitee.js new file mode 100644 index 0000000000000000000000000000000000000000..5c2722455cc9fe3dbc06a5c5f30d96bdb033ff76 --- /dev/null +++ b/js/gitee.js @@ -0,0 +1,587 @@ +// https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoGitTreesSha 获取项目文件和递归所有文件 +// https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoContents(Path) 获取仓库具体路径下的内容 +// https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoContents(Path) 获取文件内容 +// https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoGitBlobsSha 获取文件Blob +// https://gitee.com/api/v5/swagger#/postV5ReposOwnerRepoContentsPath 新建文件 +// https://gitee.com/api/v5/swagger#/putV5ReposOwnerRepoContentsPath 更新文件 +// https://gitee.com/api/v5/swagger#/deleteV5ReposOwnerRepoContentsPath 删除文件 +// https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoBranches 获取所有分支 +// https://gitee.com/api/v5/swagger#/postV5ReposOwnerRepoBranches 创建分支 +// https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoCommits 仓库的所有提交(分页) +// https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoCommitsSha 仓库的某个提交 +// inside: owner repo path content message branch +const git_url = 'https://gitee.com/api' +const header = {'Content-type': 'application/json;charset=UTF-8' } +const git_key = ['access_token','owner','repo','version'] +const initext = 'ZGQK' +function Gitee() { + /** + * API文档 https://gitee.com/api/v5/swagger#/postV5ReposOwnerRepoContentsPath 新建文件 + * @param {*} path + * @param {*} content + * @param {*} message + * @param {*} branch + */ + this.create = function (path,content,message,branch) { + chrome.storage.local.get(git_key, function (result) { + let access_token = result.access_token + let owner = result.owner + let repo = result.repo + let url = git_url + '/v5/repos/' + owner + '/' + repo + '/contents/' + path + let data = { + 'access_token': access_token, + 'content': window.btoa(content), + 'message': message, + 'branch': branch, + } + fetch(url, { + method: 'POST', + body: JSON.stringify(data), + headers: header + }).then(res => res.json()) + .catch((error) => { + chrome.notifications.create(null, { + type: 'basic', + iconUrl: 'img/icon.png', + title: '创建 ' + path, + message: error + }) + }) + .then((resp) => { + console.log('创建' + path,resp) + }) + }) + } + + /** + * API文档:https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoContents(Path) 获取仓库具体路径下的内容 + * @param {*} path 文件路径 + * @param {*} ref 分支、tag或commit。默认: 仓库的默认分支(通常是master) + */ + this.getlist = function (path,ref,element,elenum) { + chrome.storage.local.get(git_key, function (result) { + console.log('result', result) + let access_token = result.access_token + let owner = result.owner + let repo = result.repo + let version = result.version + let url = git_url + '/v5/repos/' + owner + '/' + repo + '/contents/' + path + '?access_token=' + access_token + '&ref=' + ref + fetch(url, { + method: 'GET', + headers: header + }).then(res => res.json()) + .catch((error) => { + chrome.notifications.create(null, { + type: 'basic', + iconUrl: 'img/icon.png', + title: '获取仓库路径 ' + path, + message: error + }) + }) + .then((resp) => { + console.log('getlist', resp) + if (resp['message'] != undefined) { + console.log('error',resp) + } else { + resp.forEach((vds) => { + var op = document.createElement('option'); + op.setAttribute('label', vds['name']); + op.setAttribute('value', vds['name']); + op.setAttribute('select', 'select') + element.appendChild(op); + }) + elenum.innerText = resp.length + } + }) + }) + } + + /** + * API文档:https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoContents(Path) 获取仓库具体路径下的内容 + * @param {*} path 文件路径 + * @param {*} storage + */ + this.getTodo = function (path,storage) { + chrome.storage.local.get(git_key, function (result) { + let access_token = result.access_token + let owner = result.owner + let repo = result.repo + let version = result.version + let url = git_url + '/v5/repos/' + owner + '/' + repo + '/contents/' + path + '?access_token=' + access_token + fetch(url, { + method: 'GET', + headers: header + }).then(res => res.json()) + .catch((error) => { + chrome.notifications.create(null, { + type: 'basic', + iconUrl: 'img/icon.png', + title: '获取仓库路径 ' + path, + message: error + }) + }) + .then((resp) => { + console.log(resp) + let info = JSON.parse(decodeURIComponent(window.atob(resp['content']))) + + try { + storage.set({ + easyTodoStorage: info + }) + } catch (e) { + // do nothing + } finally { + chrome.notifications.create(null, { + type: 'basic', + iconUrl: 'images/icon16.png', + title: '更新本地书签', + message: '同步完成 ' + }) + } + }) + }) + } + + // 获取指定文件内容 + /** + * API文档:https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoGitBlobsSha 获取文件Blob + * @param {*} sha + */ + this.get = function (sha) { + chrome.bookmarks.getTree((re) => { + chrome.storage.local.set({'repostatus': false}, () => { + removeall(re) + + chrome.storage.local.get(git_key, function (result) { + let access_token = result.access_token + let owner = result.owner + let repo = result.repo + let url = git_url + '/v5/repos/' + owner + '/' + repo + '/commits/' + sha + '?access_token=' + access_token + console.log(url) + if (owner === '' || owner === undefined) { + chrome.storage.local.set({ 'repostatus': true }, () => { + alert('未配置认证信息 true') + console.log('reset repostatus === true') + }) + return + } + fetch(url, { + method: 'GET', + headers: header + }).then(res => res.json()) + .catch((error) => { + chrome.storage.local.set({ 'repostatus': true }, () => { + alert('error true',error) + console.log('reset repostatus === true') + }) + chrome.notifications.create(null, { + type: 'basic', + iconUrl: 'img/icon.png', + title: '获取文件Blob ' + sha, + message: error + }) + }) + .then((resp) => { + let url = git_url + '/v5/repos/' + owner + '/' + repo + '/git/blobs/' + resp['files'][0]['sha'] + '?access_token=' + access_token + fetch(url, { + method: 'GET', + headers: header + }).then(res => res.json()) + .catch((error) => { + chrome.notifications.create(null, { + type: 'basic', + iconUrl: 'img/icon.png', + title: '获取文件Blob ' + resp['files'][0]['sha'], + message: error + }) + }) + .then((resp2) => { + console.log(resp2) + if (resp2['message'] == undefined) { + let info = JSON.parse(decodeURIComponent(window.atob(resp2['content']))) + try { + addAll(info[0].children,'') + } catch (e) { + console.log('error',e) + } finally { + chrome.notifications.create(null, { + type: 'basic', + iconUrl: 'images/icon16.png', + title: '切换等待10秒', + message: '请勿执行书签操作,否则手工完成同步操作' + }) + setTimeout(() => { + chrome.storage.local.set({ 'repostatus': true }, () => { + chrome.notifications.create(null, { + type: 'basic', + iconUrl: 'images/icon16.png', + title: '更新本地书签完成', + message: '启动书签自动同步功能' + }) + }) + },10000); + } + } else { + console.log('error get ',resp2) + } + }) + }) + }) + }) + }) + } + + // 获取指定文件内容 + /** + * API文档:https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoGitBlobsSha 获取文件Blob + * @param {*} sha + */ + this.getrecovery = function (path) { + chrome.bookmarks.getTree((re) => { + chrome.storage.local.set({'repostatus': false}, () => { + removeall(re) + + chrome.storage.local.get(git_key, function (result) { + let access_token = result.access_token + let owner = result.owner + let repo = result.repo + let url = git_url + '/v5/repos/' + owner + '/' + repo + '/contents/' + path + '?access_token=' + access_token + console.log(url) + if (owner === '' || owner === undefined) { + chrome.storage.local.set({ 'repostatus': true }, () => { + alert('未配置认证信息 true') + console.log('reset repostatus === true') + }) + return + } + + fetch(url, { + method: 'GET', + headers: header + }).then(res => res.json()) + .catch((error) => { + chrome.storage.local.set({ 'repostatus': true }, () => { + alert('error true',error) + console.log('reset repostatus === true') + }) + chrome.notifications.create(null, { + type: 'basic', + iconUrl: 'img/icon.png', + title: '获取文件Blob ' + path, + message: error + }) + }) + .then((resp) => { + console.log(resp) + let info = JSON.parse(decodeURIComponent(window.atob(resp['content']))) + try { + addAll(info[0].children,'') + } catch (e) { + console.log('error',e) + } finally { + chrome.notifications.create(null, { + type: 'basic', + iconUrl: 'images/icon16.png', + title: '切换等待10秒', + message: '请勿执行书签操作,否则手工完成同步操作' + }) + setTimeout(() => { + chrome.storage.local.set({ 'repostatus': true }, () => { + chrome.notifications.create(null, { + type: 'basic', + iconUrl: 'images/icon16.png', + title: '更新本地书签完成', + message: '启动书签自动同步功能' + }) + }) + },10000); + } + // return window.atob(resp.content) + }) + }) + }) + }) + } + + /** + * API文档: https://gitee.com/api/v5/swagger#/putV5ReposOwnerRepoContentsPath 更新文件 + * @param {*} sha + * @param {*} message + */ + this.updateTagsSha = function (sha,message) { + chrome.storage.local.get(git_key, function (result) { + let access_token = result.access_token + let owner = result.owner + let repo = result.repo + + chrome.bookmarks.getTree((re) => { + let data = { + "access_token": access_token, + "content": window.btoa(encodeURIComponent(JSON.stringify(re))), + "sha": sha, + "message": message + } + + let put_url = git_url + '/v5/repos/' + owner + '/' + repo + '/contents/' + path + fetch(put_url, { + method: 'PUT', + body: JSON.stringify(data), + headers: header + }).then(res => res.json()) + .catch((error) => { + chrome.notifications.create(null, { + type: 'basic', + iconUrl: 'img/icon.png', + title: '上传全量书签错误', + message: error + }) + }) + .then((resp) => { + console.log(resp) + chrome.notifications.create(null, { + type: 'basic', + iconUrl: 'img/icon.png', + title: '上传全量书签', + message: '成功' + }) + }) + }) + }) + } + + /** + * API文档: https://gitee.com/api/v5/swagger#/putV5ReposOwnerRepoContentsPath 更新文件 + * @param {*} path + * @param {*} message + * @param {*} ref + */ + this.updateTags = function (path,message,ref) { + let _this = this + chrome.storage.local.get(git_key, function (result) { + let access_token = result.access_token + let owner = result.owner + let repo = result.repo + + // https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoContents(Path) 获取仓库具体路径下的内容 + let url = git_url + '/v5/repos/' + owner + '/' + repo + '/contents/' + path + '?access_token=' + access_token + '&ref=' + ref + fetch(url, { + method: 'GET', + headers: header + }).then(res => res.json()) + .catch((error) => { + chrome.notifications.create(null, { + type: 'basic', + iconUrl: 'img/icon.png', + title: '获取仓库路径 ' + path, + message: error + }) + }) + .then((resp) => { + console.log('gitee get success' + JSON.stringify(resp), resp['sha']) + if (resp['sha'] == undefined) { + _this.create(path,initext,'init','master') + _this.updateTags(path,message,ref) + return + } + chrome.bookmarks.getTree((re) => { + let data = { + "access_token": access_token, + "content": window.btoa(encodeURIComponent(JSON.stringify(re))), + "sha": resp['sha'], + "message": message + } + + let put_url = git_url + '/v5/repos/' + owner + '/' + repo + '/contents/' + path + fetch(put_url, { + method: 'PUT', + body: JSON.stringify(data), + headers: header + }).then(res => res.json()) + .catch((error) => { + chrome.notifications.create(null, { + type: 'basic', + iconUrl: 'img/icon.png', + title: '上传全量书签错误', + message: error + }) + }) + .then((resp) => { + console.log(resp) + chrome.notifications.create(null, { + type: 'basic', + iconUrl: 'img/icon.png', + title: '上传全量书签', + message: '成功' + }) + }) + }) + }) + }) + } + + /** + * API文档: todo 远程同步TODOLIST + * @param {*} path + * @param {*} message + * @param {*} ref + */ + this.updateTodo = function (path,message) { + let _this = this + chrome.storage.local.get(git_key, function (result) { + let access_token = result.access_token + let owner = result.owner + let repo = result.repo + + // https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoContents(Path) 获取仓库具体路径下的内容 + let url = git_url + '/v5/repos/' + owner + '/' + repo + '/contents/' + path + '?access_token=' + access_token + fetch(url, { + method: 'GET', + headers: header + }).then(res => res.json()) + .catch((error) => { + chrome.notifications.create(null, { + type: 'basic', + iconUrl: 'img/icon.png', + title: '获取仓库路径 ' + path, + message: error + }) + }) + .then((resp) => { + console.log('gitee get success' + JSON.stringify(resp), resp['sha']) + if (resp['sha'] == undefined) { + _this.create(path,initext,'init','master') + _this.updateTodo(path,message) + return + } + chrome.bookmarks.getTree((re) => { + let data = { + "access_token": access_token, + "content": window.btoa(encodeURIComponent(JSON.stringify(re))), + "sha": resp['sha'], + "message": message + } + + // https://gitee.com/api/v5/swagger#/putV5ReposOwnerRepoContentsPath 更新文件 + let put_url = git_url + '/v5/repos/' + owner + '/' + repo + '/contents/' + path + fetch(put_url, { + method: 'PUT', + body: JSON.stringify(data), + headers: header + }).then(res => res.json()) + .catch((error) => { + chrome.notifications.create(null, { + type: 'basic', + iconUrl: 'img/icon.png', + title: '上传全量书签错误', + message: error + }) + }) + .then((resp) => { + console.log(resp) + chrome.notifications.create(null, { + type: 'basic', + iconUrl: 'img/icon.png', + title: '上传全量书签', + message: '成功' + }) + }) + }) + }) + }) + } + + /** + * API文档:https://gitee.com/api/v5/swagger#/deleteV5ReposOwnerRepoContentsPath 删除文件 + * @param {*} path + * @param {*} message + */ + this.delete = function (path,message) { + chrome.storage.local.get(git_key, function (result) { + let _this = this + let access_token = result.access_token + let owner = result.owner + let repo = result.repo + + let url = git_url + '/v5/repos/' + owner + '/' + repo + '/contents/' + path + '?access_token=' + access_token + fetch(url, { + method: 'GET', + headers: header + }).then(res => res.json()) + .catch((error) => { + chrome.notifications.create(null, { + type: 'basic', + iconUrl: 'img/icon.png', + title: '获取仓库路径 ' + path, + message: error + }) + }) + .then((resp) => { + let del_url = git_url + '/v5/repos/' + owner + '/' + repo + '/contents/' + path + '?access_token=' + access_token + '&sha=' + resp['sha'] + '&message=' + message + fetch(del_url, { + method: 'DELETE', + headers: header + }).then(res => res.json()) + .catch((error) => { + console.error('Error:', error) + }) + .then((resp) => { + console.log('github delete success' + JSON.stringify(resp)) + chrome.notifications.create(null, { + type: 'basic', + iconUrl: 'img/icon.png', + title: '书签删除', + message: '远程书签 ' + path + ' 删除完毕!' + }) + }) + }) + }) + } + + /** + * API文档:https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoCommits 仓库的所有提交(分页) + * @param {*} path 包含该文件的提交 + * @param {*} page 当前的页码 + * @param {*} per_page 每页的数量,最大为 100 + */ + this.getallcommit = function (path,page,per_page,element) { + chrome.storage.local.get(git_key, function (result) { + let access_token = result.access_token + let owner = result.owner + let repo = result.repo + let url = git_url + '/v5/repos/' + owner + '/' + repo + '/commits?access_token=' + access_token + '&path=' + path + '&page=' + page + '&per_page=' + per_page + fetch(url, { + method: 'GET', + headers: header + }).then(res => res.json()) + .catch((error) => { + chrome.notifications.create(null, { + type: 'basic', + iconUrl: 'img/icon.png', + title: '仓库的所有提交 ' + path, + message: error + }) + }) + .then((resp) => { + // console.log(resp) + // return resp + resp.forEach((vds) => { + var op = document.createElement('option'); + op.setAttribute('label', vds['commit']['message']); + op.setAttribute('value', vds['sha']); + op.setAttribute('select', 'select') + element.appendChild(op); + }) + }) + }) + } + + /** + * 本地保存reponame + * @param {*} name + */ + this.saveName = function (name) { + chrome.storage.local.set({'reponame': name}, () => { + console.log('set repo name ok') + }) + } +} diff --git a/js/github.js b/js/github.js index a816c4036a734114f75d87a81b0c94e1db38bcf8..f220773768cf32bf479e03ecd0ea088bdbde0c68 100644 --- a/js/github.js +++ b/js/github.js @@ -337,3 +337,217 @@ function Github() { }) } } + +class Githubs { + constructor () { + this.url = 'https://api.github.com' + this.tags = 'tags' + this.session = 'session' + this.bookmarks = 'bookmarks' + this.key = ['username','repos','token'] + let _this = this + chrome.storage.local.get(this.key, function (result) { + _this.user = result.username + _this.repos = result.repos + _this.token = result.token + _this.headers = { 'Authorization': 'token ' + _this.token, 'Content-type': 'application/json' } + if (_this.user === '' || _this.user === undefined) { + alert('未配置认证信息') + return + } + console.log(_this.url,this.user,this.token) + }) + } + + info () { + console.log(this.user + ' is ' + this.repos) + } + + puts(filepath,data) { + return fetch(this.url + '/repos/' + this.user + '/' + this.repos + '/contents/' + filepath, { + method: 'PUT', + body: JSON.stringify(data), + headers: new Headers({ + 'Content-Type': 'application/json', + 'Authorization': 'token ' + this.token + }) + }).then(res => res.json()) + .catch((error) => { + chrome.notifications.create(null, { + type: 'basic', + iconUrl: 'img/icon.png', + title: '初始化' + filepath + '目录', + message: error + }) + }) + .then((response) => { + console.log('初始化目录' + filepath + ' 成功') + }) + } + + gets(filepath) { + return fetch(this.url + '/repos/' + this.user + '/' + this.repos + '/contents/' + filepath, { + method: 'GET', + headers: new Headers({ + 'Content-Type': 'application/json', + 'Authorization': 'token ' + this.token + }) + }).then(res => res.json()) + .catch(error => console.error('Error:', error)) + .then((result) => { + console.log('result',result) + }) + } + + deletes(filepath,data) { + return fetch(urls, { + method: 'DELETE', + body: JSON.stringify(data), + headers: new Headers({ + 'Content-Type': 'application/json', + 'Authorization': 'token ' + this.token + }) + }).then(res => res.json()) + .catch(error => console.error('Error:', error)) + .then((response) => { + console.log('github delete success' + JSON.stringify(response)) + chrome.notifications.create(null, { + type: 'basic', + iconUrl: 'img/icon.png', + title: '书签删除', + message: '远程书签 ' + filepath + ' 删除完毕!' + }) + }) + } + + /** + * 新建文件 + * @param {*} filepath + */ + create(filepath) { + var data = { + 'message': 'commit init', + 'content': 'eyJkZWZhdWx0IjpbeyJuYW1lIjoidGl0bGUiLCJ1cmwiOiJodHRwOi8vYmFpZHUuY29tIiwiaWNvbiI6IjEyMyIsInRpbWUiOiIyMDE5LTAxLTAxIn1dfQo=' + } + return new Promise((resolve,reject) => { + this.puts(filepath,data).catch(res => { + reject(res) + }).catch(res => { + resolve(res) + }) + }) + } + + /** + * 获取远程列表 + * @param {*} filepath + */ + getlist (filepath) { + return new Promise((resolve,reject) => { + this.gets(filepath).catch(res => { + reject(res) + }).catch(res => { + resolve(res) + }) + }) + } + + // 远程加载 + /** + * + * @param {*} filepath + */ + getremote (filepath) { + this.getlist(filepath).then(result =>{ + let info = JSON.parse(decodeURIComponent(window.atob(result['content']))) + + try { + addAll(info[0].children, '') + } catch (e) { + // do nothing + } finally { + chrome.notifications.create(null, { + type: 'basic', + iconUrl: 'img/icon.png', + title: '更新本地书签', + message: '同步完成 ' + }) + } + }) + } + + // todo 远程同步 + /** + * + * @param {*} filepath + * @param {*} message + */ + updateTags (filepath,message) { + return new Promise((resolve,reject) =>{ + this.getlist(filepath).then(result => { + chrome.bookmarks.getTree(re => { + if (result['message'] === 'Not Found') { + console.log('not found create it') + } + // 'content': window.btoa(encodeURIComponent(JSON.stringify(re))), + // 'content': window.btoa(unescape(encodeURIComponent(JSON.stringify(re)))), + var data = { + 'message': '全量同步bookmarks ' + message, + 'content': window.btoa(encodeURIComponent(JSON.stringify(re))), + 'sha': result['sha'] + } + return this.puts(filepath,data) + }) + }).catch(res => { + reject(res) + }).catch(res => { + resolve(res) + }) + }) + } + + /** + * + * @param {*} filepath + * @param {*} message + * @param {*} content + */ + update(filepath,message,content) { + return new Promise((resolve,reject) => { + this.getlist(filepath).then(result => { + var data = { + 'message': message, + 'content': window.btoa(content), + 'sha': result['sha'] + } + return this.puts(filepath,data) + }).catch(res => { + reject(res) + }).catch(res => { + resolve(res) + }) + }) + } + + delete(filepath,message) { + return new Promise((resolve,reject) => { + this.getlist(filepath).then(result => { + var data = { + 'message': message, + 'branch': 'master', + 'sha': result['sha'] + } + return this.deletes(filepath,data) + }).catch(res => { + reject(res) + }).catch(res => { + resolve(res) + }) + }) + } +} + +var bb = new Githubs() +bb.info() +console.log(bb.session) +bb.create('111.md') \ No newline at end of file diff --git a/js/popup.js b/js/popup.js index b165f671dbe0b1246ee913a4a407266ae39fd99f..5072523143e5331a7e4843fbfe327bc68dd2c60b 100644 --- a/js/popup.js +++ b/js/popup.js @@ -23,10 +23,10 @@ document.getElementById('commit').onclick = function () { let repos = tmp[tmp.length - 1] let token = document.getElementById('token').value - chrome.storage.local.set({ 'username': username, 'repos': repos, 'token': token }, () => { + chrome.storage.local.set({ 'version': '1', 'username': username, 'repos': repos, 'token': token, 'owner': username, 'repo': repos, 'access_token': token }, () => { let bg = chrome.extension.getBackgroundPage() - let github = new bg.Github() - github.create('bookmarks/create') + let gitee = new bg.Gitee() + gitee.create('bookmarks/create','init','init','master') chrome.notifications.create(null, { type: 'basic', iconUrl: 'img/icon.png', @@ -34,6 +34,10 @@ document.getElementById('commit').onclick = function () { message: '认证配置成功' }) }) + + chrome.storage.local.set({ 'repostatus': true }, () => { + console.log('reset repostatus === true') + }) } // 清空标签 @@ -47,8 +51,8 @@ document.getElementById('clearonline').onclick = function () { let files = document.getElementById('filename').value if (files !== '' && files !== undefined) { let bg = chrome.extension.getBackgroundPage() - let github = new bg.Github() - github.delete('bookmarks/' + files, '删除' + files) + let gitee = new bg.Gitee() + gitee.delete('bookmarks/' + files, '删除' + files) $('#message-success').innerText = '远程删除成功' clearAndFadeMessage($('#message-success')) @@ -63,8 +67,9 @@ document.getElementById('upload').onclick = function () { let files = document.getElementById('filename').value if (files !== '' && files !== undefined) { let bg = chrome.extension.getBackgroundPage() - let github = new bg.Github() - github.updateTags('bookmarks/' + files, getDate()) + let gitee = new bg.Gitee() + gitee.updateTags('bookmarks/' + files, getDate(), 'master') + gitee.saveName(files) $('#message-success').innerText = '同步上传成功' clearAndFadeMessage($('#message-success')) @@ -79,8 +84,9 @@ document.getElementById('download').onclick = function () { let files = document.getElementById('filename').value if (files !== '' && files !== undefined) { let bg = chrome.extension.getBackgroundPage() - let github = new bg.Github() - github.get('bookmarks/' + files) + let gitee = new bg.Gitee() + gitee.getrecovery('bookmarks/' + files) + gitee.saveName(files) $('#message-success').innerText = '更新下载成功' clearAndFadeMessage($('#message-success')) @@ -152,7 +158,7 @@ window.onload = function () { $('#message-error').innerText = '未配置认证信息' clearAndFadeMessage($('#message-error')) } else { - $('#repos').value = 'https://github.com/' + username + '/' + repos + $('#repos').value = 'https://gitee.com/' + username + '/' + repos $('#token').value = token switch2Main() @@ -160,6 +166,15 @@ window.onload = function () { }) // 获取目录 - let github = new bg.Github(); - github.getlist('bookmarks/', $('#greetings'), $('#count-repo')); + let gitee = new bg.Gitee(); + // gitee.getlist('bookmarks/', $('#greetings'), $('#count-repo')); + gitee.getlist('bookmarks/', 'master', document.querySelector('#greetings'), document.querySelector('#count-repo')); + + // 获取当前repo + chrome.storage.local.get(['reponame'], (result) =>{ + document.querySelector('#current-repo').innerText = result.reponame === undefined ? '无': result.reponame + if (result.reponame !== undefined) { + document.getElementById('filename').value = result.reponame + } + }) } \ No newline at end of file diff --git a/manifest.json b/manifest.json index a8ada0eb43b811b7996c3536c847d5be8143a288..f5f7a1df4ec99c6fe321b9bb86e4793790e75138 100644 --- a/manifest.json +++ b/manifest.json @@ -22,7 +22,7 @@ "default_popup": "popup.html" }, "background": { - "scripts": ["js/background.js","js/github.js","js/bookmark.js"] + "scripts": ["js/background.js","js/gitee.js","js/bookmark.js"] }, "omnibox": {"keyword": "go"}, "homepage_url": "https://gitee.com/CLCoding/chrome-bookmark" diff --git a/popup.html b/popup.html index 4ffc12b9d67ddd68b150957c39f7cf72f7dd14fa..1fed95e76460fe70d6decd647165c0570bbe3884 100644 --- a/popup.html +++ b/popup.html @@ -24,6 +24,7 @@
本地有--条书签
线上有--个备份版本
+
当前远程版本--