Sang Anganggé:BaleDaja/common.js

Cutetan:Sesampun kawedar, Ragané patut ngentasin tembolok peramban wéb antuk nyingakin uahan.

  • Firefox/Safari: Taan Shift ritatkala ngaklik Reload, utawi teken Ctrl-F5 utawi Ctrl-R (⌘-R ring Mac)
  • Google Chrome: Taan Ctrl-Shift-R (⌘-Shift-R ring Mac)
  • Internet Explorer: Taan Ctrl ritatkala ngaklik Refresh, utawi teken Ctrl-F5
  • Opera: Teken Ctrl-F5.
  • Opera: Ngranjing ka Ménu → Pengaturan (Opera → Pustaka ring Mac) Salanturnyané ring Privacy & security → Clear browsing data → Cached images and files.
//[[Benutzer:Schnark/js/letzteredit.js]] Program untuk melihat pengguna yang aktif
mw.loader.load('https://de.wikipedia.org/w/index.php?title=Benutzer:Schnark/js/letzteredit.js&action=raw&ctype=text/javascript');

var variant = 'ban-x-pku';

mw.hook('wikipage.content').add(function () {
    var contentModel = mw.config.get('wgPageContentModel');
    var action = mw.config.get('wgAction');
    var title = mw.config.get('wgTitle');
    
    if (contentModel === 'proofread-page') {
        fixProofreadPageCss();
        if (action === 'view') {
            getTransliterationVariant();
            addTransliteration(mw.config.get('wgPageName'), $('.pagetext .mw-parser-output'), 'appendTo', getTransliterateApi());
        } else if (action === 'edit') {
            mw.loader.using('mediawiki.ForeignApi', function () { 
                getTransliterationVariant();
                transcriberEdit(title);
            });
        }
    } else if (contentModel === 'proofread-index' && action === 'view') {
        mw.loader.using('mediawiki.ForeignApi', function () {
            getTransliterationVariant();
            transcriberView(title);
        });
    } else if (title.match(/^Ban\//) && action === 'view' && mw.config.get('wgCanonicalNamespace') === '') {
        fixPagenums();
        fixMainCss();
        getTransliterationVariant();
        addTransliterationForPages();
    }
});

function transcriberLoad() {
    mw.loader.load('https://tools-static.wmflabs.org/palmleaf-transcriber/transcriber.css?v6', 'text/css');
    mw.loader.load('https://tools-static.wmflabs.org/palmleaf-transcriber/transcriber.js?v16');
}

function transcriberEdit(title) {
    var filename, pagenum;
    var match = title.match(/^(.+)\/([0-9]+)$/);
    if (match) {
        filename = match[1];
        pagenum = match[2] - 1;
    } else {
        filename = title;
        pagenum = 0;
    }

	var iaFile = getIaFile(filename);
    var imageUrl = $('.prp-page-image img').attr('src');
    if (!imageUrl) return;
    
    var textbox = document.getElementById('wpTextbox1');
    if (!textbox) return;
    textbox.style.fontFamily = "Noto Sans Balinese, Noto Serif Balinese, Vimala, monospace";

    getCommonsData(filename, pagenum).then(function (data) {
        if (!data) return;
        var imageData = data.iiifImageData;
        window.transcriberData = {
            mode: 'edit',
            textbox: textbox,
            imageUrl: imageUrl,
            leaf: pagenum,
            iaFile: iaFile,
            iaId: data.iaId,
            iiifBaseUrl: 'https:\/\/iiif.archivelab.org\/iiif',
            iiifDimensions: imageData ? (imageData.all || imageData.pages[pagenum]) : null,
            script: 'bali',
            language: 'ban-bali',
            variant: variant
        };
        transcriberLoad();
    });
}

function transcriberView(filename) {
    var iaFile = getIaFile(filename);
    var pagenum = 0;

    var wikipages = $('.prp-index-pagelist a').get().map(function (elt) {
        var href = elt.href;
        var match = href.match(/\/wiki\/(.+)$/);
        if (match) {
            return decodeURIComponent(match[1]);
        }
        match = href.match(/\btitle=(.+?)(?=&|$)/);
        if (match) {
            return decodeURIComponent(match[1]);
        }
        return null;
    });

    getCommonsData(filename, pagenum).then(function (data) {
        if (!data) return;
        window.transcriberData = {
            mode: 'view',
            commonsFile: filename,
            wikipages: wikipages,
            iaFile: iaFile,
            iaId: data.iaId,
            leaf: pagenum,
            iiifBaseUrl: 'https:\/\/iiif.archivelab.org\/iiif',
            iiifImageData: data.iiifImageData,
            script: 'bali',
            language: 'ban-bali',
            variant: variant
        };
        transcriberLoad();
    });
}

function getIaFile(filename) {
	var match = filename.match(/^Bali-lontar-(.+)\.pdf$/i);
	return match ? match[1] : null;
}

function getCommonsData(filename, pagenum) {
    var api = new mw.ForeignApi('https://commons.wikimedia.org/w/api.php');
    return new Promise(function (resolve, reject) {
        api.get({
            action: 'query',
            prop: 'iwlinks|revisions',
            titles: 'File:' + filename,
            rvprop: 'content',
            rvslots: '*',
            formatversion: 2,
            origin: '*'
        }, {
            xhrFields: { withCredentials: false }
        }).then(function (commonsRes) {
            var page = commonsRes.query.pages[0];
            if (page.missing) {
                resolve(null);
            } else {
                var transcriberData = {};
                page.iwlinks.some(function (link) {
                    if (link.prefix === 'iarchive') {
                        transcriberData.iaId = link.title;
                        return true; // short circuit
                    }
                    return false;
                });
                var match = page.revisions[0].slots.main.content.match(/<!-- ia-imagedata (.+?) -->/);
                if (match) {
                    transcriberData.iiifImageData = JSON.parse(match[1]);
                }
                resolve(transcriberData);
            }
        }).catch(reject);
    });
}

function getTransliterationVariant() {
    var userVariant = mw.user.options.get('variant-ban');
    if (userVariant && userVariant !== 'ban') {
        variant = userVariant;
    }
}

function getTransliterateApi() {
    return new mw.Api();
    //return new mw.ForeignApi(transliterateApi);    
}

function addTransliteration(page, elt, where, api) {
    api.get({
        action: 'parse',
        prop: 'text',
        text: '==== Auto-transliteration ====\n<span lang="ban"><langconvert from="ban-bali" to="' + variant + '">{{:' + page + '}}</langconvert></span>',
        contentmodel: 'wikitext',
        disablelimitreport: 1,
        disableeditsection: 1,
        formatversion: 2
    })
    .then(function (res) {
        if (!res.error) {
            var div = $(res.parse.text)[where](elt);
        }
    });
}

function fixPagenums() {
    $('.ws-noexport').attr('style', null).append('<br>');
}

function addTransliterationForPages() {
    var api = getTransliterateApi();
    $('.ws-pagenum').each(function () {
        var page = this.getAttribute('title');
        var startSpan = $(this).parents('.ws-noexport').first().parent();
        var nextSpan = startSpan.nextAll("span:has('.ws-pagenum')").first();
        if (nextSpan.length) {
            addTransliteration(page, nextSpan, 'insertBefore', api);
        } else {
            addTransliteration(page, startSpan.parent(), 'appendTo', api);
        }
    });
}

function fixProofreadPageCss() {
    var sheet = document.styleSheets[0];
    sheet.insertRule('[lang="ban-Bali"] .pagetext { text-align: start !important; word-break: break-all !important; overflow-wrap: anywhere !important; }');
}

function fixMainCss() {
    var sheet = document.styleSheets[0];
    sheet.insertRule('.mw-body-content [lang="ban-Bali"] p { word-break: break-all !important; overflow-wrap: anywhere !important; }');
}