imgbox tweaker

Adds custom formatted links and other improvements.

Από την 22/11/2022. Δείτε την τελευταία έκδοση.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name           imgbox tweaker
// @namespace      surrealmoviez.info
// @description    Adds custom formatted links and other improvements.
// @icon           data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAASBJREFUeNpiZACCltZWASDVD8QJDPQBC4C4sKa6+gMj1PL9QGzAQF9wAYgdmaA+p7flDFA7+5noGOzYQAITwwADFmyCHBwcDL4+PgxqampUseTWrVsMm7dsYfjx4weGHNYQoKblIAAyC2QmNsCESwO1AS4zB2caQAf/fn1meHegGUwTA5jYeBmEHGrBNFUc8OFYP8P3BwdJ8tm7AwwMIm5dhB1LjGF/vjwnOWiJDq2BTgOjDhh1wKgDRh1AlAOIKdMxyngeSepVRqCK5cMxHqKLZJDlAlaF1HMApHarG0FpANSGozbAZSZWB4AakNR0BKxRSnQaALVeV69ZM3LKgQUDaP8CkAMKoR1FegOQnYVMoC4yqJdK55AA2eUIshsgwAAogFP6tsoybwAAAABJRU5ErkJggg==
// @include        https://imgbox.com/
// @include        https://imgbox.com/upload/edit/*
// @include        https://imgbox.com/gallery/edit/*
// @require        https://code.jquery.com/jquery-3.5.1.min.js
// @version        1.1
// @grant          none
// @run-at         document-start
// ==/UserScript==
/*
//==============================================================================
//                         Version History:
//==============================================================================

1.1     -    Fixed: The script was breaking the upload page.

1.0     -    New feature: Working Copy buttons.
             Added an icon for the script.

0.0.4   -    Disabled centering for BBCode boxes (can be enabled with a variable below).

0.0.3   -    Fixed: the script wasn't working with the current imgbox.

0.0.2   -    Last version from SMz before his disappearance.

*/

// Change this to true to enable [center] BBCode:
const enable_center = false;


/**
 * Sets array elements into placeholders of a pattern.
 * @param {string} Base pattern. Placeholders as {%i} for the i-th replacement
 *        array.
 * @param {...string[]} Replacement sources for the pattern. The first array
 *        will set the returned array length.
 * @return {string[]} Replaced pattern elements.
 */
function createPatternedArray() {
  var pattern = arguments[0];
  var modArray = [];
  for (var i = 0; i < arguments[1].length; i++) {
    modArray[i] = pattern;
  }
  for (var j = 1; j < arguments.length; j++) {
    for (var k = 0; k < modArray.length; k++) {
      var replacement = arguments[j][k] || '';
      modArray[k] = modArray[k].split('{%' + j + '}').join(replacement);
    }
  }
  return modArray;
}


function copyInfoToClipboard(collect) {
  document.body.insertBefore(collect,document.body.firstChild);
  collect.focus();
  collect.select();
  const x = document.execCommand('copy');
  document.body.removeChild(collect);
}


function startImboxTweaker() {
  // Stop the script if on the upload page
  if ($('#upload-form').length === 1) {
    console.log("Imgbox Tweaker: Upload page detected. Terminate!");
    return;
  } else {
    console.log("Imgbox Tweaker: Upload page not detected. Continue!");
  }

  // Index page
  if ($('#upload-form').length === 1) {
    // Set defaults to enable 1-click upload
    $('#dropdown-content-type').val('2');
    $('#thumbnail-option').val('250r');
    $('#comments-option').val('0');
    $('#gallery-option').val('3');
  }

  // Upload result
  if ($('#codes-full').length === 1) {
    // Display all available outputs
    $('#codes-full').show().css('visibility', 'visible')
      .insertBefore('#codes-thumb');
    $('#codes-thumb').show().css('visibility', 'visible');

    // Extract direct links to full images and thumbs
    var links = [];
    $($('#code-html-full').text()).find('img').each(function() {
      links.push($(this).attr('src'));
    });

    var thumbs = [];
    $($('#code-html-thumb').text()).find('img').each(function() {
      thumbs.push($(this).attr('src'));
    });

    // Modify the existing outputs and titles, display all options
    $('#code-link-full')
      .text(links.join('\n'))
      .prev('div').children('span').text('Full size plain');
    $('#code-html-full')
      .text('<center>' +
        createPatternedArray('<img src="{%1}">', links).join('\n\n') +
        '</center>')
      .prev('div').children('span').text('Full size HTML');

    if(enable_center == true){
      $('#code-bb-full')
        .text('[center]' +
          createPatternedArray('[img]{%1}[/img]', links).join('\n\n') +
          '[/center]')
        .prev('div').children('span').text('Full size BBCode');
    } else {
      $('#code-bb-full')
        .text(createPatternedArray('[img]{%1}[/img]', links).join('\n\n'))
        .prev('div').children('span').text('Full size BBCode');
    }

    $('#code-link-thumb')
      .text(thumbs.join('\n'))
      .prev('div').children('span').text('Thumbs plain');
    $('#code-html-thumb')
      .text('<center>' +
        createPatternedArray('<a href="{%1}" target="imgbox-full-size">' +
          '<img src="{%2}"></a>', links, thumbs).join('\n\n') +
        '</center>')
      .prev('div').children('span').text('Linked thumbs HTML');

    if(enable_center == true){
      $('#code-bb-thumb')
        .text('[center]' +
          createPatternedArray('[url={%1}][img]{%2}[/img][/url]', links, thumbs)
            .join('\n\n') +
          '[/center]')
        .prev('div').children('span').text('Linked thumbs BBCode');
    } else {
      $('#code-bb-thumb')
        .text(createPatternedArray('[url={%1}][img]{%2}[/img][/url]', links, thumbs)
                .join('\n\n'))
        .prev('div').children('span').text('Linked thumbs BBCode');
    }

    // Preparing copy buttons
    let buttons = $('.clipboard-button');

    if (buttons.length > 2) {
      buttons.each(function() {
        let elem = $(this);
            elem.off("click");
        const collect_id  = elem.attr("data-clipboard-target");
        const collect_txt = $('#' +collect_id).text();

        let collect = document.createElement("textarea");
            collect.innerHTML+=collect_txt;
        elem.click(function() {
          copyInfoToClipboard(collect);
        });
      });
    }
  }
}

document.addEventListener('DOMContentLoaded', startImboxTweaker);