Last active
September 9, 2016 13:03
-
-
Save Cyclodex/40f51a74d144a38e59dbc8f9b6322a5e to your computer and use it in GitHub Desktop.
Trac attachment helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Trac: Add attachment links | |
// @namespace Cyclodex | |
// @version 0.7 | |
// @description Add buttons next to attachments for using them directly as link or image in the comment. Also the image buttton will take the latest added attachment. | |
// @author Cyclodex <[email protected]> | |
// @match https://*/*trac/ticket/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
// Add attachment | |
function addAttachment(file) { | |
var cursorPos = $('#comment').prop('selectionStart'); | |
var v = $('#comment').val(); | |
var textBefore = v.substring(0, cursorPos ); | |
var textAfter = v.substring( cursorPos, v.length ); | |
$('#comment').val( textBefore + file + textAfter ); | |
//console.log('adding file: ' + file ); | |
} | |
// Add new buttons | |
function addButtons( selector ) { | |
// add a button to every rawlink | |
selector = typeof selector !== 'undefined' ? selector : '#changelog'; | |
console.log(selector); | |
$( selector ).find( ".trac-rawlink" ).each(function( index ) { | |
var attachment = $( this ).prev().text(); | |
$( this ).after( "<button class='link'>Use as link</button>" + "<button class='image'>Use as image</button>"); | |
$( this ).nextAll('button.link').click( function(){ | |
var link = '[[attachment:' + attachment + ']]'; | |
addAttachment(link); | |
}); | |
$( this ).nextAll('button.image').click( function(){ | |
var image = '[[Image(' + attachment + ')]]'; | |
addAttachment(image); | |
}); | |
}); | |
} | |
// When adding an image, instead of an empty tag, this takes the latest added attachment path as well | |
$('.wikitoolbar').find('#img').click( function(){ | |
var lastAttachment = jQuery('.attachments').find('dt:last-of-type').find('a:first-child').text(); | |
addAttachment(lastAttachment); | |
}); | |
// add button on initial call already | |
addButtons('#changelog'); | |
addButtons('#attachments'); | |
// add button when ajax request was done (otherwise we loose the buttons) | |
$( document ).ajaxComplete(function() { | |
addButtons( '#changelog' ); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment