-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCnBlogImageUp.js
309 lines (257 loc) · 10.8 KB
/
CnBlogImageUp.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// ==UserScript==
// @name CnBlog_AUp博图通路
// @namespace https://www.cnblogs.com/chary
// @version 0.0.1
// @description 博客图
// @author CharyGao
// @match https://i.cnblogs.com/posts/edit*
// @require https://cdn.bootcdn.net/ajax/libs/axios/0.19.2/axios.min.js
// @require https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js
// @grant unsafeWindow
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_xmlhttpRequest
// @grant GM_openInTab
// @grant GM_setClipboard
// @grant GM_registerMenuCommand
// @grant GM_addStyle
// @grant GM_download
// @run-at document-end
// ==/UserScript==
//</editor-fold>
//<editor-fold desc="Style">
// noinspection JSUnresolvedFunction
GM_addStyle(
// language=CSS
`/*noinspection CssUnusedSymbol*/
div#showTips {
top: 20px;
left: 20px;
position: absolute;
padding: 3px 5px;
background: powderblue;
transition: opacity 800ms ease;
opacity: 0;
font-size: 12px;
margin: 0 auto;
text-align: center;
width: 350px;
height: auto;
color: darkblue;
}
`);
//</editor-fold>
(function () {
//<editor-fold desc="0.Fields">
let logCount = 0;
let totalImgCount = 0;
let downImgCount = 0;
let updateImgCount = 0;
let innerOldHTML = "";
//</editor-fold>
//<editor-fold desc="1.入口">
setTimeout(function () {
try {
setUi();
} catch (e) {
myLog('err:', e);
}
}, 5000); //页面加载完成后延时2秒执行
//</editor-fold>
//<editor-fold desc="2.设置UI">
function setUi() {
let rootHeaderNode = document.getElementById('Editor_Edit_EditorBody_toolbargroup');
if (rootHeaderNode == null) throw "aUp err!->妈的又改版了!";
let htmlSpanElement = document.createElement("span");
htmlSpanElement.id = "uploadOutPic";
htmlSpanElement.innerText = "点击同步外部图片";
rootHeaderNode.appendChild(htmlSpanElement);
htmlSpanElement.addEventListener("click", () => {
synchronizeImage();
})
}
//</editor-fold>
//<editor-fold desc="3.同步过程">
function synchronizeImage() {
let editorBody = document.getElementById("Editor_Edit_EditorBody_ifr");
innerOldHTML = editorBody.contentDocument.body.innerHTML;
let bodyImages = editorBody.contentDocument.body.querySelectorAll("img");
totalImgCount = bodyImages.length;
myLog(`totalImgCount:${totalImgCount}`);
bodyImages.forEach(img => {
processOneImage(img);
})
showTips(`正在同步!请稍等,totalImgCount:${totalImgCount}`, 300, 1);
}
//</editor-fold>
//<editor-fold desc="4.处理一张图片">
function processOneImage(img) {
let imgHref = img.src;
if (imgHref.indexOf('cnblogs.com') > 0) {
myLog(`[skip] ${imgHref}`);
} else {
myLog(`[handle] ${imgHref}`);
if (imgHref.startsWith("http")) {
downloadImage(imgHref);
} else {
myLog(`${imgHref},if (imgHref.startsWith("http")) == false`);
}
}
}
//</editor-fold>
//<editor-fold desc="5.下载图片">
function downloadImage(imgHref) {
// noinspection JSUnresolvedFunction,JSUnusedGlobalSymbols
GM_xmlhttpRequest({
method: "GET",
headers: {
"Content-Type": "image/png",
"User-Agent": "Mozilla/5.0 AppleWebKit/537.36 Chrome/81.0.4044.138 Safari/537.36",
},
url: imgHref,
cookie: document.cookie,
responseType: "blob",
timeout: 5000,//5s
onload: (imageDataBody) => {
downImgCount++;
let tempo = `downImgCount/totalImgCount=${downImgCount}/${totalImgCount}`;
// noinspection JSUnresolvedVariable
let headerJson = keyValuePairStringToLowercaseKeyJson(imageDataBody.responseHeaders);
let imageType = "image/png";
if (headerJson !== undefined && "content-type" in headerJson) {
imageType = headerJson["content-type"];
}
myLog(imageDataBody.response);
let bodyFormData = new FormData();
bodyFormData.append("imageFile",
new Blob([imageDataBody.response], {
filename: "image.png",
type: imageType,//"image/png",//
}
)
);
bodyFormData.set("host", "www.cnblogs.com");
bodyFormData.set("uploadType", "Paste");
uploadImage(imgHref, bodyFormData);
myLog(`${imgHref},${tempo}`);
},
onerror: (imageGetError) => {
myLog(`${imgHref},imageGetError:${imageGetError}`);
},
ontimeout: (exception) => {
myLog(`${imgHref},ontimeout exception:${exception}`);
}
}
)
}
//</editor-fold>
//<editor-fold desc="6.上传图片">
function uploadImage(imgHref, bodyFormData) {
axios({
method: "POST",
url: "https://upload.cnblogs.com/imageuploader/CorsUpload",
data: bodyFormData,
headers: {
"Content-Type": "multipart/form-data",
},
withCredentials: true,
}).then(response => {
if (response.status === 200) {
if (response.data.success) {
return ReplaceEditor(imgHref, response.data.message);
}
}
myLog(`uploadImage response:${response}`);
}).catch(error => {
myLog(`uploadImage error:${error}`);
})
}
//</editor-fold>
//<editor-fold desc="7.替换url">
function ReplaceEditor(oldUrl, newUrl) {
myLog("oldUrl:" + oldUrl);
myLog("newUrl:" + newUrl);
innerOldHTML = innerOldHTML.replace(new RegExp(escapeRegExp(oldUrl), 'g'), newUrl);
updateImgCount++;
if (updateImgCount >= downImgCount) {
let tempo =
`updateImgCount/downImgCount/totalImgCount = ${updateImgCount}/${downImgCount}/${totalImgCount}`;
myLog(`newBody:${innerOldHTML}`)
// noinspection JSJQueryEfficiency
let editorHTML = $("#Editor_Edit_EditorBody_ifr").contents().find("body").html();
if (null != editorHTML) {
$("#Editor_Edit_EditorBody_ifr").contents().find("body").html(innerOldHTML);
}
// document.getElementById("Editor_Edit_EditorBody_ifr").contentDocument.body.innerHTML = innerOldHTML;
document.querySelector("iframe#Editor_Edit_EditorBody_ifr").contentWindow.document.body.innerHTML =
innerOldHTML;
document.querySelector("#Editor_Edit_EditorBody_ifr").contentDocument.body.innerHTML
= innerOldHTML;
document.getElementById("Editor_Edit_EditorBody_ifr").contentDocument.body.innerHTML
= innerOldHTML;
if (navigator.clipboard) {
navigator.clipboard.writeText(innerOldHTML);
} else {
const eventCopyer = event => {
event.preventDefault();
event.clipboardData.setData("text/plain", innerOldHTML);
}
document.addEventListener("copy", eventCopyer);
document.execCommand("copy");
document.removeEventListener("copy", eventCopyer);
}
// window.parent.tinyMCE.get('Editor_Edit_EditorBody').onLoad.dispatch();
//document.querySelector("iframe#Editor_Edit_EditorBody_ifr").contentWindow.document.body.innerHTML =
// `<div id="lg" class="s-p-top"><img id="s_lg_img" class="s_lg_img_gold_show"
// src="https://www.baidu.com/img/bd_logo1.png" alt="" width="270" height="129"
// usemap="#mp" data-mce-src="https://www.baidu.com/img/bd_logo1.png">159654456789</div>`
showTips(`同步完成,单击重新同步,如同步失败请保存再编辑和同步。${tempo}`, 300, 2);
}
return true;
}
//</editor-fold>
//<editor-fold desc="9.辅助">
function myLog(param1, param2) {
param1 = param1 ? param1 : "";
param2 = param2 ? param2 : "";
console.log(`#${logCount++}AUp:`, param1, param2);
}
function showTips(content, height, time_s) {
let windowWidth = window.innerWidth;
// noinspection JSUnusedLocalSymbols
let windowHeight = window.innerHeight;
let htmlDivElement = document.createElement("div");
htmlDivElement.id = "showTips";
htmlDivElement.innerText = content;
htmlDivElement.style.top = height + "px";
htmlDivElement.style.left = height + (windowWidth / 2) - 350 / 2 + "px";
document.body.appendChild(htmlDivElement);
htmlDivElement.style.opacity = "0";
htmlDivElement.style.opacity = "1";
setTimeout(() => {
htmlDivElement.style.opacity = "0";
}, (time_s * 1000));
}
String.prototype.trimChars = function (c) {
let re = new RegExp("^[" + c + "]+|[" + c + "]+$", "g");
return this.replace(re, "");
}
function keyValuePairStringToLowercaseKeyJson(responseHeaders) {
let commaAdded = responseHeaders.replace(/(?:\r\n|\r|\n)/g, ',').trim().replace(/,+$/g, '');
let items = commaAdded.split(',');
let jsonString = items.map(item => {
return item.replace(/([^:]+)(:)(.+$)/, (match, p1, p2, p3) => {
return `"${p1.trim().toLowerCase()}": "${p3.trim().trimChars('"')}"`;
})
}).join(', ');
try {
return JSON.parse(`{${jsonString}}`);
} catch (err) {
myLog(`keyValuePairStringToJson err:${err}`);
}
}
function escapeRegExp(string) {
return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
//</editor-fold>
})();