Guest User

8chan keyboard shortcuts

a guest
Apr 16th, 2025
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         8chan Keybinds
  3. // @namespace    alleyesonus
  4. // @version      2025-04-16
  5. // @description  Add keybinds to format text on 8chan.moe
  6. // @author       Anko
  7. // @match        https://8chan.moe/*
  8. // @grant        none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12.     'use strict';
  13.  
  14.     // Function to wrap selected text with the appropriate tag
  15.     function wrapSelection(tag) {
  16.         const textarea = document.activeElement;
  17.  
  18.         if (textarea && textarea.tagName === 'TEXTAREA') {
  19.             const start = textarea.selectionStart;
  20.             const end = textarea.selectionEnd;
  21.             const selectedText = textarea.value.substring(start, end);
  22.  
  23.             if (selectedText) {
  24.                 // Split selected text into lines and wrap each line (skip empty lines)
  25.                 const lines = selectedText.split('\n');
  26.                 const wrappedLines = lines.map(line => {
  27.                     // Skip empty lines
  28.                     if (line.trim() === '') return '';
  29.  
  30.                     switch (tag) {
  31.                         case 'spoiler':
  32.                             return `[spoiler]${line}[/spoiler]`;
  33.                         case 'greenText':
  34.                             return `>${line}`;
  35.                         case 'pinktext':
  36.                             return `<${line}`;
  37.                         case 'doom':
  38.                             return `[doom]${line}[/doom]`;
  39.                         case 'bizniz':
  40.                             return `==${line}==`;
  41.                         case 'moe':
  42.                             return `[moe]${line}[/moe]`;
  43.                         default:
  44.                             return line;
  45.                     }
  46.                 });
  47.  
  48.                 const wrappedText = wrappedLines.join('\n');
  49.  
  50.                 // Insert the wrapped text back into the textarea
  51.                 const beforeText = textarea.value.substring(0, start);
  52.                 const afterText = textarea.value.substring(end);
  53.                 textarea.value = beforeText + wrappedText + afterText;
  54.             }
  55.         }
  56.     }
  57.  
  58.     // Event listener for keydown events to capture keybinds
  59.     document.addEventListener('keydown', function(event) {
  60.         const textarea = document.activeElement;
  61.  
  62.         // Only respond if a textarea is focused
  63.         if (!textarea || textarea.tagName !== 'TEXTAREA') return;
  64.  
  65.         if (event.ctrlKey) {
  66.             let tag = null;
  67.             switch (event.key) {
  68.                 case 's':
  69.                     tag = 'spoiler';
  70.                     break;
  71.                 case 'p':
  72.                     tag = 'pinktext';
  73.                     break;
  74.                 case 'm':
  75.                     tag = 'moe';
  76.                     break;
  77.                 case 'd':
  78.                     tag = 'doom';
  79.                     break;
  80.                 case 'b':
  81.                     tag = 'bizniz';
  82.                     break;
  83.                 case 'g':
  84.                     tag = 'greenText';
  85.                     break;
  86.                 default:
  87.                     return;
  88.             }
  89.  
  90.             event.preventDefault();
  91.             wrapSelection(tag);
  92.         }
  93.     });
  94. })();
Advertisement
Add Comment
Please, Sign In to add comment