Add 2ndscript.js

This commit is contained in:
{{QWERTYKBGUI}} 2023-12-19 10:32:43 +01:00
parent 56153e9bf3
commit e619ab956c

82
2ndscript.js Normal file
View File

@ -0,0 +1,82 @@
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 2023-12-15
// @description try to take over the world!
// @author You
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Add a fixed-position toolbar with an input box and two buttons
const toolbarHTML = `
<div id="tm-toolbar" style="position:fixed; top:10px; right:10px; z-index:9999; background-color:white; border:1px solid black; padding:5px;">
<input type="text" id="tm-question-input" style="width:200px;">
<button id="tm-search-btn">Search and Copy</button>
<button id="tm-clipboard-btn">Clipboard Search</button>
<span id="tm-notification" style="margin-left:10px; color:green; display:none;">Copied!</span>
</div>
`;
document.body.insertAdjacentHTML('beforeend', toolbarHTML);
// Function to copy text to clipboard using the modern Clipboard API
function copyToClipboard(text) {
navigator.clipboard.writeText(text).then(function() {
console.log('Correct answers copied to clipboard:', text);
}, function(err) {
console.error('Could not copy text:', err);
});
}
// Refactored function to search for the question and copy correct answers
function performSearchAndCopy(questionName) {
const button = document.getElementById('tm-search-btn');
const allQuestions = document.querySelectorAll('.display_question');
// Reset the button color
button.style.backgroundColor = '';
let found = false;
let correctAnswersToCopy = '';
// Search for the question
allQuestions.forEach((questionDiv) => {
if (questionDiv.innerText.includes(questionName)) {
found = true;
// Find the correct answers within the same question container
const correctAnswers = questionDiv.parentElement.querySelectorAll('.correct_answer, .answer_text.correct');
correctAnswers.forEach((answer) => {
correctAnswersToCopy += answer.innerText + '\n'; // Collect all correct answers text
});
}
});
// Copy correct answers to clipboard if found
if (found && correctAnswersToCopy) {
copyToClipboard(correctAnswersToCopy.trim());
} else {
// Change button color to red if question is not found or no correct answers
button.style.backgroundColor = 'red';
}
}
// Event listener for the input-based search button
document.getElementById('tm-search-btn').addEventListener('click', () => {
const questionName = document.getElementById('tm-question-input').value;
performSearchAndCopy(questionName);
});
// Function to fetch text from clipboard and search for answers
function clipboardSearchAndCopyAnswers() {
navigator.clipboard.readText().then((clipboardText) => {
// Use the clipboard text as the question name
performSearchAndCopy(clipboardText);
}).catch((err) => {
console.error('Failed to read clipboard contents:', err);
});
}
// Event listener for the clipboard search button
document.getElementById('tm-clipboard-btn').addEventListener('click', clipboardSearchAndCopyAnswers);
})();