-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstories.js
157 lines (122 loc) · 4.57 KB
/
stories.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Run in browser devtools to scrape a rich story link
function htmlToElement(html) {
var template = document.createElement('template');
template.innerHTML = html;
return template.content.firstChild;
}
function lobstersHtml() {
var score = document.querySelector('.score').innerHTML;
var title = '';
var where = '';
var title_el = document.querySelector('a.u-url');
var domain_el = document.querySelector('a.domain');
if (domain_el) {
title = title_el.outerHTML;
var domain = domain_el.innerHTML;
where = `<code>${domain}</code> via lobste.rs`;
} else {
title = `<span class="title-without-link">${title_el.innerHTML}</span>`;
where = 'ask';
}
var comments_el = document.querySelector('span.comments_label a');
var comments_href = comments_el.getAttribute('href')
if (comments_href.indexOf('//lobste.rs') == -1) {
comments_el.setAttribute('href', 'https://lobste.rs' + comments_href);
}
var submission_time = document.querySelector('div.byline span[title]').title;
var date = submission_time.split(' ')[0];
return `${title} (${where}) <br/>
${score} points, ${comments_el.outerHTML} on ${date}`;
}
function hackerNewsHtml() {
var subtext = document.querySelector('td.subtext');
var score = subtext.querySelector('span.score').innerHTML;
var title_el = document.querySelector('span.titleline a'); // link inside span
var title_href = title_el.getAttribute('href');
if (title_href.indexOf('//news.ycombinator.com') === -1) {
title_el.setAttribute('href', 'https://news.ycombinator.com/' + title_href);
}
var title = title_el.outerHTML;
var where = '';
var domain_el = document.querySelector('span.sitestr');
if (domain_el) {
domain = domain_el.innerHTML;
where = `<code>${domain}</code> via Hacker News`;
} else {
where = 'self';
}
var links = subtext.querySelectorAll('a');
var comments_el = links[links.length-1];
// Add domain to the URL
var comments_href = comments_el.getAttribute('href');
if (comments_href.indexOf('//news.ycombinator.com') === -1) {
comments_el.setAttribute('href', 'https://news.ycombinator.com/' + comments_href);
}
var date = links[1].innerHTML;
return `${title} (${where}) <br/>
${score}, ${comments_el.outerHTML} - ${date}`;
}
function oldRedditHtml() {
var score = document.querySelector('div.score span').innerHTML;
var domain = document.querySelector('span.domain a').innerHTML;
var site = 'Reddit';
var title = '';
var where = '';
if (domain.indexOf('self.') == -1) {
// External Link. Strip the "out.reddit.com" domain.
var reddit_el = document.querySelector('a.title');
var our_el = htmlToElement(`<a href="">${reddit_el.innerHTML}</a>`);
our_el.setAttribute('href', reddit_el.getAttribute('data-href-url'));
title = our_el.outerHTML;
where = `<code>${domain}</code> via ${site}`;
} else {
// User Discussion. Link is in comments.
var title_str = document.querySelector('a.title').innerHTML;
var title = `<span class="title-without-link">${title_str}</span>`;
// self.ProgrammingLanguages -> /r/ProgrammingLanguages
where = domain.replace('self.', '/r/');
}
var comments = document.querySelector('a.comments').outerHTML;
var date = document.querySelector('time').innerHTML;
return `${title} (${where}) <br/>
${score} points, ${comments} - ${date}`;
}
function storyHtml(url) {
var links = '';
if (url.indexOf('lobste.rs') !== -1) {
links = lobstersHtml();
} else if (url.indexOf('news.ycombinator.com') !== -1) {
links = hackerNewsHtml();
} else if (url.indexOf('old.reddit.com') !== -1) {
links = oldRedditHtml();
} else {
console.log('Not a story');
return '';
}
return links;
}
var links = storyHtml(window.location.href);
if (links) {
console.log(links);
document.body.insertAdjacentHTML('afterbegin', links);
// note: only https is valid
var post_url = "https://hashdiv.oils.pub/paste";
//var post_url = "https://dr.shxa.org/hashdiv/paste";
// var post_url = "http://localhost:5000/paste";
var form = `
<p>
<form action="${post_url}" method=POST>
<input type="hidden" name="format" value="html" />
<input id="storyhtml" type="hidden" name="data" value="" />
<input type="submit" />
</form>
</p>`;
console.log(links);
// This is meant to be embedded in CommonMark.
var h = `<div class="link-box">
${links.replace(/\n/g, '')}
</div>`;
// Add escaped data to be POSTed
document.body.insertAdjacentHTML('afterbegin', form);
document.querySelectorAll('input#storyhtml')[0].setAttribute('value', h);
}