User:Proteins/addsandboxlink.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//<pre>
// Adds link to open the user's sandbox in a new tab
//
// To use this script, add "importScript('User:Proteins/addsandboxlink.js');" to your monobook.js subpage 
// under your user page, as you can see at User:Proteins/monobook.js

function addSandboxLink() { 

	var pt_userpage;
	var userpage_anchors;
	var userpage_href = "";
	var user_sandbox_href = "";

	pt_userpage = document.getElementById('pt-userpage');
	if (!pt_userpage) {
		window.alert("Unable to find pt-userpage.");
		return;
	}
//	window.alert("Found pt-userpage.");

	userpage_anchors = pt_userpage.getElementsByTagName("A");
	if (!userpage_anchors) { 
		window.alert("Unable to find userpage anchors.");
		return;
	}
//	window.alert("Found userpage anchors.");

	userpage_href = userpage_anchors[0].href;
	if (!userpage_href) {
		window.alert("Unable to find userpage_href.");
		return;
	}
//	window.alert("Found userpage_href: " + userpage_href);

	user_sandbox_href = userpage_href + "/Sandbox";
//	window.alert("Sandbox URL: " + user_sandbox_href);

	window.open(user_sandbox_href);
	window.alert("Opened user sandbox successfully\n\nURL: " + user_sandbox_href);
	return;

} // closes function addSandboxLink()

$(function () {
mw.util.addPortletLink('p-personal', 'javascript:addSandboxLink()', 'my sandbox', 'ca-sandbox', 'Open user sandbox for editing', '5', '');
});
 
//</pre>