Skip to content

Instantly share code, notes, and snippets.

@kotobukid
Last active September 17, 2020 09:44
Show Gist options
  • Save kotobukid/3ec9dab8fd01e44d166e817b07642992 to your computer and use it in GitHub Desktop.
Save kotobukid/3ec9dab8fd01e44d166e817b07642992 to your computer and use it in GitHub Desktop.
Human Scraping (greasemonkey)
// ==UserScript==
// @name Sample Script 2
// @namespace https://daqua.jp
// @description Human scraper
// @include http://r-r.arclight.co.jp/backnumber
// @require https://code.jquery.com/jquery-3.5.1.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.11.0/underscore-min.js
// ==/UserScript==
const search_box = $('<input type="text" id="simple_search"></input>');
const result_box = $('<ul id="result_box"></ul>');
search_box.css({
'z-index': 1000000,
position: 'fixed',
top: '10px',
left: '10px',
border: '1px solid red',
'background-color': 'lightblue'
});
result_box.css({
'z-index': 1000000,
position: 'fixed',
top: '36px',
left: '10px',
border: '1px solid black',
'background-color': 'white',
'list-style': 'none',
'min-width': '100px',
'max-width': '800px',
padding: '5px',
'max-height': '800px',
'overflow-y': 'scroll',
display: 'block'
});
$('#header').append(search_box).append(result_box);
$('#simple_search').on('change', (e) => {
const search_word = e.currentTarget.value;
result_box.empty();
if (!search_word) {
} else {
_.each(search_source, (source) => {
_.each(source.items, (item) => {
if ((item.title.indexOf(search_word) > -1) || (item.text.indexOf(search_word) > -1)) {
result_box.append($(`<li style="margin-bottom: 2px;"><a href="http://r-r.arclight.co.jp/backnumber/${source.key}" target="_blank">${source.key}</a>
<span>title: ${item.title}</span>
<br>
<span>text: ${item.text}</span>
</li>`));
}
})
});
}
}).on('keydown', (e) => {
if (e.key === 13) {
debugger;
e.preventDefault();
}
});
const search_source = [];
_.each(Object.keys(localStorage), (key) => {
search_source.push({
key,
items: JSON.parse(localStorage.getItem(key))
});
});
// ==UserScript==
// @name Sample Script
// @namespace https://daqua.jp
// @description Human scraper
// @include http://r-r.arclight.co.jp/backnumber/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js
// ==/UserScript==
const urlInfo = document.location.href.split('/');
const entry_id = urlInfo[urlInfo.length - 1];
if (entry_id === 'backnumber') {
return;
}
const $elements = $('.backnumberText').children();
const dict = [];
let item = {title: '', text: []};
let i = 0;
while (i < $elements.length) {
if ($elements[i].tagName === 'H1' || $elements[i].tagName === 'H3') {
if (!!item.title) {
dict.push({title: item.title, text: item.text.join('\n')});
item = {title: '', text: []};
}
item.title = $elements[i].innerText;
} else {
item.text.push($elements[i].innerText);
}
i = i + 1;
}
if (!!item.title) {
dict.push({title: item.title, text: item.text.join('\n')});
}
// console.log(JSON.stringify(dict));
localStorage.setItem(entry_id, JSON.stringify(dict));
// alert('complete');
$('#wrapper').css('background-color', 'pink')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment