-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcontent-script.js
123 lines (105 loc) · 3.36 KB
/
content-script.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
(function () {
/*
* DOM selector
* @param selector {String} css selector for element
* @param context {Element} root element
* usage: $$('body')
* result: You will get <body> element
*/
function $$(selector, context) {
context = context || document;
var elements = context.querySelectorAll(selector);
return elements.length == 1
? Array.prototype.slice.call(elements)[0]
: Array.prototype.slice.call(elements);
}
/**
*
* copy text to clipboard
* https://stackoverflow.com/questions/3436102/copy-to-clipboard-in-chrome-extension/12693636#12693636
*
* @param str
* @param mimeType 'Text'
*/
function copy(str) {
navigator.clipboard.writeText(str);
}
function addCSS(styles) {
let styleSheet = document.createElement("style")
styleSheet.innerText = styles
document.head.appendChild(styleSheet)
}
// 内容区开启复制
content_views = document.querySelector("#content_views")
content_views.replaceWith(content_views.cloneNode(true));
// 功能一: 修改复制按钮,支持一键复制
const buttons = Array.isArray($$(".hljs-button"))
? $$(".hljs-button")
: [$$(".hljs-button")];
buttons.forEach((btn) => {
// 更改标题
btn.dataset.title = "复制";
// 移除点击事件
btn.setAttribute("onclick", "");
// 克隆按钮
elClone = btn.cloneNode(true);
// 替回按钮
btn.parentNode.replaceChild(elClone, btn);
// 重新添加点击事件
elClone.addEventListener("click", (e) => {
// 实现复制
const parentPreBlock = e.target.closest("pre");
const codeBlock = $$("code", parentPreBlock);
copy(codeBlock.innerText);
e.target.dataset.title = "复制成功";
setTimeout(() => {
e.target.dataset.title = "复制";
}, 1000);
e.stopPropagation();
e.preventDefault();
});
});
// 功能二:解除 关注博主即可阅读全文的提示,
let readMore = document.querySelector('.btn-readmore')
let style = `
.hide-article-box{
z-index: -1 !important;
}
`
if (readMore) {
addCSS(style)
}
// 判定是否需要展开代码块
listenerChange()
// 自动展开代码块
function openPre() {
const pres = Array.from(document.querySelectorAll('main div.blog-content-box pre.set-code-hide'))
const presBox = Array.from(document.querySelectorAll('.hide-preCode-box'))
pres.forEach((pre) => {
pre.style.height = 'unset'
pre.style.maxHeight = 'unset'
})
presBox.forEach((box) => {
box.style.display = 'none'
})
}
function listenerChange() {
// chrome.storage.onChanged.addListener((changes, namespace) => {
// for (let [key, { oldValue, newValue }] of Object.entries(changes)) {
// console.log(
// `Storage key "${key}" in namespace "${namespace}" changed.`,
// `Old value was "${oldValue}", new value is "${newValue}".`
// );
// if (key === 'autoOpenCodeCheck') {
// }
// }
// });
chrome.storage.sync.get(['autoOpenCodeCheck']).then((result) => {
const check =
result.autoOpenCodeCheck === undefined ? true : result.autoOpenCodeCheck;
if (check) {
openPre()
}
});
}
})();