-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (44 loc) · 1.69 KB
/
index.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
$(function () {
let defaultURL = 'https://fiery13.wordpress.com/2013/11/07/ky-cambri-tro-lai-chuong-49/'
function saveTextAsFile(text, fileName) {
var textToSave = text
var textToSaveAsBlob = new Blob([textToSave], { type: "text/plain" });
var textToSaveAsURL = window.URL.createObjectURL(textToSaveAsBlob);
var fileNameToSaveAs = fileName;
var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "Download File";
downloadLink.href = textToSaveAsURL;
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();
}
function loadContent() {
let newURL = $('#txtURL').val();
let loadURL = _.isEmpty(newURL) ? defaultURL : newURL
let contentElement = 'div.entry-content'
$.ajax({
url: loadURL,
type: 'GET',
success: function (resp) {
// define name of text file
let fileName = [moment().format("DDMMYYYY.HHmmss"), 'txt'].join('.')
// return tag that contains main content
let htmlVal = $(resp.responseText).find(contentElement).html()
// return an array of text in html tags
let arr = _.map($(htmlVal), function (ele) {
return ele.innerText
})
// adjust the content, display source url
arr.splice(0, 0, '--------------------------------------------------------------------------------------------------')
arr.splice(0, 0, 'source::: ' + loadURL)
// export to txt file
saveTextAsFile(arr.join("\n"), fileName)
$('#container').html(arr.join("\n"))
}
})
}
$('#btnGet').click(function () {
loadContent()
})
})