Skip to content

Instantly share code, notes, and snippets.

@tamago324
Last active June 20, 2023 23:58
Show Gist options
  • Save tamago324/6972c3d6607f2e939154d5dd8af3eb0a to your computer and use it in GitHub Desktop.
Save tamago324/6972c3d6607f2e939154d5dd8af3eb0a to your computer and use it in GitHub Desktop.
AtCoder Text Cleanup for Kotlin
// ==UserScript==
// @name AtCoder Text Cleanup for Kotlin
// @namespace http://atcoder.jp/
// @version 0.4
// @description Remove lines starting with @file or package and blank lines when pasting into text boxes on AtCoder contest pages.
// @author tamgo324
// @include https://atcoder.jp/contests/*/tasks/*
// @include https://atcoder.jp/contests/*/submit
// @grant none
// ==/UserScript==
/**
* "@file" か "package " で始まる行を削除して貼り付ける
* また、 "@file" と "package" で始まる行の後にある空白行も削除します
* もし、Test & Submit ボタンがあったら、それをクリックします。
*/
(function() {
'use strict';
// Wait until the DOM is fully loaded
window.addEventListener('load', function() {
// Get all CodeMirror instances
var codeMirrors = document.querySelectorAll('.CodeMirror');
codeMirrors.forEach(cmElement => {
// Get the actual CodeMirror instance
var cmInstance = cmElement.CodeMirror;
cmInstance.on('paste', function(instance, event) {
// Get the paste data as text
var pasteText = (event.clipboardData || window.clipboardData).getData('text');
// Stop the original paste event
event.preventDefault();
// Split the pasted text by line
var lines = pasteText.split('\n');
// Create a new array to hold the cleaned lines
var cleanedLines = [];
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
// If the line starts with '@file' or 'package ', skip this line and the next blank line
if (line.startsWith('@file') || line.startsWith('package ')) {
if (i + 1 < lines.length && lines[i + 1].trim() === '') {
i++; // Skip the next blank line
}
} else {
// Add non-blank lines to the cleanedLines array
cleanedLines.push(line);
}
}
var newText = cleanedLines.join('\n');
// Get the current selection range
var selection = {from: cmInstance.getCursor('start'), to: cmInstance.getCursor('end')};
// Replace the selected range with the new text
cmInstance.replaceRange(newText, selection.from, selection.to);
// Get the "Test and Submit" button by id
var button = document.getElementById('atcoder-easy-test-btn-test-and-submit');
// If the button exists, click it
if (button) {
button.click();
}
});
});
});
})();
@tamago324
Copy link
Author

v0.2 "@file" と "package" で始まる行の間にある空白行も削除するように修正

@tamago324
Copy link
Author

v0.3 全体を選択してから貼り付けをしても正常に貼り付けできるように変更

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment