From 05a1f535fe6daa5a2856b2d2c7230b254371e252 Mon Sep 17 00:00:00 2001 From: inertia Date: Thu, 13 Jun 2024 17:07:19 +0800 Subject: [PATCH] fix the method of getting tags --- _locales/en/messages.json | 3 + _locales/zh_CN/messages.json | 3 + js/oper.js | 497 +++++++++++++++++++---------------- 3 files changed, 272 insertions(+), 231 deletions(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index b23cfd9..7ffa40a 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -85,5 +85,8 @@ }, "memoFailed":{ "message": "Failed! 😭" + }, + "invalidToken":{ + "message": "Invalid token or url 😭" } } \ No newline at end of file diff --git a/_locales/zh_CN/messages.json b/_locales/zh_CN/messages.json index 1551e35..ba2402f 100644 --- a/_locales/zh_CN/messages.json +++ b/_locales/zh_CN/messages.json @@ -88,5 +88,8 @@ }, "memoFailed":{ "message": "发送失败 😭" + }, + "invalidToken":{ + "message": "无效的 token 或 url 😭" } } diff --git a/js/oper.js b/js/oper.js index 5076842..2c8cf41 100644 --- a/js/oper.js +++ b/js/oper.js @@ -11,6 +11,7 @@ function get_info(callback) { memo_lock: '', open_action: '', open_content: '', + userid: '', resourceIdList: [] }, function (items) { @@ -32,6 +33,7 @@ function get_info(callback) { returnObject.memo_lock = items.memo_lock returnObject.open_content = items.open_content returnObject.open_action = items.open_action + returnObject.userid = items.userid returnObject.resourceIdList = items.resourceIdList if (callback) callback(returnObject) @@ -45,17 +47,17 @@ get_info(function (info) { $('#blog_info').hide() } var memoNow = info.memo_lock - if(memoNow == ''){ + if (memoNow == '') { chrome.storage.sync.set( - { memo_lock: 'PUBLIC'} + { memo_lock: 'PUBLIC' } ) $("#lock-now").text(chrome.i18n.getMessage("lockPublic")) } - if(memoNow == "PUBLIC"){ + if (memoNow == "PUBLIC") { $("#lock-now").text(chrome.i18n.getMessage("lockPublic")) - }else if(memoNow == "PRIVATE"){ + } else if (memoNow == "PRIVATE") { $("#lock-now").text(chrome.i18n.getMessage("lockPrivate")) - }else if(memoNow == "PROTECTED"){ + } else if (memoNow == "PROTECTED") { $("#lock-now").text(chrome.i18n.getMessage("lockProtected")) } $('#apiUrl').val(info.apiUrl) @@ -126,7 +128,7 @@ function initDrag() { ev.preventDefault() var files = ev.dataTransfer.files || ev.target.files for (var i = 0; i < files.length; i++) { - file = files[i] + file = files[i] } uploadImage(file) } @@ -165,7 +167,7 @@ function uploadImage(data) { processData: false, contentType: false, dataType: 'json', - headers : {'Authorization':'Bearer ' + info.apiTokens}, + headers: { 'Authorization': 'Bearer ' + info.apiTokens }, success: function (data) { console.log(data) if (data.id) { @@ -173,7 +175,7 @@ function uploadImage(data) { relistNow.push(data.id) chrome.storage.sync.set( { - open_action: '', + open_action: '', open_content: '', resourceIdList: relistNow }, @@ -187,7 +189,7 @@ function uploadImage(data) { //发送失败 清空open_action(打开时候进行的操作),同时清空open_content chrome.storage.sync.set( { - open_action: '', + open_action: '', open_content: '', resourceIdList: [] }, @@ -209,40 +211,72 @@ function uploadImage(data) { } $('#saveKey').click(function () { - chrome.storage.sync.set( - { - apiUrl: $('#apiUrl').val(), - apiTokens: $('#apiTokens').val() - }, - function () { - $.message({ - message: chrome.i18n.getMessage("saveSuccess") - }) - $('#blog_info').hide() + var apiUrl = $('#apiUrl').val() + var apiTokens = $('#apiTokens').val() + // 设置请求参数 + const settings = { + async: true, + crossDomain: true, + url: apiUrl + 'api/v1/auth/status', + method: 'POST', + headers: { + 'Authorization': 'Bearer ' + apiTokens } - ) -}) + }; + + $.ajax(settings).done(function (response) { + if (response && response.id) { + // 如果响应包含用户 ID,存储 apiUrl 和 apiTokens + chrome.storage.sync.set( + { + apiUrl: apiUrl, + apiTokens: apiTokens, + userid: response.id + }, + function () { + $.message({ + message: chrome.i18n.getMessage("saveSuccess") + }); + $('#blog_info').hide(); + } + ); + } else { + // 如果响应不包含用户 ID,显示错误消息 + $.message({ + message: chrome.i18n.getMessage("invalidToken") + }); + } + }).fail(function () { + // 请求失败时显示错误消息 + $.message({ + message: chrome.i18n.getMessage("invalidToken") + }); + }); +}); $('#opensite').click(function () { get_info(function (info) { - chrome.tabs.create({url:info.apiUrl}) + chrome.tabs.create({ url: info.apiUrl }) }) }) $('#tags').click(function () { get_info(function (info) { if (info.apiUrl) { - var tagUrl = info.apiUrl+'api/v1/tag' - var tagDom = "" + var parent = "memos/-"; + // 如果不使用 user 过滤,会返回所有用户的标签 + var filter = "?filter=" + encodeURIComponent(`creator == 'users/${info.userid}'`); + var tagUrl = info.apiUrl + 'api/v1/' + parent + '/tags' + filter; + var tagDom = ""; $.ajax({ - url:tagUrl, - type:"GET", - contentType:"application/json;", - dataType:"json", - headers : {'Authorization':'Bearer ' + info.apiTokens}, - success: function(data){ - $.each(data, function(i,obj){ - tagDom += '#'+obj+'' + url: tagUrl, + type: "GET", + contentType: "application/json;", + dataType: "json", + headers: { 'Authorization': 'Bearer ' + info.apiTokens }, + success: function (data) { + $.each(data.tagAmounts, function (tag, amount) { + tagDom += '#' + tag + ''; }); tagDom += '' $("#taglist").html(tagDom).slideToggle(500) @@ -256,7 +290,7 @@ $('#tags').click(function () { }) }) -$(document).on("click","#hideTag",function () { +$(document).on("click", "#hideTag", function () { $('#taghide').slideToggle(500) }) @@ -277,75 +311,75 @@ $('#saveTag').click(function () { }) $('#lock').click(function () { - $("#lock-wrapper").toggleClass( "!hidden", 1000 ); + $("#lock-wrapper").toggleClass("!hidden", 1000); }) -$(document).on("click",".item-lock",function () { - $("#lock-wrapper").toggleClass( "!hidden", 1000 ); +$(document).on("click", ".item-lock", function () { + $("#lock-wrapper").toggleClass("!hidden", 1000); $("#lock-now").text($(this).text()) - _this = $(this)[0].dataset.type; - chrome.storage.sync.set( - {memo_lock: _this} - ) + _this = $(this)[0].dataset.type; + chrome.storage.sync.set( + { memo_lock: _this } + ) }) $('#search').click(function () { get_info(function (info) { - if (info.status) { - $("#randomlist").html('').hide() - var searchDom = "" - const pattern = $("textarea[name=text]").val() - if(pattern){ - $.ajax({ - //memos+"api/"+apiV1+"memo?creatorId="+bbMemo.creatorId+"&content="+serchText+"&limit=20"; - url:info.apiUrl+"api/v1/memo", - type:"GET", - contentType:"application/json;", - dataType:"json", - headers : {'Authorization':'Bearer ' + info.apiTokens}, - success: function(data){ - const options = {keys: ['content']}; - const fuse = new Fuse(data, options); - var searchData = fuse.search(pattern) - for(var i=0;i < searchData.length;i++){ - searchDom += '
'+dayjs(new Date(searchData[i].item.createdTs)*1000).fromNow()+'
'+searchData[i].item.content.replace(/!\[.*?\]\((.*?)\)/g,' ').replace(/\[(.*?)\]\((.*?)\)/g,' $1 ')+'
' - if(searchData[i].item.resourceList && searchData[i].item.resourceList.length > 0){ - var resourceList = searchData[i].item.resourceList; - for(var j=0;j < resourceList.length;j++){ - var restype = resourceList[j].type.slice(0,5); - var resexlink = resourceList[j].externalLink - var resLink = '',fileId='' - if(resexlink){ - resLink = resexlink - }else{ - fileId = resourceList[j].publicId || resourceList[j].filename - resLink = info.apiUrl+'o/r/'+resourceList[j].id+'/'+fileId - } - if(restype == 'image'){ - searchDom += '' - } - if(restype !== 'image'){ - searchDom += ''+resourceList[j].filename+'' + if (info.status) { + $("#randomlist").html('').hide() + var searchDom = "" + const pattern = $("textarea[name=text]").val() + if (pattern) { + $.ajax({ + //memos+"api/"+apiV1+"memo?creatorId="+bbMemo.creatorId+"&content="+serchText+"&limit=20"; + url: info.apiUrl + "api/v1/memo", + type: "GET", + contentType: "application/json;", + dataType: "json", + headers: { 'Authorization': 'Bearer ' + info.apiTokens }, + success: function (data) { + const options = { keys: ['content'] }; + const fuse = new Fuse(data, options); + var searchData = fuse.search(pattern) + for (var i = 0; i < searchData.length; i++) { + searchDom += '
' + dayjs(new Date(searchData[i].item.createdTs) * 1000).fromNow() + '
' + searchData[i].item.content.replace(/!\[.*?\]\((.*?)\)/g, ' ').replace(/\[(.*?)\]\((.*?)\)/g, ' $1 ') + '
' + if (searchData[i].item.resourceList && searchData[i].item.resourceList.length > 0) { + var resourceList = searchData[i].item.resourceList; + for (var j = 0; j < resourceList.length; j++) { + var restype = resourceList[j].type.slice(0, 5); + var resexlink = resourceList[j].externalLink + var resLink = '', fileId = '' + if (resexlink) { + resLink = resexlink + } else { + fileId = resourceList[j].publicId || resourceList[j].filename + resLink = info.apiUrl + 'o/r/' + resourceList[j].id + '/' + fileId + } + if (restype == 'image') { + searchDom += '' + } + if (restype !== 'image') { + searchDom += '' + resourceList[j].filename + '' + } } } + searchDom += '
' } - searchDom += '
' + window.ViewImage && ViewImage.init('.random-image') + $("#randomlist").html(searchDom).slideDown(500); } - window.ViewImage && ViewImage.init('.random-image') - $("#randomlist").html(searchDom).slideDown(500); - } - }); - }else{ + }); + } else { + $.message({ + message: chrome.i18n.getMessage("searchNow") + }) + } + } else { $.message({ - message: chrome.i18n.getMessage("searchNow") + message: chrome.i18n.getMessage("placeApiUrl") }) } - } else { - $.message({ - message: chrome.i18n.getMessage("placeApiUrl") - }) - } -}) + }) }) $('#random').click(function () { @@ -353,47 +387,47 @@ $('#random').click(function () { if (info.status) { $("#randomlist").html('').hide() var nowTag = $("textarea[name=text]").val().match(/#([^\s#]+)/) - if( $("#taglist").is(':visible') && nowTag[1]){ - var tagUrl = info.apiUrl+'api/v1/memo?rowStatus=NORMAL&tag='+nowTag[1] + if ($("#taglist").is(':visible') && nowTag[1]) { + var tagUrl = info.apiUrl + 'api/v1/memo?rowStatus=NORMAL&tag=' + nowTag[1] $.ajax({ - url:tagUrl, - type:"GET", - contentType:"application/json;", - dataType:"json", - headers : {'Authorization':'Bearer ' + info.apiTokens}, - success: function(data){ + url: tagUrl, + type: "GET", + contentType: "application/json;", + dataType: "json", + headers: { 'Authorization': 'Bearer ' + info.apiTokens }, + success: function (data) { let randomNum = Math.floor(Math.random() * (data.length)); var randomData = data[randomNum] randDom(randomData) } }) - }else{ - var randomUrl0 = info.apiUrl+'api/v1/memo?rowStatus=NORMAL&limit=1' + } else { + var randomUrl0 = info.apiUrl + 'api/v1/memo?rowStatus=NORMAL&limit=1' $.ajax({ - url:randomUrl0, - type:"GET", - contentType:"application/json;", - dataType:"json", - headers : {'Authorization':'Bearer ' + info.apiTokens}, - success: function(data0){ + url: randomUrl0, + type: "GET", + contentType: "application/json;", + dataType: "json", + headers: { 'Authorization': 'Bearer ' + info.apiTokens }, + success: function (data0) { var creatorId = data0[0].creatorId - var randomUrl1 = info.apiUrl+'api/v1/memo/stats?creatorId='+creatorId + var randomUrl1 = info.apiUrl + 'api/v1/memo/stats?creatorId=' + creatorId $.ajax({ - url:randomUrl1, - type:"GET", - contentType:"application/json;", - dataType:"json", - headers : {'Authorization':'Bearer ' + info.apiTokens}, - success: function(data1){ + url: randomUrl1, + type: "GET", + contentType: "application/json;", + dataType: "json", + headers: { 'Authorization': 'Bearer ' + info.apiTokens }, + success: function (data1) { let randomNum = Math.floor(Math.random() * (data1.length)) + 1; - var randomUrl2 = info.apiUrl+'api/v1/memo?rowStatus=NORMAL&limit=1&offset='+randomNum + var randomUrl2 = info.apiUrl + 'api/v1/memo?rowStatus=NORMAL&limit=1&offset=' + randomNum $.ajax({ - url:randomUrl2, - type:"GET", - contentType:"application/json;", - dataType:"json", - headers : {'Authorization':'Bearer ' + info.apiTokens}, - success: function(data2){ + url: randomUrl2, + type: "GET", + contentType: "application/json;", + dataType: "json", + headers: { 'Authorization': 'Bearer ' + info.apiTokens }, + success: function (data2) { var randomData = data2[0] randDom(randomData) } @@ -411,72 +445,72 @@ $('#random').click(function () { }) }) -function randDom(randomData){ +function randDom(randomData) { get_info(function (info) { - var randomDom = '
'+dayjs(new Date(randomData.createdTs * 1000)).fromNow()+'
'+randomData.content.replace(/!\[.*?\]\((.*?)\)/g,' ').replace(/\[(.*?)\]\((.*?)\)/g,' $1 ')+'
' - if(randomData.resourceList && randomData.resourceList.length > 0){ - var resourceList = randomData.resourceList; - for(var j=0;j < resourceList.length;j++){ - var restype = resourceList[j].type.slice(0,5); - var resexlink = resourceList[j].externalLink - var resLink = '',fileId='' - if(resexlink){ - resLink = resexlink - }else{ - fileId = resourceList[j].publicId || resourceList[j].filename - resLink = info.apiUrl+'o/r/'+resourceList[j].id+'/'+fileId - } - if(restype == 'image'){ - randomDom += '' - } - if(restype !== 'image'){ - randomDom += ''+resourceList[j].filename+'' + var randomDom = '
' + dayjs(new Date(randomData.createdTs * 1000)).fromNow() + '
' + randomData.content.replace(/!\[.*?\]\((.*?)\)/g, ' ').replace(/\[(.*?)\]\((.*?)\)/g, ' $1 ') + '
' + if (randomData.resourceList && randomData.resourceList.length > 0) { + var resourceList = randomData.resourceList; + for (var j = 0; j < resourceList.length; j++) { + var restype = resourceList[j].type.slice(0, 5); + var resexlink = resourceList[j].externalLink + var resLink = '', fileId = '' + if (resexlink) { + resLink = resexlink + } else { + fileId = resourceList[j].publicId || resourceList[j].filename + resLink = info.apiUrl + 'o/r/' + resourceList[j].id + '/' + fileId + } + if (restype == 'image') { + randomDom += '' + } + if (restype !== 'image') { + randomDom += '' + resourceList[j].filename + '' + } } } - } - randomDom += '
' - window.ViewImage && ViewImage.init('.random-image') - $("#randomlist").html(randomDom).slideDown(500); + randomDom += '
' + window.ViewImage && ViewImage.init('.random-image') + $("#randomlist").html(randomDom).slideDown(500); }) } -$(document).on("click","#random-link",function () { +$(document).on("click", "#random-link", function () { var memoId = $("#random-link").data('id'); get_info(function (info) { - chrome.tabs.create({url:info.apiUrl+"m/"+memoId}) + chrome.tabs.create({ url: info.apiUrl + "m/" + memoId }) }) }) -$(document).on("click","#random-delete",function () { -get_info(function (info) { - var memosId = $("#random-delete").data('id'); - var deleteUrl = info.apiUrl+'api/v1/memo/'+memosId - $.ajax({ - url:deleteUrl, - type:"PATCH", - data:JSON.stringify({ - 'id': memosId, - 'rowStatus': "ARCHIVED" - }), - contentType:"application/json;", - dataType:"json", - headers : {'Authorization':'Bearer ' + info.apiTokens}, - success: function(result){ - $("#randomlist").html('').hide() - $.message({ - message: chrome.i18n.getMessage("archiveSuccess") - }) - },error:function(err){//清空open_action(打开时候进行的操作),同时清空open_content - $.message({ - message: chrome.i18n.getMessage("archiveFailed") - }) - } +$(document).on("click", "#random-delete", function () { + get_info(function (info) { + var memosId = $("#random-delete").data('id'); + var deleteUrl = info.apiUrl + 'api/v1/memo/' + memosId + $.ajax({ + url: deleteUrl, + type: "PATCH", + data: JSON.stringify({ + 'id': memosId, + 'rowStatus': "ARCHIVED" + }), + contentType: "application/json;", + dataType: "json", + headers: { 'Authorization': 'Bearer ' + info.apiTokens }, + success: function (result) { + $("#randomlist").html('').hide() + $.message({ + message: chrome.i18n.getMessage("archiveSuccess") + }) + }, error: function (err) {//清空open_action(打开时候进行的操作),同时清空open_content + $.message({ + message: chrome.i18n.getMessage("archiveFailed") + }) + } + }) }) }) -}) -$(document).on("click",".item-container",function () { - var tagHtml = $(this).text()+" " +$(document).on("click", ".item-container", function () { + var tagHtml = $(this).text() + " " add(tagHtml); }) @@ -487,10 +521,10 @@ $('#newtodo').click(function () { $('#getlink').click(function () { chrome.tabs.query({ active: true, currentWindow: true }, ([tab]) => { - var linkHtml = " ["+tab.title+"]("+tab.url+") " - if(tab.url){ + var linkHtml = " [" + tab.title + "](" + tab.url + ") " + if (tab.url) { add(linkHtml); - }else{ + } else { $.message({ message: chrome.i18n.getMessage("getTabFailed") }) @@ -502,13 +536,13 @@ $('#upres').click(async function () { $('#inFile').click() }) -$('#inFile').on('change', function(data){ - var fileVal = $('#inFile').val(); +$('#inFile').on('change', function (data) { + var fileVal = $('#inFile').val(); var file = null - if(fileVal == '') { + if (fileVal == '') { return; } - file= this.files[0]; + file = this.files[0]; uploadImage(file) }); @@ -516,10 +550,10 @@ function add(str) { var tc = document.getElementById("content"); var tclen = tc.value.length; tc.focus(); - if(typeof document.selection != "undefined"){ + if (typeof document.selection != "undefined") { document.selection.createRange().text = str; - }else{ - tc.value = + } else { + tc.value = tc.value.substr(0, tc.selectionStart) + str + tc.value.substring(tc.selectionStart, tclen); @@ -532,35 +566,35 @@ $('#blog_info_edit').click(function () { $('#content_submit_text').click(function () { var contentVal = $("textarea[name=text]").val() - if(contentVal){ + if (contentVal) { sendText() - }else{ + } else { $.message({ message: chrome.i18n.getMessage("placeContent") }) } }) -function getOne(memosId){ +function getOne(memosId) { get_info(function (info) { - if (info.apiUrl) { - $("#randomlist").html('').hide() - var getUrl = info.apiUrl+'api/v1/memo/'+memosId - $.ajax({ - url:getUrl, - type:"GET", - contentType:"application/json;", - dataType:"json", - headers : {'Authorization':'Bearer ' + info.apiTokens}, - success: function(data){ - randDom(data) - } - }) - } else { - $.message({ - message: chrome.i18n.getMessage("placeApiUrl") - }) - } + if (info.apiUrl) { + $("#randomlist").html('').hide() + var getUrl = info.apiUrl + 'api/v1/memo/' + memosId + $.ajax({ + url: getUrl, + type: "GET", + contentType: "application/json;", + dataType: "json", + headers: { 'Authorization': 'Bearer ' + info.apiTokens }, + success: function (data) { + randDom(data) + } + }) + } else { + $.message({ + message: chrome.i18n.getMessage("placeApiUrl") + }) + } }) } @@ -576,46 +610,47 @@ function sendText() { var showTag = info.showtag var nowTag = $("textarea[name=text]").val().match(/(#[^\s#]+)/) var sendvisi = info.memo_lock || '' - if(nowTag){ - if(nowTag[1] == showTag){ + if (nowTag) { + if (nowTag[1] == showTag) { sendvisi = 'PUBLIC' - }else if(nowTag[1] == hideTag){ + } else if (nowTag[1] == hideTag) { sendvisi = 'PRIVATE' } } $.ajax({ - url:info.apiUrl+'api/v1/memos', - type:"POST", - data:JSON.stringify({ + url: info.apiUrl + 'api/v1/memos', + type: "POST", + data: JSON.stringify({ 'content': content, 'visibility': sendvisi, 'resourceIdList': info.resourceIdList || [], }), - contentType:"application/json;", - dataType:"json", - headers : {'Authorization':'Bearer ' + info.apiTokens}, - success: function(data){ - //发送成功 - getOne(data.id) - chrome.storage.sync.set( - { open_action: '', open_content: '',resourceIdList:''}, - function () { - $.message({ - message: chrome.i18n.getMessage("memoSuccess") - }) - //$("#content_submit_text").removeAttr('disabled'); - $("textarea[name=text]").val('') - } + contentType: "application/json;", + dataType: "json", + headers: { 'Authorization': 'Bearer ' + info.apiTokens }, + success: function (data) { + //发送成功 + getOne(data.id) + chrome.storage.sync.set( + { open_action: '', open_content: '', resourceIdList: '' }, + function () { + $.message({ + message: chrome.i18n.getMessage("memoSuccess") + }) + //$("#content_submit_text").removeAttr('disabled'); + $("textarea[name=text]").val('') + } ) - },error:function(err){//清空open_action(打开时候进行的操作),同时清空open_content - chrome.storage.sync.set( - { open_action: '', open_content: '',resourceIdList:'' }, - function () { - $.message({ - message: chrome.i18n.getMessage("memoFailed") - }) - } - )}, + }, error: function (err) {//清空open_action(打开时候进行的操作),同时清空open_content + chrome.storage.sync.set( + { open_action: '', open_content: '', resourceIdList: '' }, + function () { + $.message({ + message: chrome.i18n.getMessage("memoFailed") + }) + } + ) + }, }) } else { $.message({