User:Azermite/citecheck.js

// ==UserScript== // @name        Wikipedia CiteCheck Source Finder // @namespace   https://citecheck.nouspo.com // @version     1.4 // @description Find sources for selected text using CiteCheck API // @author      Salim Masmoudi // @match       https://en.wikipedia.org/* // @grant       none // ==/UserScript==

(function {   'use strict';

// Function to send text to CiteCheck API and display results async function findSources(text) { console.log('Sending selected text to CiteCheck API:', text); // Log the selected text

try { const response = await fetch('https://citecheck.nouspo.com/check', {               method: 'POST',                headers: {                    'Content-Type': 'application/json'                },                body: JSON.stringify({ text: text })            });

const urls = await response.json; console.log('Response received from CiteCheck API:', urls); // Log the API response displaySources(urls); } catch (error) { console.error('Error connecting to the CiteCheck API:', error); alert('Error connecting to the CiteCheck API.'); }   }

// Function to display the sources function displaySources(urls) { if (urls.length === 0) { alert('No sources found.'); return; }

let sourcesList = 'Potential sources:\n\n'; urls.forEach((url, index) => {           sourcesList += `${index + 1}. ${url}\n`;        });

alert(sourcesList); }

// Handle the keyboard shortcut (Ctrl+Shift+S) function handleKeyboardShortcut(event) { if (event.ctrlKey && event.shiftKey && event.key === 'S') { console.log('Keyboard shortcut Ctrl+Shift+S pressed'); // Log the shortcut press

const selectedText = window.getSelection.toString; if (!selectedText) { alert('Please select some text to find sources.'); console.log('No text selected'); // Log if no text is selected return; }

findSources(selectedText); }   }

// Wait for the page to fully load window.addEventListener('keydown', handleKeyboardShortcut); });