User:Kiteretsu/diff-permalink.js

From Test Wiki
Revision as of 14:17, 8 April 2024 by Kiteretsu (talk | contribs) (+)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.
// <nowiki>
$.when( $.ready, mw.loader.using( [ "mediawiki.util" ] ) ).then( function () {
    var suffix = mw.config.get( "wgDiffNewId" );
    var page;
    if( suffix ) {
        if( document.getElementsByClassName( "diff-multi" ).length ||
            mw.config.get("wgCanonicalSpecialPageName") === "ComparePages" ) {
            suffix = mw.config.get( "wgDiffOldId" ) + "/" + suffix;
        }
        page = "Special:Diff/" + suffix;
    } else {
        // If "oldid" is present in the URL, show an appropriate rev id there as well.
        if( mw.util.getParamValue( "oldid" ) ) {
            page = "Special:Permalink/" + mw.config.get( "wgRevisionId" );
        } else return; // nothing to do here
    }

    var permalinkEl = $( "<div>" ).append(
        "Permalink to this " + ( suffix ? "diff" : "oldid" ) + ": ",
        $( "<input>" )
            .attr( { "id": "diff-permalink-link" } )
            .val( page ),
        $( "<button>" )
            .text( "Copy" )
            .css( { "padding": "0.5em", "cursor": "pointer", "margin-left": "0.5em" } )
            .click( function () {
                document.getElementById( "diff-permalink-link" ).select();
                document.execCommand( "copy" );
            } ) );

    $( "#diff-permalink-link" ).attr( "size", page.length ); // resize to diff length

    if( suffix ) {
        $( "#bodyContent" ).prepend( permalinkEl );
    } else {
        $( "#contentSub" ).after( permalinkEl );
    }
} );
// </nowiki>