User:Liangent/Scripts/Copyvio.js

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

$(function() {
    addPortletLink(
        'p-cactions',
        'javascript:copyvio();',
        'cv',
        'ca-copyvio',
        'Mark as copyright violation'
    );
});

function copyvio() {
    var $ = jQuery;
    var urlPromptMessage = 'URL (cancel = abort, leave blank = finished)';
    var rawurl = prompt(urlPromptMessage);
    if (rawurl == null || rawurl == '') {
        return;
    }
    var url = '*' + rawurl;
    while (true) {
        rawurl = prompt(urlPromptMessage);
        if (rawurl == null) {
            return;
        } else if (rawurl == '') {
            break;
        } else {
            url = url + '\n*' + rawurl;
        }
    }
    $.ajax({
        type: 'POST',
        url: wgScriptPath + '/api.php',
        data: {
            format: 'xml',
            action: 'query',
            prop: 'info',
            intoken: 'edit',
            titles: wgPageName,
        },
        error: function() {
            alert('Cannot get edit token');
        },
        success: function(xml) {
            if ($('error, warnings', xml).length != 0) {
                alert('Cannot get edit token');
                return;
            }
            var token = $('page', xml).attr('edittoken');
            $('#jump-to-nav').after(
                '<div id="copyvio-marking">Adding Template:Copyvio ... <span id="copyvio-marking-status"></span></div>' + 
                '<div id="copyvio-vfd">Adding link on WP:CV ... <span id="copyvio-vfd-status"></span></div>' + 
                '<div id="copyvio-warning">Warning creator:&nbsp;<span id="copyvio-creator-username">(Loading...)</span> ... <span id="copyvio-warning-status"></span></div>'
            );
            function updateStatus(id, text, iserror) {
                return function(xml) {
                    $('#' + id).text(iserror ? 'Failed' : ($('error, warnings', xml).length == 0 ? 'Done' : 'Failed'));
                };
            }
            // {{copyvio}}
            $.ajax({
                type: 'POST',
                url: wgScriptPath + '/api.php',
                data: {
                    format: 'xml',
                    action: 'edit',
                    title: wgPageName,
                    text: '{\{subst:Copyvio/auto|url=' + url + '}}',
                    token: token
                },
                error: updateStatus('copyvio-marking-status', true),
                success: updateStatus('copyvio-marking-status', false)
            });
            // vfd
            $.ajax({
                type: 'POST',
                url: wgScriptPath + '/api.php',
                data: {
                    format: 'xml',
                    action: 'edit',
                    title: 'Wikipedia:頁面存廢討論/疑似侵權',
                    section: 'new',
                    text: '{\{subst:CopyvioVFDRecord|' + wgPageName + '}}',
                    token: token
                },
                error: updateStatus('copyvio-vfd-status', true),
                success: updateStatus('copyvio-vfd-status', false)
            });
            // warning
            $.ajax({
                type: 'POST',
                url: wgScriptPath + '/api.php',
                data: {
                    format: 'xml',
                    action: 'query',
                    prop: 'revisions',
                    titles: wgPageName,
                    rvlimit: '1',
                    rvprop: 'user',
                    rvdir: 'newer'
                },
                error: function() {
                    $('#copyvio-creator-username').text('(Failed)');
                },
                success: function(xml) {
                    if ($('error, warnings', xml).length != 0) {
                        $('#copyvio-creator-username').text('(Failed)');
                        return;
                    }
                    var username = $('rev', xml).attr('user');
                    $('#copyvio-creator-username').text(username);
                    $.ajax({
                        type: 'POST',
                        url: wgScriptPath + '/api.php',
                        data: {
                            format: 'xml',
                            action: 'edit',
                            title: 'User talk:' + username,
                            section: 'new',
                            text: '{\{subst:CopyvioNotice|' + wgPageName + '}}',
                            token: token
                        },
                        error: updateStatus('copyvio-warning-status', true),
                        success: updateStatus('copyvio-warning-status', false)
                    });
                }
            });
        }
    });
}
//</nowiki>