-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCopyPostHtml.js
220 lines (206 loc) · 7.25 KB
/
CopyPostHtml.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
// ==UserScript==
// @name CopyPostHtml
// @namespace https://github.com/a549956952/CopyPostHtml
// @description 复制Discuz!贴子HTML代码,方便制作EPUB。
// @author 废喵
// @include *forum.php?*
// @grant GM_addStyle
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// @run-at document-body
// @version 2021.02.04
// @name:zh-CN 复制帖子HTML
// ==/UserScript==
//读取设置
var start= GM_getValue('start', 1); //起始楼层
var end= GM_getValue('end', 1); //结束楼层
var isReplaceing = GM_getValue('isReplaceing', false); //是否替换HTML
var isAuto = GM_getValue('isAuto', false); //是否自动复制
var isRemoveTag = GM_getValue('isRemoveTag', false); //是否自动去除标签
var removeTag = GM_getValue('removeTag',['font','strong']); //自动去除标签
//悬浮窗实现
function Toast(msg,duration,mode){
duration = typeof duration !== 'undefined'?duration : 3000;
mode = typeof mode !== 'undefined'?mode :"none";
var m = document.createElement('div');
m.innerHTML = msg;
switch(mode) {
case "r":
m.style.cssText="max-width:60%;min-width: 150px;padding:0 14px;height: 40px;color: rgb(255, 255, 255);line-height: 40px;text-align: center;border-radius: 4px;position: fixed;top: 50%;left: 50%;transform: translate(-50%, -50%);z-index: 999999;background: rgba(255, 0, 0,.7);font-size: 16px;";
break;
case "g":
m.style.cssText="max-width:60%;min-width: 150px;padding:0 14px;height: 40px;color: rgb(255, 255, 255);line-height: 40px;text-align: center;border-radius: 4px;position: fixed;top: 50%;left: 50%;transform: translate(-50%, -50%);z-index: 999999;background: rgba(0, 255, 0,.7);font-size: 16px;";
break;
default:
m.style.cssText="max-width:60%;min-width: 150px;padding:0 14px;height: 40px;color: rgb(255, 255, 255);line-height: 40px;text-align: center;border-radius: 4px;position: fixed;top: 50%;left: 50%;transform: translate(-50%, -50%);z-index: 999999;background: rgba(0, 0, 0,.7);font-size: 16px;";
}
document.body.appendChild(m);
setTimeout(function() {
var d = 0.5;
m.style.webkitTransition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in';
m.style.opacity = '0';
setTimeout(function() { document.body.removeChild(m) }, duration);
}, duration);
}
//查找str里第num个cha的位置
function find(str,cha,num){
num--;
var x=str.indexOf(cha);
for(var i=0;i<num;i++){
x=str.indexOf(cha,x+1);
}
return x;
}
//复制到剪切板实现
function copyToClipboard (text) {
/* if(text.indexOf('-') !== -1) {
let arr = text.split('-');
text = arr[0] + arr[1];
} */
var textArea = document.createElement("textarea");
textArea.style.position = 'fixed';
textArea.style.top = '0';
textArea.style.left = '0';
textArea.style.width = '2em';
textArea.style.height = '2em';
textArea.style.padding = '0';
textArea.style.border = 'none';
textArea.style.outline = 'none';
textArea.style.boxShadow = 'none';
textArea.style.background = 'transparent';
textArea.value = text;
document.body.appendChild(textArea);
textArea.select();
var flag=false;
try {
flag = document.execCommand('copy');
} catch (err) {
flag = false;
}
document.body.removeChild(textArea);
return flag;
}
//重新加载会变动部分
function relaod()
{
GM_unregisterMenuCommand(replaceingMenuId);
GM_unregisterMenuCommand(autoMenuId);
GM_unregisterMenuCommand(isRemoveTagMenuId);
addReplaceingMenu();
addAutoMenu();
addIsRemoveTagMenu();
}
//添加按钮
function addMenu() {
GM_registerMenuCommand('复制HTML代码', onMenu);
}
function onMenu() {
var url = window.location.href;
var page = 1;
if(url.lastIndexOf('&page=')!==-1)
{
if(url.indexOf('&',url.lastIndexOf('&page=')+5)==-1) page = url.slice(url.lastIndexOf('&page=')+6); else page = url.slice(url.lastIndexOf('&page=')+6,url.indexOf('&',url.lastIndexOf('&page=')+5));
}
var text= new String();
var rep1 = /<br[^>]*>/ig;
var rep2 = /<p><\/p>|<p>[\n]<\/p>/ig;
var posts = document.querySelectorAll(".t_f");
var title=document.getElementById("thread_subject").textContent;
if(start>posts.length)
{
start = posts.length;
}
if(end>posts.length)
{
end = posts.length;
}
for(var i = start-1;i<end;i++)
{
var post = posts[i].innerHTML;
if(isRemoveTag)
{
for(var j = 0;j<removeTag.length;j++)
{
var r =new RegExp("<\\/?" + removeTag[j] + "[^>]*>","gi");
post = post.replace(r,'');
//post = post.replace(/<\/?font[^>]*>/gi, '');
}
}
if(isReplaceing)
{
post= post.slice(0,1)+"<p>"+post.slice(1)
post=post+'</p>\n';
post = post.replace( rep1, '</p><p>' );
post = post.replace( rep2, '\n<br />' );
}
text=text+"\n<\!-- "+title+" 第"+page+"页 第"+(i+1)+"楼 -->\n"+post;
}
if(copyToClipboard(text)) Toast('已经复制当前页面'+start+'至'+end+'楼',3000,"g"); else Toast('复制失败',3000,"r");
}
addMenu();
var settingMenuId;
function addSettingMenu() {
settingMenuId = GM_registerMenuCommand('设置复制楼层(仅当前页面楼层)', onSettingMenu);
}
function onSettingMenu() {
start=prompt("起始楼层(1~10)",1);
end=prompt("结束楼层(1~10)",1);
GM_setValue('start', start);
GM_setValue('end', end);
}
addSettingMenu();
var settingTagMenuId;
function addSettingTagMenu() {
settingTagMenuId = GM_registerMenuCommand('设置移除标签', onSettingTagMenu);
}
function onSettingTagMenu() {
var s =prompt("输入标签,英文逗号分隔(如font,strong)","font,strong");
removeTag = s.split(',');
GM_setValue('removeTag',removeTag);
}
addSettingTagMenu();
var replaceingMenuId;
function addReplaceingMenu() {
replaceingMenuId = GM_registerMenuCommand('是否替换换行符为段落标签(' + (isReplaceing ? '是' : '否') + ')', onReplaceingMenu);
}
function onReplaceingMenu() {
isReplaceing = !isReplaceing;
GM_setValue('isReplaceing', isReplaceing);
relaod();
}
addReplaceingMenu();
var autoMenuId;
function addAutoMenu() {
autoMenuId = GM_registerMenuCommand('是否自动执行(' + (isAuto ? '是' : '否') + ')', onAutoMenu);
}
function onAutoMenu() {
isAuto = !isAuto;
GM_setValue('isAuto', isAuto);
relaod();
}
addAutoMenu();
var isRemoveTagMenuId;
function addIsRemoveTagMenu() {
isRemoveTagMenuId = GM_registerMenuCommand('是否去除标签(' + (isRemoveTag ? '是' : '否') + ')', onIsRemoveTagMenu);
}
function onIsRemoveTagMenu() {
isRemoveTag = !isRemoveTag;
GM_setValue('isRemoveTag', isRemoveTag);
relaod();
}
addIsRemoveTagMenu();
const body = document.querySelector("body");
body.addEventListener("dblclick", (e) => {
if (e.ctrlKey) {
onMenu();
}
})
//自动复制
window.onload = function(){
if(isAuto)
{
onMenu();
}
}