-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (61 loc) · 1.72 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
$(document).ready(function () {
var tabInfo = null;
var _api = null;
//检查是否设置了api地址
var checkApi = async function(){
let api = await Common.get("apiUrl");
if(!api) {
//显示 输入api设置框
$("#api_url_container").show();
$("#save_page_info").hide();
$("#save_url").click(async ()=> {
var url = $("#api_url").val();
if(!url) return Common.showMessage("api接口必须填写.");
await Common.set("apiUrl", url);
$("#save_url").html("保存成功");
});
return false;
}
_api = api;
return true;
};
//获取当前页面相关
var loadPageInfo = async function(){
return new Promise(function(resolve){
chrome.tabs.query({'active': true}, function (tabs) {
let t = tabs[0];
let tabInfo = {
title: t.title,
url: t.url,
}
resolve(tabInfo);
});
});
}
//初始化
var init = async function(){
await checkApi();
var pageInfo = await loadPageInfo();
var lastTag = await Common.get("lastTag");
$("#last_tag").val(lastTag);
$("#page_title").html(pageInfo.title);
$("#save_page").click(async () => await doSave());
var txt = pageInfo.title + "\n" + pageInfo.url;
$("textarea").html(txt);
};
//保存
var doSave = async function(){
var tag = $("#last_tag").val();
var txt = $("textarea").html();
await Common.set("lastTag", tag);
var content = tag + "\r\n" + txt;
let req = {
content: content,
};
var resp = await Common.curl(_api, req);
Common.showMessage(resp.message);
};
(async ()=>{
await init();
})();
});