User:Ladsgroup/AHK.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
/* 
    A tool to add information template
    Author: Amir (User:Ladsgroup)
*/
(function ($, mw) {
    'use strict';

    function setwindow (text) {
        alert(text);
        return
        var uri = new mw.Uri();
        uri.query = {
            action: 'edit',
            withJS: 'User:Ladsgroup/AHK2.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));