/**
* open_action: 打开这个页面执行的操作
* open_text:打开这页面需要复原的输入框的内容
*/
get_info(function (info) {
if (info.status) {
//已经有绑定信息了,折叠
$('#blog_info').hide()
}
var memoNow = info.memo_lock
if(memoNow == ''){
chrome.storage.sync.set(
{ memo_lock: 'PUBLIC'}
)
$("#lock-now").text("所有人可见")
}
if(memoNow == "PUBLIC"){
$("#lock-now").text("所有人可见")
}else if(memoNow == "PRIVATE"){
$("#lock-now").text("仅自己可见")
}else if(memoNow == "PROTECTED"){
$("#lock-now").text("登录用户可见")
}
$('#apiUrl').val(info.apiUrl)
if (info.open_action === 'upload_image') {
//打开的时候就是上传图片
uploadImage(info.open_content)
} else {
$("textarea[name=text]").val(info.open_content)
}
//从localstorage 里面读取数据
setTimeout(get_info, 1)
})
//监听输入结束,保存未发送内容到本地
$("textarea[name=text]").blur(function () {
chrome.storage.sync.set(
{ open_action: 'save_text', open_content: $("textarea[name=text]").val() }
)
})
$("textarea[name=text]").on('keydown', function (ev) {
if (ev.code === 'Enter' && (ev.ctrlKey || ev.metaKey)) {
$('#content_submit_text').click()
}
})
//监听拖拽事件,实现拖拽到窗口上传图片
initDrag()
//监听复制粘贴事件,实现粘贴上传图片
document.addEventListener('paste', function (e) {
let photo = null
if (e.clipboardData.files[0]) {
photo = e.clipboardData.files[0]
} else if (e.clipboardData.items[0] && e.clipboardData.items[0].getAsFile()) {
photo = e.clipboardData.items[0].getAsFile()
}
if (photo != null) {
uploadImage(photo)
}
})
function initDrag() {
var file = null
var obj = $("textarea[name=text]")[0]
obj.ondragenter = function (ev) {
if (ev.target.className === 'common-editor-inputer') {
$.message({
message: '拖拽到窗口上传该图片',
autoClose: false
})
$('body').css('opacity', 0.3)
}
ev.dataTransfer.dropEffect = 'copy'
}
obj.ondragover = function (ev) {
ev.preventDefault() //防止默认事件拖入图片 放开的时候打开图片了
ev.dataTransfer.dropEffect = 'copy'
}
obj.ondrop = function (ev) {
$('body').css('opacity', 1)
ev.preventDefault()
var files = ev.dataTransfer.files || ev.target.files
for (var i = 0; i < files.length; i++) {
file = files[i]
}
uploadImage(file)
}
obj.ondragleave = function (ev) {
ev.preventDefault()
if (ev.target.className === 'common-editor-inputer') {
console.log('ondragleave' + ev.target.tagName)
$.message({
message: '取消上传'
})
$('body').css('opacity', 1)
}
}
}
let relistNow = []
function uploadImage(data) {
//显示上传中的动画……
$.message({
message: '上传图片中……',
autoClose: false
})
//根据data判断是图片地址还是base64加密的数据
get_info(function (info) {
const formData = new FormData()
if (info.status) {
formData.append('file', data)
$.ajax({
url: info.apiUrl.replace(/api\/memo/,'api/resource/blob'),
data: formData,
type: 'post',
cache: false,
processData: false,
contentType: false,
dataType: 'json',
success: function (result) {
console.log(result)
if (result.data.id) {
//获取到图片
relistNow.push(result.data.id)
chrome.storage.sync.set(
{
open_action: '',
open_content: '',
resourceIdList: relistNow
},
function () {
$.message({
message: '上传成功'
})
}
)
} else {
//发送失败
//清空open_action(打开时候进行的操作),同时清空open_content
chrome.storage.sync.set(
{
open_action: '',
open_content: '',
resourceIdList: []
},
function () {
$.message({
message: '上传图片失败'
})
}
)
}
}
})
} else {
$.message({
message: '所需要信息不足,请先填写好绑定信息'
})
}
})
}
$('#saveKey').click(function () {
// 保存数据
chrome.storage.sync.set(
{
apiUrl: $('#apiUrl').val()
},
function () {
$.message({
message: '保存信息成功'
})
$('#blog_info').hide()
}
)
})
$('#getone').click(function () {
getOne()
})
function getOne(){
get_info(function (info) {
if (info.apiUrl) {
$("#randomlist").html('').hide()
var getUrl = info.apiUrl+'&rowStatus=NORMAL&limit=1'
$.get(getUrl,function(data){
var getData = data.data[0]
randDom(getData)
});
} else {
$.message({
message: '请先填写好 API 链接'
})
}
})
}
$('#tags').click(function () {
get_info(function (info) {
if (info.apiUrl) {
var tagUrl = info.apiUrl.replace(/api\/memo/,'api/tag')
var tagDom = ""
$.get(tagUrl,function(data){
var arrData = data.data
$.each(arrData, function(i,obj){
tagDom += '#'+obj+''
});
//console.log(tagDom)
$("#taglist").html(tagDom).slideToggle(500)
});
} else {
$.message({
message: '请先填写好 API 链接'
})
}
})
})
$('#lock').click(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}
)
})
dayjs.extend(window.dayjs_plugin_relativeTime)
dayjs.locale('zh-cn')
$('#search').click(function () {
get_info(function (info) {
if (info.status) {
$("#randomlist").html('').hide()
var searchDom = ""
const pattern = $("textarea[name=text]").val()
if(pattern){
$.get( info.apiUrl ,function(data){
const options = {keys: ['content']};
const fuse = new Fuse(data.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 = ''
if(resexlink){
resLink = resexlink
}else{
resLink = info.apiUrl.replace(/api\/memo.*/,'')+'o/r/'+resourceList[j].id+'/'+resourceList[j].filename
}
if(restype == 'image'){
searchDom += '

'
}
if(restype !== 'image'){
searchDom += '
'+resourceList[j].filename+''
}
}
}
searchDom += '
'
}
window.ViewImage && ViewImage.init('.random-image')
$("#randomlist").html(searchDom).slideDown(500);
});
}else{
$.message({
message: '想搜点啥?'
})
}
} else {
$.message({
message: '请先填写好 API 链接'
})
}
})
})
$('#random').click(function () {
get_info(function (info) {
if (info.status) {
$("#randomlist").html('').hide()
var nowTag = $("textarea[name=text]").val().replace(/#([^\s#]+)/,'$1') ;
if( $("#taglist").is(':visible') && nowTag){
var tagUrl = info.apiUrl+'&rowStatus=NORMAL&tag='+nowTag
$.get(tagUrl,function(data){
let randomNum = Math.floor(Math.random() * (data.data.length));
var randomData = data.data[randomNum]
randDom(randomData)
})
}else{
var randomUrl0 = info.apiUrl+'&rowStatus=NORMAL&limit=1'
$.get(randomUrl0,function(data){
var creatorId = data.data[0].creatorId
var randomUrl1 = info.apiUrl.replace(/api\/memo.*/,'api/memo/stats?creatorId=')+creatorId
$.get(randomUrl1,function(data){
console.log(data.data.length)
let randomNum = Math.floor(Math.random() * (data.data.length)) + 1;
var randomUrl2 = info.apiUrl+'&rowStatus=NORMAL&limit=1&offset='+randomNum
$.get(randomUrl2,function(data){
var randomData = data.data[0]
randDom(randomData)
});
});
});
}
} else {
$.message({
message: '请先填写好 API 链接'
})
}
})
})
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 = ''
if(resexlink){
resLink = resexlink
}else{
resLink = info.apiUrl.replace(/api\/memo.*/,'')+'o/r/'+resourceList[j].id+'/'+resourceList[j].filename
}
if(restype == 'image'){
randomDom += '

'
}
if(restype !== 'image'){
randomDom += '
'+resourceList[j].filename+''
}
}
}
randomDom += '
'
window.ViewImage && ViewImage.init('.random-image')
$("#randomlist").html(randomDom).slideDown(500);
})
}
$(document).on("click","#random-link",function () {
var memoId = $("#random-link").data('id');
get_info(function (info) {
chrome.tabs.create({url:info.apiUrl.replace(/api\/memo.*/,'')+"m/"+memoId})
})
})
$(document).on("click","#random-delete",function () {
get_info(function (info) {
var memosId = $("#random-delete").data('id');
var deleteUrl = info.apiUrl.replace(/api\/memo(.*)/,'api/memo/'+memosId+'$1')
$.ajax({
url:deleteUrl,
type:"PATCH",
data:JSON.stringify({
'id': memosId,
'rowStatus': "ARCHIVED"
}),
contentType:"application/json;",
dataType:"json",
success: function(result){
$("#randomlist").html('').hide()
$.message({
message: '归档成功!😊'
})
},error:function(err){//清空open_action(打开时候进行的操作),同时清空open_content
$.message({
message: '网络问题,归档失败!😭'
})
}
})
})
})
$(document).on("click",".item-container",function () {
var tagHtml = $(this).text()+" "
add(tagHtml);
})
$('#newtodo').click(function () {
var tagHtml = "\n- [ ] "
add(tagHtml);
})
$('#getlink').click(function () {
chrome.tabs.query({ active: true, currentWindow: true }, ([tab]) => {
var linkHtml = " ["+tab.title+"]("+tab.url+") "
if(tab.url){
add(linkHtml);
}else{
$.message({message: '获取失败 😂'})
}
})
})
$('#upres').click(async function () {
$('#inFile').click()
})
$('#inFile').on('change', function(data){
var fileVal = $('#inFile').val();
var file = null
if(fileVal == '') {
return;
}
file= this.files[0];
uploadImage(file)
});
function add(str) {
var tc = document.getElementById("content");
var tclen = tc.value.length;
tc.focus();
if(typeof document.selection != "undefined"){
document.selection.createRange().text = str;
}else{
tc.value =
tc.value.substr(0, tc.selectionStart) +
str +
tc.value.substring(tc.selectionStart, tclen);
}
}
$('#blog_info_edit').click(function () {
$('#blog_info').slideToggle()
})
function get_info(callback) {
chrome.storage.sync.get(
{
apiUrl: '',
memo_lock: 'Public',
open_action: '',
open_content: '',
resourceIdList: []
},
function (items) {
var flag = false
var returnObject = {}
if (items.apiUrl === '' || items.repo === '') {
flag = false
} else {
flag = true
}
returnObject.status = flag
returnObject.apiUrl = items.apiUrl
returnObject.memo_lock = items.memo_lock
returnObject.open_content = items.open_content
returnObject.open_action = items.open_action
returnObject.resourceIdList = items.resourceIdList
if (callback) callback(returnObject)
}
)
}
//发送操作
$('#content_submit_text').click(function () {
var contentVal = $("textarea[name=text]").val()
if(contentVal){
sendText()
}else{
$.message({message: '写点什么,再记呗?'})
}
})
function sendText() {
get_info(function (info) {
if (info.status) {
//信息满足了
$.message({message: '发送中~~'})
//$("#content_submit_text").attr('disabled','disabled');
let content = $("textarea[name=text]").val()
$.ajax({
url:info.apiUrl,
type:"POST",
data:JSON.stringify({
'content': content,
'visibility': info.memo_lock || '',
'resourceIdList': info.resourceIdList || [],
}),
contentType:"application/json;",
dataType:"json",
success: function(result){
//发送成功
getOne()
chrome.storage.sync.set(
{ open_action: '', open_content: '',resourceIdList:''},
function () {
$.message({
message: '发送成功!😊'
})
//$("#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: '网络问题,发送失败!😭(记得点下小锁图标,设置一下状态哦)'
})
}
)},
})
} else {
$.message({
message: '请先填写好 API 链接'
})
}
})
}