User:DreamRimmer/copyvio-check.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//Original at User:DannyS712/copyvio-check.js

$(document).ready(function() {
    if (mw.config.get('wgNamespaceNumber') !== 0 && mw.config.get('wgNamespaceNumber') !== 2) {
        return;
    }

    function runCheck() {
        var checkUrl = '//copyvios.toolforge.org/api.json?version=1&action=search&project=wikipedia&lang=en&title=' + encodeURIComponent(mw.config.get('wgTitle'));
        
        $.ajax({
            url: checkUrl,
            dataType: 'json',
            success: function(result) {
                if (result && result.best && result.best.confidence !== undefined) {
                    var confidence = Math.round(result.best.confidence * 100);
                    if (confidence < 50) {
                        $('#copyvio-check-report').css('background', '#EFE');
                    } else if (confidence > 50) {
                        $('#copyvio-check-report').css('background', '#FEE');
                    }
                    $('#copyvio-check-report').empty().append(
                        'Around ' + confidence + '% chance of being a copyvio (',
                        $('<a>')
                            .attr('id', 'copyvio-report-link')
                            .attr('href', '//copyvios.toolforge.org/?lang=en&project=wikipedia&title=' + encodeURIComponent(mw.config.get('wgTitle')) + '&oldid=&action=search&use_engine=1&use_links=1&turnitin=0&noredirect=true')
                            .attr('target', '_blank')
                            .text('view details'),
                        ')'
                    );
                } else {
                    $('#copyvio-check-report').text('Error: Unable to retrieve copyvio information.');
                }
            },
            error: function(xhr, status, error) {
                console.error('Error occurred while fetching copyvio information:', error);
                $('#copyvio-check-report').text('Error: Failed to fetch copyvio information.');
            }
        });
    }

    var counter = 0;
    setInterval(function() {
        if ($('.mwe-pt-toolbar-big').length && !counter) {
            counter++;
            var $copyvioDisplay = $('<div>')
                .attr('id', 'copyvio-check-report')
                .css('background', '#EFE')
                .css('padding', '0.5em')
                .text('Check for copyvio');
            if ($('.redirectMsg').length) {
                $copyvioDisplay.append('Redirects are not normally copyvios. ');
            }
            $('#mwe-pt-info > div.mwe-pt-tool-flyout').append($copyvioDisplay);
            $copyvioDisplay.on('click', function() {
                $copyvioDisplay.css('background', '#e8e8e8').text('Calculating copyvio percentage...');
                runCheck();
            });
        }
    }, 250);
});