-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdaum.mjs
More file actions
108 lines (84 loc) · 2.43 KB
/
daum.mjs
File metadata and controls
108 lines (84 loc) · 2.43 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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('#harmonyContainer')
if (el) {
return el.textContent
}
}
function fetchArticles (data) {
const url = `https://search.daum.net/search?w=news&q=${encodeURIComponent(data.email)}&sort=recency`
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 listItems = buildItems(list)
const container = buildContainer(data, listItems)
const aside = document.querySelector('#mAside')
aside.insertBefore(container, aside.querySelector('.hc_news_pc_mAside_popular_news'))
}
function buildItems (list) {
const fragment = document.createDocumentFragment()
list.forEach((item, index) => {
const li = document.createElement('li')
li.innerHTML = `<em class="num_newsview num${index + 1}"></em><strong class="tit_g"><a href="${item.url}">${item.title}</a></strong>`
fragment.appendChild(li)
})
return fragment
}
function buildContainer (data, fragment) {
const title = data.name ? `${data.name} ${data.email}` : data.email
const el = document.createElement('div')
el.className = 'aside_g aside_popular'
el.innerHTML = `
<h3>${title}</h3>
<ul class="tab_aside">
<li class="on">
<div class="cont_aside">
<ul class="list_ranking"></ul>
<div class="util_aside">
<a href="${data.url}" class="link_all">
<span class="txt_newsview"></span>
<span class="ico_newsview"></span>
</a>
</div>
</div>
</li>
</ul>
`
el.querySelector('.list_ranking').appendChild(fragment)
return el
}
function parseList (text) {
const parser = new window.DOMParser()
const doc = parser.parseFromString(text, 'text/html')
return Array.from(doc.querySelectorAll('#clusterResultUL li')).map(li => {
const title = li.querySelector('.f_link_b').textContent
const url = li.querySelector('a.f_nb').getAttribute('href')
return {
title: sanitizeHTML(title),
url
}
})
}
export {
run
}