Last active
November 9, 2015 02:27
-
-
Save whatalnk/14d4fd863594ac94373b to your computer and use it in GitHub Desktop.
AtCoderの入出力を取得
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 getExampleCases | |
// @namespace what_alnk | |
// @description AtCoderの入力例と出力例をコピーするボタンを設置する | |
// @include /^http://[0-9a-zA-Z]+\.contest\.atcoder\.jp/tasks/[A-D]/ | |
// @version 1.2 | |
// @grant none | |
// ==/UserScript== | |
// 通知許可確認 | |
Notification.requestPermission(); | |
// 通知 | |
var showMessage = function (msg) { | |
var n = new Notification(msg); | |
setTimeout(n.close.bind(n), 2000); | |
} | |
// 入力例(出力例)探す | |
var allParts = document.getElementsByClassName('part'); | |
var exampleCases = new Array; | |
var reInput = /入力例/; | |
var reOutput = /出力例/; | |
Array.prototype.forEach.call(allParts, function (item1) { | |
var h3Elements = item1.getElementsByTagName('h3'); | |
Array.prototype.forEach.call(h3Elements, function (item2) { | |
if (reInput.test(item2.textContent) || reOutput.test(item2.textContent)) { | |
exampleCases.push(item2); | |
} | |
}) | |
}) | |
// 入力例(出力例)コピー | |
var copyExampleCase = function () { | |
var range = document.createRange(); | |
range.selectNode(this.parentNode.getElementsByTagName('pre') [0]); | |
window.getSelection().addRange(range); | |
try { | |
document.execCommand('copy'); | |
showMessage('コピーしました.'); | |
} catch (e) { | |
console.log('Copy error'); | |
} | |
window.getSelection().removeAllRanges(); | |
} | |
// ボタン定義 | |
var copyButton = document.createElement('div'); | |
var copyButton_content = document.createElement('button'); | |
copyButton_content.textContent = 'Copy'; | |
copyButton_content.style.marginBottom = '3px'; | |
copyButton.appendChild(copyButton_content); | |
copyButton.style.display = 'inline-block'; | |
copyButton.className = 'inputOutputExampleCopy'; | |
// ボタン設置 | |
Array.prototype.forEach.call(exampleCases, function (item) { | |
var copyButton_clone = copyButton.cloneNode(true); | |
copyButton_clone.addEventListener('click', copyExampleCase); | |
h3Node = item; | |
h3Node_parent = item.parentNode; | |
h3Node_parent.insertBefore(copyButton_clone, h3Node.nextSibling); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Installation