-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnaver.mjs
More file actions
88 lines (65 loc) · 2.04 KB
/
Copy pathnaver.mjs
File metadata and controls
88 lines (65 loc) · 2.04 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
79
80
81
82
83
84
85
86
87
88
import { parseEmails, sanitizeHTML } from './util.mjs'
function run () {
const content = getTextContent()
if (!content) {
return
}
const emails = parseEmails(content)
if (!emails.length) {
return
}
fetchArticles(emails[emails.length - 1])
}
function getTextContent () {
const el = document.querySelector('#articleBodyContents')
if (el) {
return el.textContent
}
}
function fetchArticles (data) {
const url = `https://search.naver.com/search.naver?where=news&query=${encodeURIComponent(data.email)}&sort=1`
window.fetch(url).then(response => {
return response.text()
}).then(body => {
data.url = url
generateList(body, data)
})
}
function generateList (text, data) {
const list = parseList(text)
if (!list || !list.length) {
return
}
const fragment = document.createDocumentFragment()
list.forEach(item => {
const li = document.createElement('li')
li.innerHTML = `<a href="${item.url}">${item.title}</a>`
fragment.appendChild(li)
})
const title = data.name ? `${data.name} ${data.email}` : data.email
const div = document.createElement('div')
div.className = 'section'
div.innerHTML = `<h4><a href="${data.url}">${title}</a></h4><div class="classfy" style="border-top:1px solid #e8e8e8;display:block;"><ul class="list_txt"></ul><a href="${data.url}" class="more_link">더보기</a></div>`
div.querySelector('ul').appendChild(fragment)
document.querySelector('table.container td.aside div.aside').insertBefore(div, document.querySelector('.section:first-child'))
}
function parseList (text) {
const LIST_RE = /<ul class="type01">(.*?)<\/ul>/gi
const matches = LIST_RE.exec(text)
if (!matches) {
return
}
const $ul = document.createElement('ul')
$ul.innerHTML = matches[0]
return Array.from($ul.querySelectorAll('li')).map(li => {
const title = li.querySelector('._sp_each_title').textContent
const url = li.querySelector('a._sp_each_url').getAttribute('href')
return {
title: sanitizeHTML(title),
url
}
})
}
export {
run
}