-
Notifications
You must be signed in to change notification settings - Fork 15
/
picktrue.user.js
136 lines (124 loc) · 3.64 KB
/
picktrue.user.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// ==UserScript==
// @name PickTrueBrowser
// @author [email protected]
// @version 0.0.4
// @namespace tools
// @description A tool to get meta info form ArtStation within browser to provide downloading service.
// @match *://*/*
// @grant GM_xmlhttpRequest
// @require https://code.jquery.com/jquery-1.12.4.min.js
// @run-at context-menu
// ==/UserScript==
let utils = {
isFirefox: function () {
return (navigator.userAgent.indexOf("Firefox") !== -1)
},
isChrome: function () {
return (navigator.userAgent.indexOf("Chrome") !== -1)
}
};
let logger = {
info: function(...args) {
console.log("[PickTrue]: ", ...args);
}
};
let BrowserClient = function () {
function fetchUrl(url, callback) {
logger.info("Fetching url:", url);
return $.get(url, callback);
}
return {
fetchUrl: fetchUrl,
}
};
let RequestProxy = function () {
let client = BrowserClient();
function submitTask(request_url, respData, callback) {
logger.info("Submit response:", respData);
let request_data = JSON.stringify(respData);
let data = JSON.stringify(
{
request_url: request_url,
response: request_data
}
)
let details = {
url: "http://localhost:2333/tasks/submit/",
data: data,
method: "POST",
onloadend: function (data) {
logger.info("Submit response done: ", data);
callback()
},
};
return GM_xmlhttpRequest(details);
}
function getTask() {
let details = {
url: "http://localhost:2333/tasks/",
method: "GET",
onloadend: function (resp) {
logger.info("Get task: ", resp);
let data = JSON.parse(resp.responseText);
if (data.length <= 0){
return getTask()
} else {
client.fetchUrl(
data[0],
function (respData) {
submitTask(data[0], respData, getTask)
},
)
}
},
};
return GM_xmlhttpRequest(details);
}
return {
getTask: getTask,
submitTask: submitTask,
};
};
function entry() {
alert("请确保已经启动了PickTrue客户端。将要解析当前用户的所有图集并将下载地址发送PickTrue下载器,确认后将立即开始。");
let proxy = RequestProxy();
proxy.getTask();
}
function _setUpContextMenuFirefox(entryFn) {
var menu = document.body.appendChild(document.createElement("menu"));
var html = document.documentElement;
if (html.hasAttribute("contextmenu")) {
// We don't want to override web page context menu if any
var contextmenu = $("#" + html.getAttribute("contextmenu"));
contextmenu[0].appendChild(menu); // Append to web page context menu
} else {
html.setAttribute("contextmenu", "userscript-picktrue-context-menu");
}
menu.outerHTML = '<menu id="userscript-picktrue-context-menu"\
type="context">\
<menuitem id="userscript-picktrue-menuitem"\
label="发送相册到PickTrue并下载">\
</menuitem>\
</menu>';
if ("contextMenu" in html && "HTMLMenuItemElement" in window) {
var menuitem = $("#userscript-picktrue-menuitem")[0];
menuitem.addEventListener("click", entryFn, false);
}
}
function _setUpContextMenuChrome(entryFn) {
$(document).on("contextmenu", function (e) {
if (e.ctrlKey){
entryFn()
}
});
}
function setUpContextMenu(entryFn) {
if (utils.isFirefox()) {
_setUpContextMenuFirefox(entryFn);
} else if (utils.isChrome()) {
_setUpContextMenuChrome(entryFn);
} else {
alert("Unsupported browser " + navigator.userAgent);
}
}
setUpContextMenu(entry);