MediaWiki:Common.js

From Thank You
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */

// Framebuster for https://phabricator.wikimedia.org/T176669
if ( self !== top ) {
    top.location.href = window.location;
}

// Redirect anons to en if page doesn't exist
if ( mw.config.get('wgUserName') === null && mw.config.get('wgArticleId') === 0 ) {
    var basePageName = mw.config.get('wgPageName').split('/')[0];
    var redirectUrl = 'https://thankyou.wikipedia.org/wiki/';
    if ( basePageName === 'Recurring_donor_page' ) {
        redirectUrl += 'Recurring_donor_page/en';
    } else {
        redirectUrl += 'Thank_You/en';
    }
    redirectUrl += window.location.search; // params
    window.location.replace( redirectUrl );
}

mw.loader.using( [ 'mediawiki.cookie', 'mediawiki.Uri' ], function() {

	/**
     * Extend a url with a subset of query parameters
     *
     * @param {string} url             Base url to extend
     * @param {object} allParams       Object with all available query parameters (usually for the current location)
     * @param {array}  paramsToInclude List of parameters to include in the extended url
     * @return {string} Extended url
     */
    function extendWithSomeParams( url, allParams, paramsToInclude ) {
        var uriObject = new mw.Uri( url ),
            filteredParams = {};

        // Fallback for survey links
        allParams.wmf_medium   = allParams.wmf_medium   || allParams.utm_medium;
        allParams.wmf_campaign = allParams.wmf_campaign || allParams.utm_campaign;
        allParams.wmf_source   = allParams.wmf_source   || allParams.utm_source;

        paramsToInclude.forEach( function(key) {
            if ( allParams[key] ) {
                filteredParams[key] = allParams[key];
            }
        });

        uriObject.extend( filteredParams );
        return uriObject.toString();
    }

    /* -- Start of main TY page code -- */

    var params = new mw.Uri( document.location.href ).query,
        country = params.country,
        pageLanguage = mw.config.get('wgPageName').split('/')[1];

    if ( params.payment_method === 'bt' ) {
        document.getElementById('ty-banktransfer').style.display = 'block';
    }

    if ( params.recurringConversion === '1' ) {
        document.getElementById('ty-recurringConversion').style.display = 'block';
    }
    
    // Recurring Upgrade
    if ( params.recurUpgrade === '1' ) {
        var recurUpgradeAmt = params.recurAmount;
        var recurUpgradeDate = params.recurDate;
        $('.recurUpgrade-yes').show();
        if ( recurUpgradeAmt && recurUpgradeDate ) {
        	$('.recurUpgradeAmt').text(recurUpgradeAmt);
        	if ( params.country === 'US' ) {
        		$('.recurUpgradeAmt').prepend("$");
        	}
        	var upgradeDate = new Date(recurUpgradeDate);
            var formatOptions = { timeZone: 'UTC', weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
            $('.recurUpgradeDate').text( upgradeDate.toLocaleDateString('en-US', formatOptions) );
        	$('.recurUpgrade-receipt').show();
        } else {
        	$('.recurUpgrade-receipt').hide();
        }
    } else if ( params.recurUpgrade === '0' ) {
        $('.recurUpgrade-no').show();
    } else {
        $('.ty-vision, .ty-text').show();
    }

    // Extend survey links
    $('.ty-survey-link, a[href^="https://www.surveymonkey.com"]').attr('href', function( i, val ) {
        return extendWithSomeParams( val, params, [ 'country', 'payment_method', 'recurring', 'recurringConversion', 'amount', 'currency', 'order_id', 'wmf_medium', 'wmf_campaign', 'wmf_source' ] );
    });

    // Return to Wikipedia link
    var returnToUrl = mw.cookie.get( 'fundraising_returnTo', '' );
    if ( returnToUrl ) {
        $( '.ty-return a' ).attr( 'href', returnToUrl );
        $( '.ty-return' ).show();
        mw.cookie.set( 'fundraising_returnTo', null, { prefix: '', domain: '.wikipedia.org' } );
    }

    // TODO: refactor this to be by feature instead of country?
    if ( params.featureEndowment ) {
        // To force endowment feature
        $( '.primary-ctas .endowment, \
            .other-ctas, \
            .other-ctas .store, \
            .other-ctas .survey, \
            .other-ctas .edit-6c' ).show();
    } else if ( country === 'US' ) {
        $( '.primary-ctas .endowment, \
            .other-ctas, \
            .other-ctas .store, \
            .other-ctas .survey, \
            .other-ctas .edit-6c' ).show();
    } else if ( country === 'CA' || country === 'GB' ) {
        $( '.primary-ctas .survey, \
            .other-ctas, \
            .other-ctas .store, \
            .other-ctas .will-notUS, \
            .other-ctas .edit-6c' ).show();
    } else if ( country === 'IE' || country === 'AU' || country === 'NZ' ) {
        $( '.primary-ctas .survey, \
            .other-ctas, \
            .other-ctas .store, \
            .other-ctas .edit-6c' ).show();
    } else if ( country === 'IT' || country === 'PT' || country === 'BR' || country === 'JP' || country === 'FR' ) {
        $( '.primary-ctas .survey, \
            .other-ctas, \
            .other-ctas .store' ).show();
    } else if ( country === 'ES' || country === 'CL' || country === 'CO' || country === 'MX' || country === 'PE' || country === 'UY' ) {
        $( '.primary-ctas .survey, \
            .other-ctas, \
            .other-ctas .store' ).show();
    } else {
        $( '.primary-ctas .survey' ).show();
    }

    if ( country === 'SE' ) {
        $('.chapter-SE').show();
    }

    if ( country === 'CZ' ) {
        $('.chapter-CZ').show();
    }
    
    if ( country === 'IT' ) {
    	$('.chapter-IT').show();
    }
    
    if ( country === 'AU' ) {
    	$('.chapter-AU').show();
    }
    
    if ( country === 'GB' ) {
    	$('.chapter-GB').show();
    }
    
    if ( country === 'CA' ) {
    	$('.chapter-CA').show();
    }
    
    if ( country === 'NO' ) {
    	$('.chapter-NO').show();
    }
    
    if ( country === 'BE' ) {
    	$('.chapter-BE').show();
    }

    /* -- "Try editing" feature -- */

    var editingUrls = {
        'IN': {
            'en': 'https://en.wikipedia.org/w/index.php?title=Special:CreateAccount&geEnabled=1&campaign=typage-in-en-2022'
        },
        'ZA': {
            'en': 'https://en.wikipedia.org/w/index.php?title=Special:CreateAccount&geEnabled=1&campaign=typage-za-en-2022'
        },
        // Latin America
        'BR': {
            'en': 'https://en.wikipedia.org/w/index.php?title=Special:CreateAccount&geEnabled=1&campaign=typage-latam-en-2022',
            'pt-br': 'https://pt.wikipedia.org/w/index.php?title=Special:CreateAccount&geEnabled=1&campaign=typage-latam-pt-2022&uselang=pt-br'
        },
        'AR': {
            'en': 'https://en.wikipedia.org/w/index.php?title=Special:CreateAccount&geEnabled=1&campaign=typage-latam-en-2022',
            'es-419': 'https://es.wikipedia.org/w/index.php?title=Special:CreateAccount&geEnabled=1&campaign=typage-latam-es-2022'
        },
        'CL': {
            'en': 'https://en.wikipedia.org/w/index.php?title=Special:CreateAccount&geEnabled=1&campaign=typage-latam-en-2022',
            'es-419': 'https://es.wikipedia.org/w/index.php?title=Special:CreateAccount&geEnabled=1&campaign=typage-latam-es-2022'
        },
        'CO': {
            'en': 'https://en.wikipedia.org/w/index.php?title=Special:CreateAccount&geEnabled=1&campaign=typage-latam-en-2022',
            'es-419': 'https://es.wikipedia.org/w/index.php?title=Special:CreateAccount&geEnabled=1&campaign=typage-latam-es-2022'
        },
        'MX': {
            'en': 'https://en.wikipedia.org/w/index.php?title=Special:CreateAccount&geEnabled=1&campaign=typage-latam-en-2022',
            'es-419': 'https://es.wikipedia.org/w/index.php?title=Special:CreateAccount&geEnabled=1&campaign=typage-latam-es-2022'
        },
        'PE': {
            'en': 'https://en.wikipedia.org/w/index.php?title=Special:CreateAccount&geEnabled=1&campaign=typage-latam-en-2022',
            'es-419': 'https://es.wikipedia.org/w/index.php?title=Special:CreateAccount&geEnabled=1&campaign=typage-latam-es-2022'
        },
        'UY': {
            'en': 'https://en.wikipedia.org/w/index.php?title=Special:CreateAccount&geEnabled=1&campaign=typage-latam-en-2022',
            'es-419': 'https://es.wikipedia.org/w/index.php?title=Special:CreateAccount&geEnabled=1&campaign=typage-latam-es-2022'
        },
        // Northern and Western Europe
        'SE': {
            'sv': 'https://sv.wikipedia.org/w/index.php?title=Special:CreateAccount&campaign=typage-nwe-sv-2023',
            'en': 'https://en.wikipedia.org/w/index.php?title=Special:CreateAccount&campaign=typage-nwe-sv-2023'
        },
        'IT': {
            'it': 'https://it.wikipedia.org/w/index.php?title=Special:CreateAccount&campaign=typage-nwe-it-2023',
            'en': 'https://en.wikipedia.org/w/index.php?title=Special:CreateAccount&campaign=typage-nwe-it-2023'
        },
        'FR': {
            'fr': 'https://fr.wikipedia.org/w/index.php?title=Special:CreateAccount&campaign=typage-nwe-fr-2023',
            'en': 'https://en.wikipedia.org/w/index.php?title=Special:CreateAccount&campaign=typage-nwe-fr-2023'
        },
        'NL': {
            'nl': 'https://nl.wikipedia.org/w/index.php?title=Special:CreateAccount&campaign=typage-nwe-nl-2023',
            'en': 'https://en.wikipedia.org/w/index.php?title=Special:CreateAccount&campaign=typage-nwe-nl-2023'
        },
        // East, Southeast Asia, and Pacific
        'JP': {
            'ja': 'https://ja.wikipedia.org/w/index.php?title=Special:CreateAccount&campaign=typage-eseap-ja-2023',
            'en': 'https://en.wikipedia.org/w/index.php?title=Special:CreateAccount&campaign=typage-eseap-ja-2023'
        },

    };

    // Separate list so we can have others in prep for proofing
    var editingCTACountries = [ 'IN', 'ZA', 'BR', 'AR', 'CL', 'CO', 'MX', 'PE', 'UY',
    							'SE', 'IT', 'FR', 'NL', 'JP' ];

    if ( editingCTACountries.indexOf(country) !== -1 || params.testEditingCTA ) {
        if ( editingUrls[country] ) {
            if ( editingUrls[country][pageLanguage] ) {
                $( '.try-editing a' ).attr( 'href', editingUrls[country][pageLanguage] );
                $( '.primary-ctas .survey' ).hide();
                $( '.primary-ctas .try-editing, .other-ctas, .other-ctas .survey' ).show();
            }
        }
    }

    // Only show survey 25% of time on English page
    //if ( pageLanguage === 'en' ) {
    //    if ( Math.random() > 0.25 ) {
    //        $('.survey').hide();
    //    }
    //}
    if ( pageLanguage === 'en' ) {
        var surveyNumber = Math.random();
        if ( surveyNumber > 0.5 ) {
        	$('.survey').hide();
        } else if ( surveyNumber > 0.25 && surveyNumber <= 0.5 ) {
        	$('.survey-1').hide();
        } else if ( surveyNumber > 0 && surveyNumber <= 0.25 ) {
        	$('.survey-2').hide();
        }
    }
    
    // Don't show editing CTAs on apps: https://phabricator.wikimedia.org/T353023
    if ( params.utm_medium === 'WikipediaApp' || params.wmf_medium === 'WikipediaApp' ) {
    	$('.other-ctas .edit-6c, .primary-ctas .try-editing').hide();
    }
    
    // Finally, if there are no secondary CTAs shown then hide the section header
    if ( $('.other-ctas .focus-block:visible').length === 0 ) {
    	$('.other-ctas h2').hide();
    }

});

if ( navigator.share ) {
    $('#ty-web-share').click(function(e) {
        var url = 'https://donate.wikimedia.org/?utm_medium=SocialMedia&utm_campaign=ThankYouPage&utm_source=WebShare';
        if ( $(this).data('share-language') ) {
            url += '&uselang=' + $(this).data('share-language');
        }
        navigator.share({
            text: $(this).data('share-text'),
            url: url
        });
        e.preventDefault();
    });
    $('#ty-web-share').show();
}

/* Show all elements for testing and checking translations */
function tyDebug() {
    $('.ty-page *').show();
}