User:Ladsgroup/AHK.js
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
/*
A tool to add information template
Author: Amir (User:Ladsgroup)
*/
(function ($, mw) {
'use strict';
function setwindow (text) {
var uri = new mw.Uri();
uri.query = {
action: 'edit',
withJS: 'MediaWiki:AHK.js',
text: text,
summary: 'Adding Information template using [[User:Ladsgroup/AHK.js|tool]]',
};
window.location = uri.toString();
}
function check (text, callback) {
if (text.search(/\{\{[Ss]elf/) !== -1) {
var date = '';
var info = '';
try {
date = '{{According to EXIF data|' + document.getElementsByClassName('exif-datetimeoriginal')[0].lastElementChild.innerHTML.split(", ")[1] + '}}';
} catch (err) {
date = '';
}
var author = document.getElementById('mw-imagepage-section-filehistory').lastElementChild.lastElementChild.lastElementChild.children[4].firstElementChild.href.replace('&action=edit&redlink=1','').split('User:')[1];
info = '{{Information\n |description = $2\n |date = ' + date + '\n |source = {{own}}\n |author = [[User:' + author + ']]\n |permission = \n |other_versions = \n}}\n';
text = text.replace(/(^(?:\=\= *?\{\{int\:filedesc\}\} *?\=\=\n)?)([\s\S]*?)\n*?((?:\=\=[\s\S]+?)?\{\{[Ss]elf.+?\}\})/, "$1" + info + "$3");
text = text.replace(/\n?\[\[Category\:Media missing infobox template\]\]/, '');
callback(text);
} else {
info = '{{Information\n |description = $2\n |date = \n |source = \n |author = \n |permission = \n |other_versions = \n}}\n';
text = text.replace(/(^(?:\=\= *?\{\{int\:filedesc\}\} *?\=\=\n)?)([\s\S]*?)\n*?((?:\=\=[\s\S]+?)?\{\{)/, "$1" + info + "$3");
text = text.replace(/\n?\[\[Category\:Media missing infobox template\]\]/, '');
callback(text);
}
}
function loadPage(title, callback) {
new mw.Api().get({
action: 'query',
prop: 'revisions',
titles: title,
rvprop: 'content',
format: 'json'
}).done(function (data) {
try {
callback($.map(data.query.pages, function (value) {
return value;
})[0].revisions[0]['*'], setwindow);
}
catch (exception) {
callback('', setwindow)}
}).fail(function (data) {
callback($.map(data.query.pages, function (value) {
return value;
})[0].revisions[0], setwindow);
});
}
if (mw.config.get('wgNamespaceNumber') === 6 && !(document.getElementById("fileinfotpl_desc"))) {
$(function () {
$(mw.util.addPortletLink('p-views', '#', 'Add info', 'ca-ahk', 'Adding Information template'))
.click(function (e) {
loadPage(mw.config.get('wgPageName'), check);
});
});
}
}(jQuery, mediaWiki));