User:Zanhsieh/monobook.js

维基百科,自由的百科全书

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。

importScript("Wikipedia:維基百科工具/強迫更新/purge.js");


// define  custom buttons (id, class, popup title, image src, width, height, alt text, onClick and parameters)
var wikEdButton = {};
wikEdButton[100] = ['wikEdDiv', 'wikEdButton', 'Make the selection a html DIV element', 'http://upload.wikimedia.org/wikipedia/commons/6/67/WikEd_logo.png', '16', '16', 'DIV', 'javascript:WikEdEditButton(this, this.id, null, TestHandler);' ];
wikEdButton[101] = ['wikEdTest', 'wikEdButton', 'This is a test button', 'http://upload.wikimedia.org/wikipedia/commons/0/07/WikEd_disabled.png', '16', '16', 'Test', 'javascript:WikEdEditButton(this, this.id, null, TestHandler1);' ];

// define custom button bars (id outer, class outer, id inner, class inner, height, grip title, button numbers)
var wikEdButtonBar = {};
wikEdButtonBar['custom1'] = ['wikEdButtonBarCustom1',  'wikEdButtonBarCustom1',  'wikEdButtonsCustom1',  'wikEdButtonsCustom1',  44, 'My custom buttons', [100, 'br', 101] ];

// define the function which is called upon clicking the custom button

function TestHandler(obj) {

// select the appropriate text change target (whole, selection, cursor, focusWord, focusLine, selectionWord, or selectionLine)
//   focus... is the text under the cursor; ...Word and ...Line extend the target to the start/end of the word or line
WikEdGetText(obj, 'selection, cursor');
if (obj.selection.plain != '') {
obj.changed = obj.selection;
}
else {
obj.changed = obj.cursor;
}

// make the changes to the plain target text

// remove the previously added formatting
if ( /<div>(.*?)<\/div>/i.test(obj.changed.plain) ) {
obj.changed.plain = obj.changed.plain.replace(/<div>(.*?)<\/div>/gi, '$1');
}

// add the text formatting
else {
obj.changed.plain = '<div>' + obj.changed.plain + '</div>';
obj.changed.plain = obj.changed.plain.replace(/(<div>)( *)(.*?)( *)(<\/div>)/, '$2$1$3$5$4');
}

// keep the changed text selected, needed to remove the formatting with a second click
obj.changed.keepSel = true;

return;
}

function TestHandler1(obj) {
WikEdGetText(obj, 'whole');
obj.changed = obj.whole;
regExpFind         = new RegExp("([^,]*),");
regExpFindRefBegin = new RegExp("<ref[^>]*>");
regexpFindRefEnd   = new RegExp("</ref>");
var inRef = 0;
while (regExpFind.test(obj.changed.plain)) {
    if (regExpFindRefBegin.test(obj.changed.plain)) {
        inRef = 1;
    } else {
        if (inRef == 0) {
            obj.changed.plain = obj.changed.plain.replace(regExpFind, "$1,");
        } else if (regExpFindRefEnd.test(obj.changed.plain)) {
            inRef = 0;
            obj.changed.plain = obj.changed.plain.replace(regExpFind, "$1,");
        } else {
        }
    }
}
return;
}

//function TestHandler1(obj) {
//WikEdGetText(obj, 'whole');
//obj.changed = obj.whole;
//regExpFind = new RegExp(",");
//while (regExpFind.test(obj.changed.plain)) {
//    obj.changed.plain = obj.changed.plain.replace(regExpFind, ',');
//}
//return;
//}