MediaWiki:Gadget-Bluelinker.js

From the AARoads Wiki: Read about the road before you go
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.
if ( mw.config.get( 'wgAction' ) === 'edit' || mw.config.get( 'wgAction' ) === 'submit' ) {
$( function () {
	const box = document.createElement("div");
	box.id = 'bluelinkerBox';
	
	const buttonContainer = document.createElement("div");
	box.appendChild(buttonContainer);
	
	const bluelinkButton = document.createElement("button");
	bluelinkButton.type = 'button';
	bluelinkButton.innerText = "Bluelinker";
	buttonContainer.appendChild(bluelinkButton);

	$(bluelinkButton).click ( function (e) {
		e.preventDefault();
		$(box).children("form").remove();
		var data = $("#wpTextbox1").val();

		data = data.replace(/\*?{{Attached\W?KML[^}}]*}}/, '')
			.replace(/\*?{{Short description[^}}]*}}/, '')
			.replace(/\*?{{Use American English[^}}]*}}/, '')
			.replace(/\*?{{Use British English[^}}]*}}/, '')
			.replace(/\*?{{Use Canadian English[^}}]*}}/, '')
			.replace(/\*?{{Use mdy dates[^}}]*}}/, '')
			.replace(/\*?{{Use dmy dates[^}}]*}}/, '')
			.replace(/\*?{{EngvarB[^}}]*}}/, '')
			.replace(/\*?{{Good article[^}}]*}}/, '')
			.replace(/\*?{{Featured article[^}}]*}}/, '')
			.replace(/\*?{{Portal[^}}]*}}/, '')
			.replace(/\*?{{Authority control[^}}]*}}/, '')
			.replace(/\*?{{Open access[^}}]*}}/, '')
			.replace(/\*?{{Free access[^}}]*}}/, '')
			.replace(/\*?{{\w+\-\w+\-stub[^}}]*}}/, '');
		
		var prelinked = /\[\[w:/.test(data);
		
		var excludedNamespacesRegex = /(\[\[(?!w?:?File:)(?!w?:?Image:)(?!w?:?Commons:)(?!w?:?Category:)(?!w?:?meta:)(?!w?:?m:)(?!w?:?mw:)(?!w?:?Help:)(?!w?:?H:)(?!w?:?phab:)(?!w?:?MediaWiki:)(?!w?:?Special:)(?!w?:?Template:)(?!w?:?Module:)(?!w?:?AARoads:)(?!w?:?AA:)(?!#)[^\]]*\]\])/i;
		var list = data.split(excludedNamespacesRegex);

		const selectorForm = document.createElement("form");
		const formTable = $(document.createElement("table"))
			.css("position", "relative")
			.append(
				$(document.createElement("thead"))
				.append(
					$(document.createElement("tr"))
					.append(
						$(document.createElement("th"))
						.css({
							"position": "sticky",
							"top": 0,
							"background": "white"
						})
						.text("Article")
					).append(
						$(document.createElement("th"))
						.css({
							"position": "sticky",
							"top": 0,
							"background": "white"
						})
						.text("AARoads")
					).append(
						$(document.createElement("th"))
						.css({
							"position": "sticky",
							"top": 0,
							"background": "white"
						})
						.text("Wiki")
					)
				)
			)[0];
		selectorForm.appendChild(formTable);

		const formBody = document.createElement("tbody");
		formTable.appendChild(formBody);
		
		var articles = {}; //Takes form of matrix, where first column is id of article name, and second is location along the textarea

		for (var i = 0; i<list.length; i++) {
			if (excludedNamespacesRegex.test(list[i])) {
				var regex = /(?<=\[\[)[^\|]*(?=\||\]\])/.exec(list[i])[0];
				var selected = prelinked && !regex.startsWith('w:');
				regex = regex.replace('w:','');
				var id = regex.replaceAll(' ', '_').replaceAll("'", "%27").toLowerCase();
				if (articles[id])
					articles[id].push(i);
				else {
					/**var label = $(document.createElement("input"))
						.attr({
							"id": "label",
							"type": "text",
							"readonly": "true",
							"value": regex
						});
					label.on("click", function () { 
						console.log("something"); 
					});**/ //return to this 
					articles[id] = [i];
					$(formBody).append(
						$(document.createElement("tr"))
							.css("background", formBody.childNodes.length%2==0 ? "lightgray" : "")
							.append(
								$(document.createElement("td"))
								.text(regex) //append(label);
							)
							.append(
								$(document.createElement("td"))
								.css("text-align", "center")
								.append(
									$(document.createElement('input'))
									.attr({
										"type": "radio",
										"id": "radioRoad"+id,
										"name": "radio"+id,
										"value": 'false',
										"checked": selected ? "" : null
									})
								)
							)
							.append(
								$(document.createElement("td"))
								.css("text-align", "center")
								.append(
									$(document.createElement('input'))
									.attr({
										"type": "radio",
										"id": 'radioWiki'+id,
										"name": "radio"+id,
										"value": 'true',
										"checked": !selected ? "" : null
									})
								)
							)
					);
				}
			}
		}

		selectorForm.innerHTML += "<input type='submit'>";

		selectorForm.addEventListener('submit', function (e) {
			e.preventDefault();
			const selectorFormData = new FormData(selectorForm);
			var output = "";

			for (var id in articles) {
				for (var i = 0; i<articles[id].length; i++) {
					var newOut = list[articles[id][i]];
					if (selectorFormData.get("radio"+id)=="true") {
						if (newOut.indexOf('|')==-1)
							newOut = newOut.replace("]]", "|"+/(?<=\[\[).*(?=\||\]\])/.exec(newOut)[0]+"]]");
						if (newOut.substring(0, 4)!="[[w:")
							newOut = newOut.replace("[[", "[[w:");
					}
					else newOut = newOut.replace("[[w:", "[[");
					list[articles[id][i]] = newOut;
				}
			}
			
			$("#wpTextbox1").val(list.join(''));
			if (!$('#wpSummary').val().toLowerCase().includes('bluelink'))
				$('#wpSummary').val($('#wpSummary').val()+'Bluelinked');
			$([document.documentElement, document.body]).animate({
		        scrollTop: $("#wpTextbox1").offset().top
		    }, 0);
			
		});

		buttonContainer.after(selectorForm);
	});
	
	const singleBluelinkButton = document.createElement("button");
	singleBluelinkButton.type = 'button';
	singleBluelinkButton.innerText = "Link Selected Text to Wikipedia";
	buttonContainer.appendChild(singleBluelinkButton);
	
	$(singleBluelinkButton).click ( function (e) {
		const textarea = $("#wpTextbox1");
		var selected = textarea.textSelection('getSelection');
		if (selected.substr(0,2)=="[[" && selected.substr(selected.length-2,selected.length)=="]]")
			selected = selected.substr(2, selected.length-2);
		textarea.textSelection( 'encapsulateSelection', {
			replace: true,
			peri: "[[w:" + selected + "|" + selected + "]]"
		} );
	});
	
	$( '.editOptions' ).before( box );
});}