KAT - Add IMG bbcode

Allows highlighting url and then clicking or using Ctrl + I to make IMG bbcode

Fra 26.07.2015. Se den seneste versjonen.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

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

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        KAT - Add IMG bbcode
// @namespace   IMGbbcode
// @version     1.02
// @description  Allows highlighting url and then clicking or using Ctrl + I to make IMG bbcode
// @match      http://kat.cr/*
// @match      https://kat.cr/*
// ==/UserScript==

// Enabled - 1 - No highlighting / invalid highlight prompts the user to enter the URL - Default
// Disabled - 0 - No highlighting / invalid highlight just adds [IMG][/IMG]
var promptUser = 1;

$(".bbedit-toolbar").append('<span class="bbedit-img" title="Use URL" style="background: url(\'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAIAAAAC64paAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAYdEVYdFNvZnR3YXJlAHBhaW50Lm5ldCA0LjAuNWWFMmUAAAD9SURBVDhP7ZChskVQFIbvo4iiIAiCKHgAQfAAgiAIZkRBFAVBFERBEAQPIYiCIAqC+7l75ow5o5gTz/3Tv9b+vzVrr5/jA/3DD/UxvCyL7/uYYRhM05ym6e/pVJZlrusKT8zzPMMwLMtq25bOCZOmhSmKQlVVEmf2OLZt03VdkiT8uq6yLNd1ve/7PM9VVdF8h6MoIgRGmed5mqYCTpIkjmPMVe8wiTAMMZSaprGqgFm+aRqM4ziExaAbeBxHsL7vbdumKWCOwiIY9MrfwBhKRVG6rsMLmFvyHX5+zd/DZVkSxSABI145p5gbBAGdE34kTs3BhX8MX/V98HH8ApmOKX9Nyo+KAAAAAElFTkSuQmCC\')"></span>');

$('.bbedit-img').click(function()
{ 
    if (window.getSelection) 
    {
        var ta = $('textarea').get(0);
        var start = ta.selectionStart;
        var end = ta.selectionEnd;
        var text = ta.value.substring(start, end);
        if(/^https?:\/\/|www/i.test(text)) 
        {
            ta.value = ta.value.substring(0, start)
            + '[IMG]' + text + '[/IMG]'
            + ta.value.substring(end, ta.value.length);
        }
        else
        {
            var newValue = ta.value.substring(0, start);
            if (promptUser == 1)
            {
                var url=prompt('Image URL: ','');
                if(url!==null && url!=='' && /^https?:\/\/|www/i.test(url))
                {
                    newValue += '[IMG]' + url + '[/IMG] ';
                }
                else
                {
                    newValue += '[IMG][/IMG] ';
                }  
            }
            else
            {
                newValue += '[IMG][/IMG] ';
            } 
            newValue += text + ta.value.substring(end, ta.value.length);
            ta.value = newValue;
            if (promptUser == 0 && ta.setSelectionRange) ta.setSelectionRange(start + 5, start + 5);
            
        }
    }
});

$("textarea").keydown(function(event)
{
    if (event.ctrlKey && event.which == 73) 
    {
        event.preventDefault();
        $('.bbedit-img').trigger("click");
    }
});