-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmarkdown_insert.js
84 lines (75 loc) · 3.01 KB
/
markdown_insert.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
/**
* author: https://github.com/onvno
*/
/**
* 考虑几中情况
* 1. 未选择任何内容 - 此时应该取title内容
* 2. 选择文字 - 此时拼接内容 + url
* 3. 选择链接 - 此时调整url链接的格式
* 4. 选择图片 - 调整url链接为图片格式
*/
var handleClip = () => {
var userSelection = window.getSelection();
// console.log(userSelection);
// console.log(userSelection.getRangeAt(0).cloneContents())
// debugger
var selectCont = userSelection.toString().trim();
var finalCont;
/**
* 3 - handle text
* 1 - handle img link
*/
if(userSelection.anchorNode.nodeType === 3){
if(selectCont.length == 0 ){
finalCont = '[' + document.title.trim() + '](' + window.location.href +')';
}
else if (selectCont.indexOf('http')==0 || selectCont.indexOf('ftp')==0) {
var httpWholeHref =
userSelection.anchorNode.parentNode && userSelection.anchorNode.parentNode.href ?
userSelection.anchorNode.parentNode.href :
userSelection.anchorNode.nextSibling && userSelection.anchorNode.nextSibling.href ?
userSelection.anchorNode.nextSibling.href :
selectCont
finalCont = '[Replace Text : MarkDown ClipBoard](' + httpWholeHref + ')';
}
else if (userSelection.anchorNode.parentNode && userSelection.anchorNode.parentNode.href) {
finalCont = '[' + userSelection + '](' + userSelection.anchorNode.parentNode.href + ')';
}
else if (userSelection.anchorNode.nextSibling && userSelection.anchorNode.nextSibling.href) {
finalCont = '[' + userSelection + '](' + userSelection.anchorNode.nextSibling.href + ')';
}
else {
finalCont = '[' + userSelection + '](' + window.location.href + ')'
}
}
else if (userSelection.anchorNode.nodeType === 1) {
var range = userSelection.getRangeAt(0)
var fragment = range.cloneContents()
var imgs = fragment.querySelectorAll('img')
var imgAry = Array.from(imgs);
finalCont = '';
imgAry.map((item) => {
finalCont += '![' + item.getAttribute('alt') + ']' + '(' + item.getAttribute('src') +')\n';
})
}
// console.log("finalCont:", finalCont);
/**
* Copy Content
*/
var hiddenDOM
if(!document.querySelector('#markdown-clipboard-chrome-extension')) {
const el = document.createElement('textarea');
el.setAttribute("id", 'markdown-clipboard-chrome-extension');
el.setAttribute("style", "visiblity:hidden; height: 0px")
document.body.appendChild(el);
}
hiddenDOM = document.querySelector('#markdown-clipboard-chrome-extension')
hiddenDOM.value = finalCont;
hiddenDOM.select();
if (document.execCommand('copy')) {
console.log('Clipboard Copy Success !')
} else {
alert('MarkDown ClipBoard : Copy Failure, Please Update Browser Version or Contact Author!');
}
}
handleClip();