Skip to content

Commit

Permalink
添加contextMenus快速添加书签与备忘
Browse files Browse the repository at this point in the history
  • Loading branch information
luchenqun committed May 14, 2018
1 parent 3b3b7c8 commit 23bfc9d
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 79 deletions.
1 change: 1 addition & 0 deletions background.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<div class="container">
<h1>I love this world</h1>
</div>
<script type="text/javascript" src="js/externe/jquery-1.8.3.js"></script>
<script type="text/javascript" src="js/background.js"></script>
</body>
</html>
123 changes: 117 additions & 6 deletions js/background.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,128 @@

// 预留一个方法给popup调用
function showMsg(body, title, time) {
var note = new Notification(title || "通知", {
dir: "auto",
tag: "bookmark",
body: body,
var note = new Notification(title || "通知", {
dir: "auto",
tag: "bookmark",
body: body,
icon: "img/note.png",
renotify: true,
});

if(time){
if (time) {
setTimeout(() => {
note.close();
}, time);
}
}

function jqAjax(url, type, data, successCallback, errorCallback, beforeSendCallback, completeCallback) {
// console.log(url, type, data);
$.ajax({
url: url,
type: type, //GET POST
contentType: "application/json", //必须有
async: true, //或false,是否异步
data: data,
timeout: 3000, //超时时间
dataType: 'json', //返回的数据格式:json/xml/html/script/jsonp/text
success: function (data, textStatus, jqXHR) {
successCallback && successCallback(data, textStatus, jqXHR)
},
error: function (xhr, textStatus) {
errorCallback && errorCallback(xhr, textStatus)
},
beforeSend: function (xhr) {
beforeSendCallback && beforeSendCallback(xhr)
},
complete: function () {
completeCallback && completeCallback();
}
})
}

function init() {
jqAjax("http://mybookmark.cn/api/tags/", "GET", {}, function (data, textStatus, jqXHR) {
chrome.contextMenus.removeAll();

const contextPageAddUrl = "page";
const contextSelectionAddNote = "selection";

var parentIdPageAddUrl = chrome.contextMenus.create({
"title": "添加当前网址到书签系统",
"id": "PageAddUrl",
"contexts": [contextPageAddUrl],
});

var parentIdSelectionAddNote = chrome.contextMenus.create({
"title": "添加备忘到系统",
"id": "SelectionAddNote",
"contexts": [contextSelectionAddNote],
});

data.sort((a, b) => {
return (a.last_use > b.last_use) ? -1 : 1;
})

for (const tag of data) {
chrome.contextMenus.create({
"title": tag.name,
"id": "u" + tag.id.toString(),
"contexts": [contextPageAddUrl],
"parentId": parentIdPageAddUrl,
"onclick": function (info, tab) {
let url = "http://mybookmark.cn/api/addBookmark/";
let params = {
id: "",
url: info.pageUrl,
title: tab.title,
public: '1',
tags: [tag.id],
description: ""
}
jqAjax(url, "POST", JSON.stringify({
params: params
}), function (data, textStatus, jqXHR) {
if (data.title) {
var msg = '[ ' + data.title + ' ] 添加成功!\n' + (data.update ? '系统检测到该书签之前添加过,只更新链接,描述,标题,分类。创建日期与最后点击日期不更新!' : '') + '\n窗口 3 秒后自动关闭。';
showMsg(msg, "书签添加成功", 3000);
} else {
showMsg('[ ' + params.title + ' ] 添加失败', "书签添加失败", 3000);
}
}, function (xhr, textStatus) {
showMsg('[ ' + params.title + ' ] 添加失败', "书签添加失败", 3000);
})
init();
}
});

chrome.contextMenus.create({
"title": tag.name,
"id": "n" + tag.id.toString(),
"contexts": [contextSelectionAddNote],
"parentId": parentIdSelectionAddNote,
"onclick": function (info, tab) {
let url = "http://mybookmark.cn/api/addNote/";
let params = {
tag_id: tag.id,
content: info.selectionText,
}
jqAjax(url, "POST", JSON.stringify({
params: params
}), function (data, textStatus, jqXHR) {
var brief = params.content.length > 60 ? (params.content.substring(0, 60) + ' ......') : (params.content);
if (data.retCode === 0) {
var msg = '备忘 [ ' + brief + ' ] 添加成功!\n';
showMsg(msg, "备忘录添加成功", 3000);
} else {
showMsg('备忘 [ ' + brief + ' ] 添加失败', "备忘录添加失败!", 6000);
}
}, function (xhr, textStatus) {
showMsg('备忘 [ ' + brief + ' ] 添加失败', "备忘录添加失败!", 6000);
})
}
});
}
})
}

init();
2 changes: 1 addition & 1 deletion js/content-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// 注意,必须设置了run_at=document_start 此段代码才会生效
document.addEventListener('DOMContentLoaded', function () {
// 注入自定义JS
injectCustomJs();
// injectCustomJs();
});

// 向页面注入JS
Expand Down
112 changes: 41 additions & 71 deletions js/popup.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,9 @@
function jqAjax(url, type, data, successCallback, errorCallback, beforeSendCallback, completeCallback) {
console.log(url, type, data);
$.ajax({
url: url,
type: type, //GET POST
contentType: "application/json", //必须有
async: true, //或false,是否异步
data: data,
timeout: 3000, //超时时间
dataType: 'json', //返回的数据格式:json/xml/html/script/jsonp/text
success: function (data, textStatus, jqXHR) {
successCallback && successCallback(data, textStatus, jqXHR)
},
error: function (xhr, textStatus) {
errorCallback && errorCallback(xhr, textStatus)
},
beforeSend: function (xhr) {
beforeSendCallback && beforeSendCallback(xhr)
},
complete: function () {
completeCallback && completeCallback();
}
})
}

(function (window) {
chrome.tabs.getSelected(null, function (tab) {
console.log(tab);
var bg = chrome.extension.getBackgroundPage();
// console.log(tab);
var tags = [];
var selectedTags = new Set();
var selectedTag = null;
var userId = null;
var login = false;
var firstSelectTag = true;
Expand All @@ -38,55 +14,46 @@ function jqAjax(url, type, data, successCallback, errorCallback, beforeSendCallb

let url = 'http://mybookmark.cn/api/tags/';

jqAjax(url, 'GET', {}, function(_tags, textStatus, jqXHR){
bg.jqAjax(url, 'GET', {}, function (_tags, textStatus, jqXHR) {
login = true;
tags = _tags;
tags.sort((a, b) => {
return (a.last_use > b.last_use) ? -1 : 1;
})
console.log(tags);
for (let tag of tags) {
$('#js-add-tag').before(`<div class="ui label js-tag" id="${tag.id}" style="margin:3px 10px 8px 0px;cursor:default;">${tag.name}</div>`);
}
}, function(xhr, textStatus){
if(xhr.status === 401){
}, function (xhr, textStatus) {
if (xhr.status === 401) {
toastr.error('您必须先登陆!3秒后自动跳转到登陆页面。', "错误");
setTimeout(() => {
chrome.tabs.create({ url: 'http://mybookmark.cn/#/login' });
chrome.tabs.create({
url: 'http://mybookmark.cn/#/login'
});
}, 3000);
login = false;
}
}, null, function(){
}, null, function () {
$(".ui.inverted.dimmer").removeClass("active");
if (tags.length > 0) {
$("#" + tags[0].id).addClass("green");
selectedTags.add(tags[0].id);
selectedTag = tags[0].id;
}

$("#js-add-tag").click(function () {
toastr.info('请到网站分类页面添加分类,3秒后自动打开新的网页。', "提示");
setTimeout(() => {
chrome.tabs.create({ url: 'http://mybookmark.cn/#/tags' });
chrome.tabs.create({
url: 'http://mybookmark.cn/#/tags'
});
window.close();
}, 3000);
});

$(".js-tag").click(function () {
if ($(this).hasClass("green")) {
$(this).removeClass("green");
selectedTags.delete($(this).attr("id"));
} else {
if ($(".js-tag.green").length >= 3) {
toastr.error('您至少要选择一个分类!最多选择三个分类!如果暂时没想到放到哪个分类,可以先选择未分类。', "错误");
} else {
if(firstSelectTag){
$("#"+tags[0].id).removeClass("green");
selectedTags.delete(tags[0].id);
}
$(this).addClass("green");
selectedTags.add($(this).attr("id"));
firstSelectTag = false;
}
}
$(".js-tag.green").removeClass("green");
selectedTag = $(this).attr("id");
$("#" + selectedTag).addClass("green");
});
})

Expand All @@ -95,10 +62,12 @@ function jqAjax(url, type, data, successCallback, errorCallback, beforeSendCallb
});

$('.js-send-bookmark').click(() => {
if(!login){
if (!login) {
toastr.error('您必须先登陆!3秒后自动跳转到登陆页面。', "错误");
setTimeout(() => {
chrome.tabs.create({ url: 'http://mybookmark.cn/#/login' });
chrome.tabs.create({
url: 'http://mybookmark.cn/#/login'
});
}, 3000);
}

Expand All @@ -108,27 +77,28 @@ function jqAjax(url, type, data, successCallback, errorCallback, beforeSendCallb
url: $("#js-url").val(),
title: $("#js-title").val(),
public: $('.ui.checkbox.js-public').checkbox('is checked') ? '1' : '0',
tags: Array.from(selectedTags),
tags: [selectedTag],
description: $("#js-desc").val()
}

if (!/http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/.test(params.url)) {
toastr.error('检撤到您的书签链接非法,是否忘记加http或者https了?建议直接从打开浏览器地址栏复制出来直接粘贴到输入框。', "错误");
return;
}
if (params.tags.length < 1 || params.tags.length > 3) {
toastr.error('您至少要选择一个分类!最多选择三个分类!如果暂时没想到放到哪个分类,可以先选择未分类', "错误");
if (!selectedTag) {
toastr.error('您必须要选择一个分类!可新增分类,如果暂时没想到放到哪个分类,可以先选择未分类', "错误");
return;
}
if (!params.title) {
toastr.error('书签标题不能为空!', "错误");
return;
}

jqAjax(url, "POST", JSON.stringify({params:params}), function (data, textStatus, jqXHR) {
bg.jqAjax(url, "POST", JSON.stringify({
params: params
}), function (data, textStatus, jqXHR) {
if (data.title) {
var msg = '[ ' + data.title + ' ] 添加成功!\n' + (data.update ? '系统检测到该书签之前添加过,只更新链接,描述,标题,分类。创建日期与最后点击日期不更新!' : '') + '\n窗口 3 秒后自动关闭。';
var bg = chrome.extension.getBackgroundPage();
bg.showMsg(msg, "书签添加成功", 3000);
window.close();
} else {
Expand All @@ -137,39 +107,41 @@ function jqAjax(url, type, data, successCallback, errorCallback, beforeSendCallb
}, function (xhr, textStatus) {
toastr.error('[ ' + params.title + ' ] 添加失败', "提示");
})
bg.init();
});

$('.js-send-note').click(() => {
if(!login){
if (!login) {
toastr.error('您必须先登陆!3秒后自动跳转到登陆页面。', "错误");
setTimeout(() => {
chrome.tabs.create({ url: 'http://mybookmark.cn/#/login' });
chrome.tabs.create({
url: 'http://mybookmark.cn/#/login'
});
}, 3000);
}

var tags = Array.from(selectedTags);
if (tags.length !== 1 ) {
toastr.error('您至少且最多必须选择一个分类!', "错误");
if (!selectedTag) {
toastr.error('您必须选择一个分类!', "错误");
return;
}

var url = "http://mybookmark.cn/api/addNote/";
var params = {
tag_id: tags[0],
tag_id: selectedTag,
content: $("#js-desc").val(),
}

if(!params.content) {
if (!params.content) {
toastr.error('请输入备忘内容!', "错误");
return;
}

jqAjax(url, "POST", JSON.stringify({params:params}), function (data, textStatus, jqXHR) {
console.log(data);
bg.jqAjax(url, "POST", JSON.stringify({
params: params
}), function (data, textStatus, jqXHR) {
var brief = params.content.length > 60 ? (params.content.substring(0, 60) + ' ......') : (params.content);
if (data.retCode === 0) {
var msg = '备忘 [ ' + brief + ' ] 添加成功!\n';
var bg = chrome.extension.getBackgroundPage();
bg.showMsg(msg, "备忘录添加成功", 3000);
window.close();
} else {
Expand All @@ -180,6 +152,4 @@ function jqAjax(url, type, data, successCallback, errorCallback, beforeSendCallb
})
});
});
})(window);


})(window);
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "书签快速添加",
"version": "1.0.1",
"version": "1.0.2",
"description": "快速将书签添加到系统 http://mybookmark.cn/ 中",
"icons": {
"16": "img/icon.png",
Expand Down

0 comments on commit 23bfc9d

Please sign in to comment.