Skip to content

Instantly share code, notes, and snippets.

@qb20nh
Created November 7, 2023 10:08
Show Gist options
  • Save qb20nh/9542ffc8f586f7d3a6579526206b221b to your computer and use it in GitHub Desktop.
Save qb20nh/9542ffc8f586f7d3a6579526206b221b to your computer and use it in GitHub Desktop.
Keyboard submit on programmers.co.kr
// ==UserScript==
// @name Ctrl+Enter executes for programmers.co.kr
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://school.programmers.co.kr/learn/courses/*/lessons/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=programmers.co.kr
// @grant none
// ==/UserScript==
(async function() {
'use strict';
function forElement(selector, rootNode = document) {
return new Promise((resolve, reject) => {
const element = rootNode.querySelector(selector);
if (element) {
resolve(element);
return;
}
const observer = new MutationObserver(mutations => {
const element = rootNode.querySelector(selector);
if (element) {
observer.disconnect();
resolve(element);
}
});
observer.observe(rootNode, {
childList: true,
subtree: true
});
});
}
function forProperty(obj, propName, timeout = 3000, interval = 100) {
return new Promise((resolve, reject) => {
const startTime = Date.now();
// Function to check the property
function checkProperty() {
// If the property exists, resolve the promise with its value
if (propName in obj) {
resolve(obj[propName]);
} else {
// If the timeout has not elapsed, check again after the interval
if (Date.now() - startTime < timeout) {
setTimeout(checkProperty, interval);
} else {
// If the timeout has elapsed, reject the promise
reject(new Error(`Property ${propName} was not found within ${timeout}ms`));
}
}
}
// Start the polling
checkProperty();
});
}
const cm = await forProperty(await forElement('.CodeMirror'), 'CodeMirror');
const codeMirrorTextarea = document.querySelector('.CodeMirror textarea');
const runCodeBtn = document.getElementById('run-code');
// Your code here...
document.addEventListener('keydown', ({ctrlKey, key}) => {
if (ctrlKey && key === 'Enter') {
const focusElement = document.querySelector(':focus');
if (focusElement === codeMirrorTextarea) {
runCodeBtn.click();
cm.execCommand('undo');
}
}
}, {
passive: true
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment