-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink-to-markdown.user.js
More file actions
78 lines (71 loc) · 2.46 KB
/
link-to-markdown.user.js
File metadata and controls
78 lines (71 loc) · 2.46 KB
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
// ==UserScript==
// @name Link to Markdown
// @namespace https://github.com/CyrusYip/userscripts
// @match *://*/*
// @require https://cdn.jsdelivr.net/npm/@violentmonkey/shortcut@1
// @require https://cdn.jsdelivr.net/gh/sizzlemctwizzle/GM_config@06f2015c04db3aaab9717298394ca4f025802873/gm_config.js
// @grant GM.getValue
// @grant GM.notification
// @grant GM.registerMenuCommand
// @grant GM.setClipboard
// @grant GM.setValue
// @grant GM_getValue
// @grant GM_setValue
// @version 10.0.0
// @author Cyrus Yip
// @description Get the link and title of current page, convert them to Markdown link, and write it to the clipboard. To use this script, click "Copy Markdown link" in the userscript manager's menu or press Shift + Alt + L . To change settings, click "Link to Markdown Settings" in the menu. Key Definition: https://github.com/violentmonkey/vm-shortcut#key-definition .
// ==/UserScript==
'use strict';
let title, href, hash, markdownLink, enableShortcut, enableNotification, shortcutDefinition
const loadConfig = function () {
enableShortcut = this.get('enableShortcut')
enableNotification = this.get('enableNotification')
shortcutDefinition = this.get('shortcutDefinition')
setShortcut()
}
let config = new GM_config({
'id': 'Config',
'title': 'Link to Markdown Settings',
'fields': {
'enableShortcut': {
'label': 'Enable shortcut',
'type': 'checkbox',
'default': true,
},
'enableNotification': {
'label': 'Enable notification',
'type': 'checkbox',
'default': true,
},
'shortcutDefinition': {
'label': 'Shortcut definition',
'type': 'text',
'default': 'shift-alt-l',
},
},
'events': {
'init': loadConfig,
'save': loadConfig,
}
})
const copyLink = () => {
({ title } = document);
({ href, hash } = window.location)
markdownLink = `[${title}${hash}](${href})`
GM.setClipboard(markdownLink)
}
const showResult = () => {
if (enableNotification === false) { return }
markdownLink
? GM.notification(`✔ Link was copied.\n${markdownLink}`)
: GM.notification(`✘ Failed to get link.`)
}
const setShortcut = () => {
if (enableShortcut === false) { return }
VM.shortcut.register(shortcutDefinition, () => {
copyLink()
showResult()
})
}
GM.registerMenuCommand('Copy Markdown link', () => { copyLink(); showResult() })
GM.registerMenuCommand('Link to Markdown Settings', () => { config.open() })