FiveGuard Notes

Add notes to FiveGuard license entries

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

You will need to install an extension such as Tampermonkey to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         FiveGuard Notes
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Add notes to FiveGuard license entries
// @author       R4Z0R
// @match        *://*/*
// @grant        GM_getValue
// @grant        GM_setValue
// @license MIT
// ==/UserScript==

(function() {
   'use strict';

   const style = `
       .note-textarea {
           width: 100%;
           padding: 5px;
           margin: 2px;
           border: 1px solid #ccc;
       }

       .license-note-cell {
           min-width: 150px;
       }
   `;

   const styleSheet = document.createElement("style");
   styleSheet.textContent = style;
   document.head.appendChild(styleSheet);

   function loadNote(licenseId) {
       return GM_getValue(licenseId, '');
   }

   function saveNote(licenseId, note) {
       GM_setValue(licenseId, note);
   }

   function addNotesToTable() {
       const rows = document.querySelectorAll('tr[id^="Permissions_AlternativePermissions_license_"]');

       rows.forEach(row => {
           const licenseId = row.id.replace('Permissions_AlternativePermissions_license_', '');

           const existingNotes = row.querySelectorAll('.license-note-cell');
           if (existingNotes.length > 0) {
               existingNotes.forEach((note, index) => {
                   if (index > 0) note.remove();
               });
               return;
           }

           const noteCell = document.createElement('td');
           noteCell.className = 'license-note-cell';

           const noteInput = document.createElement('input');
           noteInput.type = 'text';
           noteInput.placeholder = 'Add note here';
           noteInput.className = 'note-textarea';

           const savedNote = loadNote(licenseId);
           noteInput.value = savedNote;

           noteInput.addEventListener('change', () => {
               saveNote(licenseId, noteInput.value);
           });

           noteInput.addEventListener('keyup', (event) => {
               if (event.key === 'Enter') {
                   saveNote(licenseId, noteInput.value);
                   noteInput.blur();
               }
           });

           noteCell.appendChild(noteInput);

           const actionCell = row.querySelector('.d-flex')?.parentElement;
           if (actionCell) {
               row.insertBefore(noteCell, actionCell);
           } else {
               row.appendChild(noteCell);
           }
       });
   }

   function addNotesHeader() {
       const table = document.querySelector('table[id="Permissions_AlternativePermissions"]');
       if (table && !table.querySelector('th.notes-header')) {
           const headerRow = table.querySelector('thead tr');
           const noteHeader = document.createElement('th');
           noteHeader.className = 'notes-header';
           noteHeader.textContent = 'Notes';
           headerRow.insertBefore(noteHeader, headerRow.lastElementChild);
       }
   }

   addNotesHeader();
   addNotesToTable();

   function debounce(func, wait) {
       let timeout;
       return function executedFunction(...args) {
           const later = () => {
               clearTimeout(timeout);
               func(...args);
           };
           clearTimeout(timeout);
           timeout = setTimeout(later, wait);
       };
   }

   const debouncedInit = debounce(() => {
       addNotesHeader();
       addNotesToTable();
   }, 250);

   const observer = new MutationObserver(() => {
       debouncedInit();
   });

   observer.observe(document.body, {
       childList: true,
       subtree: true
   });
})();