MediaWiki:Gadget-ezcopy.js

From Old School Near-Reality Wiki
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.
 1 $(function () {
 2 	function try_copy(x, $el) {
 3 		var txt = document.createElement('textarea'), $txt = $(txt);
 4 		$txt.text(x).css({
 5 			position: 'fixed',
 6 			top: 0,
 7 			left: 0,
 8 			width: '2em',
 9 			heigh: '2em',
10 			padding: 0,
11 			border: 'none',
12 			outline: 'none',
13 			boxShadow: 'none',
14 			background: 'transparent'
15 		}).appendTo('body');
16 		txt.select();
17 		try {
18 			document.execCommand('copy');
19 			//mw.notify('Copied "'+x+'" to the clipboard', {tag: 'ezcopy'});
20 			$el.addClass('rsw-ezcopy-copied');
21 			setTimeout(function(){$el.removeClass('rsw-ezcopy-copied')}, 2500)
22 		} catch (err) {}
23 		$txt.remove();
24 	}
25 	function init() {
26 		var $copy_fullpagename, $copy_pagename, $copy_displaytitle, $container,
27 			FULLPAGENAME, PAGENAME, DISPLAYTITLE;
28 		PAGENAME = mw.config.get('wgTitle');
29 		$container = $('<span>').addClass('rsw-ezcopy-container');
30 		$copy_pagename = $('<span>')
31 			.addClass('rsw-ezcopy-button rsw-ezcopy-pagename')
32 			.append('<span class="rsw-ezcopy-icon">','<span class="rsw-ezcopy-label">Page\nname</span>')
33 			.attr('title', 'Copy "'+PAGENAME+'"');
34 		
35 		$copy_pagename.click(function(){try_copy(PAGENAME, $copy_pagename);});
36 		$container.append($copy_pagename);
37 		FULLPAGENAME = PAGENAME;
38 		if (mw.config.get('wgCanonicalNamespace') !== '') {
39 			var ns = mw.config.get('wgFormattedNamespaces')[mw.config.get('wgNamespaceNumber')];
40 			if (ns !== undefined) {
41 				FULLPAGENAME = ns+':'+PAGENAME;
42 				$copy_fullpagename = $('<span>')
43 					.addClass('rsw-ezcopy-button rsw-ezcopy-fullpagename')
44 					.append('<span class="rsw-ezcopy-icon">','<span class="rsw-ezcopy-label">Full</span>')
45 					.attr('title', 'Copy "'+FULLPAGENAME+'"');
46 				$copy_fullpagename.click(function(){try_copy(FULLPAGENAME, $copy_fullpagename);});
47 				$container.append($copy_fullpagename);
48 			}
49 		}
50 		if (mw.config.get('wgAction') === 'view' && mw.config.get('wgDiffOldId') === null) { // wgDiffOldId is either an ID or false if in a diff, null if not a diff
51 			DISPLAYTITLE = $('#firstHeading').text();
52 			if (FULLPAGENAME !== DISPLAYTITLE) {
53 				$copy_displaytitle = $('<span>')
54 					.addClass('rsw-ezcopy-button rsw-ezcopy-displaytitle')
55 					.append('<span class="rsw-ezcopy-icon">','<span class="rsw-ezcopy-label">Display\ntitle</span>')
56 					.attr('title', 'Copy "'+DISPLAYTITLE+'"');
57 				$copy_displaytitle.click(function(){try_copy(DISPLAYTITLE, $copy_displaytitle)});
58 				$container.append($copy_displaytitle);
59 			}
60 		}
61 		$('#firstHeading').append($container);
62 	}
63 	init()
64 })