Created
August 20, 2022 05:45
-
-
Save refracta/2bfb6b0af911fa12ef157818e1fb62da to your computer and use it in GitHub Desktop.
acmicpc auto downloader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name DownloadAllSolvedProblem | |
// @version 0.1 | |
// @author refracta | |
// @match https://www.acmicpc.net/* | |
// ==/UserScript== | |
function waitFor(vf, t) { | |
return new Promise(r => { | |
let i = setInterval(_ => { | |
try { | |
let v = vf(); | |
if (v) { | |
clearInterval(i); | |
r(v); | |
} | |
} catch (e) {} | |
}, t); | |
}); | |
} | |
function add_alert(text) { | |
$($('.row').get(1)).prepend($(`<div class="col-md-12"><div class="alert alert-success alert-dismissable fade in"> | |
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> | |
<div id="custom-alert" class="alert-body"></div> | |
</div> | |
</div>`)); | |
$('#custom-alert').text(text); | |
} | |
function save() { | |
localStorage.custom_data = JSON.stringify(custom_data); | |
} | |
let language_map = { | |
"Python 3": 'py', | |
'PyPy3': 'py', | |
"Java 11": 'java', | |
"C99": 'c', | |
"C++17": 'cpp', | |
"node.js": 'js', | |
"Text": 'txt' | |
}; | |
(async function () { | |
self.custom_data = JSON.parse(localStorage.custom_data ? localStorage.custom_data : '{}'); | |
// Init | |
let path = location.pathname; | |
if (path.startsWith('/status')) { | |
$('.form-inline').append($('<button id="download-button" onclick="return false;" class="btn btn-primary btn-sm margin-left-3 form-control">전부 다운로드</button>')); | |
$('#download-button').on('click', function () { | |
custom_data = { | |
status: 'crawl', | |
list: [], | |
start_page: location.href | |
}; | |
save(); | |
location.reload(); | |
}); | |
if (custom_data.status == 'crawl') { | |
$('.form-inline').append($('<button id="stop-button" onclick="return false;" class="btn btn-danger btn-sm margin-left-3 form-control">중지</button>')); | |
$('#stop-button').on('click', function () { | |
custom_data.status = 'stop'; | |
save(); | |
}); | |
custom_data.list = [...custom_data.list, ...$('tbody > tr').toArray().map(e => ({ | |
id: $(e).find('td:nth-child(1)').text(), | |
problem: $(e).find('td:nth-child(3)').text().trim(), | |
language: $(e).find('td:nth-child(7) > a:nth-child(1)').text().trim(), | |
code: location.origin + $(e).find('td:nth-child(7) > a:nth-child(1)').attr('href') | |
}))] | |
save(); | |
add_alert(`${custom_data.list.length} source crawled!`); | |
let is_next_valid = $('#next_page').attr('href'); | |
let next_url = location.origin + $('#next_page').attr('href'); | |
if (is_next_valid) { | |
location.href = next_url; | |
} else { | |
custom_data.current_source_index = 0; | |
custom_data.status = 'source'; | |
custom_data.list = custom_data.list.reverse(); | |
save(); | |
location.href = custom_data.list[custom_data.current_source_index].code; | |
} | |
} else if (custom_data.status == 'end') { | |
add_alert(`crawling end!`); | |
$.getScript('https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js', function () { | |
$.getScript('https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js', function () { | |
var zip = new JSZip(); | |
let list = custom_data.list; | |
for (let s of list) { | |
let name = 'P' + s.problem + '.' + language_map[s.language]; | |
zip.file(name, s.source); | |
} | |
zip.generateAsync({ | |
type: "blob" | |
}) | |
.then(function (content) { | |
// see FileSaver.js | |
saveAs(content, "sources.zip"); | |
}); | |
}); | |
}); | |
custom_data = {}; | |
save(); | |
} | |
} else if (path.startsWith('/source')) { | |
add_alert(`${custom_data.current_source_index + 1}/${custom_data.list.length} source crawled!`); | |
custom_data.list[custom_data.current_source_index++].source = document.getElementsByTagName('textarea')['source'].value; | |
if (custom_data.current_source_index >= custom_data.list.length) { | |
custom_data.status = 'end'; | |
save(); | |
location.href = custom_data.start_page; | |
} else { | |
save(); | |
location.href = custom_data.list[custom_data.current_source_index].code; | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment