User:Gryllida/js/pasteWithQuotes.js

From Wikinews, the free news source you can write!
Jump to navigation Jump to search

Note: After saving, you may have to bypass your browser's cache to see the changes. Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac); IE: hold Ctrl while clicking Refresh, or press Ctrl-F5; Konqueror: simply click the Reload button, or press F5; Opera users may need to completely clear their cache in Tools→Preferences. — More skins

mw.loader.using(['mediawiki.api'], function () {
	"use strict";
	function insertTextAtCursor(text) {
	    var sel, range, html;
	    if (window.getSelection) {
	        sel = window.getSelection();
	        if (sel.getRangeAt && sel.rangeCount) {
	            range = sel.getRangeAt(0);
	            range.deleteContents();
	            range.insertNode( document.createTextNode(text) );
	        }
	    } else if (document.selection && document.selection.createRange) {
	        document.selection.createRange().text = text;
	    }
	}
	
	$('.CodeMirror-line').each(function(){ 
		this.addEventListener('paste',function (e) { 
			var clipboardData, pastedData;
			e.stopPropagation();  
			e.preventDefault();
	    	clipboardData = e.clipboardData || window.clipboardData;
	    	pastedData = clipboardData.getData('Text');
			pastedData = '"' + pastedData + '"';
			insertTextAtCursor(pastedData);
		});
	});
});