User:Ronhjones/OTRSreceived.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Ronhjones/OTRSreceived. |
/* ========================================================================== *\
* *
* *
* Modified by User:Ronhjones, to show id= not ticket= *
* Copyright (c) 2007 Bryan Tong Minh. *
* Licensed under the terms of the MIT license *
* *
* ========================================================================== *
* *
* This scripts allows you to add OTRS received links in an easy manner. *
* It will replace {{OTRS pending}} by the correct permission template. *
* If no occurrence of {{OTRS pending}} can be found, it will OVERWRITE *
* the permission field of the information template. If the information *
* template is not available, the script will fail. *
* *
* ========================================================================== *
* *
* Tested with: Mozilla Firefox 2.0.0.6 *
* Install this script by adding the following code to your monobook.js: *
* *
\* ========================================================================== */
function addReceived(ticket)
{
var req = sajax_init_object();
req.open('GET', wgScriptPath + '/api.php?action=query&prop=info|revisions&' +
'format=json&intoken=edit&rvprop=content|timestamp&titles=' +
encodeURIComponent(mw.config.get('wgPageName')), false);
req.send(null);
var info = eval('(' + req.responseText + ')');
for (var key in info['query']['pages'])
{
var page = info['query']['pages'][key];
var token = page['edittoken'];
var content = page['revisions'][0]['*'];
var editTime = page['revisions'][0]['timestamp'].replace(/[^0-9]/g, '');
var rOTRS = new RegExp('\\{\\{Otrs[_ ]pending\\}\\}', 'i');
if (rOTRS.test(content))
{
content = content.replace(rOTRS, '{{OTRS received|id=' + ticket + '}}');
}
else
{
var rPermission = new RegExp('\\n\\|Permission[ \\t]*=.*', 'i');
if (rPermission.test(content))
{
content = content.replace(rPermission, '\n|Permission={{OTRS received|id=' +
ticket + '}}');
}
else
{
alert('No suitable place found to insert template!');
return;
}
}
var postdata = '';
postdata += 'wpTextbox1=' + encodeURIComponent(content);
postdata += '&wpSummary=' + encodeURIComponent('Adding OTRS received using [[User:Ronhjones/OTRSreceived.js]]');
postdata += '&wpSave=save';
postdata += '&wpEditToken=' + encodeURIComponent(token);
postdata += '&wpEdittime=' + editTime;
postdata += '&wpStarttime=' + editTime;
req = sajax_init_object();
req.open('POST', wgScriptPath + '/index.php?action=submit&title=' + encodeURIComponent(mw.config.get('wgPageName')), false);
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
req.setRequestHeader('Content-Length', postdata.length);
req.send(postdata);
document.close();
location.href = location.href; //Reload
return;
}
}
function OTRSrec()
{
var ticket = prompt('Ticket link?');
if (ticket) addReceived(ticket);
}
$(function () {
if (wgNamespaceNumber == 6) { //NS_IMAGE
var t = document.getElementById('t-whatlinkshere');
if (!t) return;
var li = document.createElement('li');
var a = document.createElement('a');
a.setAttribute('href', 'javascript:void(OTRSrec())');
a.appendChild(document.createTextNode('Received OTRS'));
li.appendChild(a);
t.parentNode.appendChild(li);
}
});