-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
127 lines (112 loc) · 3.71 KB
/
content.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
var sticker
// turns on for the first time when loading a page
chrome.runtime.sendMessage({request: "state"}, function(response) {
if (response.state === "on") {
turnOn()
}
});
// recieves the message after the action button is pressed
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.message === "turn_on") {
turnOn()
} else if (request.message === "turn_off") {
turnOff()
}
}
)
function turnOn() {
displaySticker()
document.addEventListener('click', handleClick)
}
function turnOff() {
removeSticker()
document.removeEventListener('click', handleClick)
}
function handleClick(event) {
if (event.target.className === 'audio') {
var link = document.createElement('a');
var linkBeginning = /https?:\/\/(www)?.linguee.[^\/]*/g.exec(window.location)[0]
link.href = linkBeginning + '/mp3/' + event.target.id
var word = event.target.parentElement.querySelectorAll('.dictLink')[0].innerText
link.download = sanitizeFilename(word)
//Dispatching click event.
if (document.createEvent) {
var e = document.createEvent('MouseEvents');
e.initEvent('click' ,true ,true);
link.dispatchEvent(e);
return true;
}
}
}
function displaySticker() {
sticker = document.createElement('div')
sticker.innerHTML = 'click any 🔊 to download'
sticker.style.position = 'fixed'
sticker.style.top = '5px'
sticker.style.right = '5px'
sticker.style.background = '#222'
sticker.style.color = '#fff'
sticker.style.fontSize = '10px'
sticker.style.borderRadius = '5px'
sticker.style.padding = '3px'
sticker.style.zIndex = '9999'
document.body.appendChild(sticker)
}
function removeSticker() {
document.body.removeChild(sticker)
}
// from: https://github.com/parshap/node-sanitize-filename
/*jshint node:true*/
'use strict';
/**
* Replaces characters in strings that are illegal/unsafe for filenames.
* Unsafe characters are either removed or replaced by a substitute set
* in the optional `options` object.
*
* Illegal Characters on Various Operating Systems
* / ? < > \ : * | "
* https://kb.acronis.com/content/39790
*
* Unicode Control codes
* C0 0x00-0x1f & C1 (0x80-0x9f)
* http://en.wikipedia.org/wiki/C0_and_C1_control_codes
*
* Reserved filenames on Unix-based systems (".", "..")
* Reserved filenames in Windows ("CON", "PRN", "AUX", "NUL", "COM1",
* "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
* "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", and
* "LPT9") case-insesitively and with or without filename extensions.
*
* Capped at 255 characters in length.
* http://unix.stackexchange.com/questions/32795/what-is-the-maximum-allowed-filename-and-folder-size-with-ecryptfs
*
* @param {String} input Original filename
* @param {Object} options {replacement: String}
* @return {String} Sanitized filename
*/
function truncate(s, len) {
return s.substr(0, len)
}
var illegalRe = /[\/\?<>\\:\*\|":]/g;
var controlRe = /[\x00-\x1f\x80-\x9f]/g;
var reservedRe = /^\.+$/;
var windowsReservedRe = /^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$/i;
var windowsTrailingRe = /[\. ]+$/;
function sanitize(input, replacement) {
var sanitized = input
.replace(illegalRe, replacement)
.replace(controlRe, replacement)
.replace(reservedRe, replacement)
.replace(windowsReservedRe, replacement)
.replace(windowsTrailingRe, replacement);
return truncate(sanitized, 255);
}
function sanitizeFilename(input, options) {
var replacement = (options && options.replacement) || '';
var output = sanitize(input, replacement);
if (replacement === '') {
return output;
}
return sanitize(output, '');
};