User:Xover/copySource.js

// ================================================================== // Copy wikitext source of the current page to the clipboard. // ==================================================================

// Make sure the necessary modules are loaded mw.loader.using('mediawiki.util', function {

// Wait for the page to be parsed (new-style $(document).ready) $(function {    // Special:-pages have no wiki-source    if (mw.config.get('wgCanonicalNamespace') === 'Special') {      return;    }

var copyPortlet = mw.util.addPortletLink(     'p-cactions', '#', 'Copy wikitext', 'ca-copywikitext', 'Copy the wikitext of this page to the clipboard'    ); $(copyPortlet).on('click', function (e) {     e.preventDefault;      doCopy;    }); }); // END: $(document).ready }); // END: mw.loader.using

function doCopy { var api = new mw.Api; var page = mw.config.get('wgArticleId'); var source = '';

api.get({   action: 'parse',    pageid: page,    prop:   'wikitext',    format: 'json'  }).then(function (data) {    source = data.parse.wikitext['*'];	navigator.clipboard.writeText(source).then(function  { mw.notify(			"Page wikisource copied to clipboard.",			{title: 'Page source copied', type: 'info', tag: 'copySource'}		); }); }); }