User:Yintan/common.js

// This attempts to automatically fill out the shared IP form for you. // The API we're using (ipinfo) has a ratelimit of 1k per day. // I don't have the time or the patience to actually reach that limit, so if you somehow manage to, let me know if it doesn't fail gracefully. sharedcallback = function friendlysharedCallback { var Window = new Morebits.simpleWindow( 600, 420 ); Window.setTitle( "Shared IP address tagging" ); Window.setScriptName( "Twinkle" ); Window.addFooterLink( "Twinkle help", "WP:TW/DOC#shared" );

var form = new Morebits.quickForm( Twinkle.shared.callback.evaluate );

var div = form.append( {			type: 'div',			id: 'sharedip-templatelist',			className: 'morebits-scrollbox'		}	); div.append( { type: 'header', label: 'Shared IP address templates' } ); div.append( { type: 'radio', name: 'shared', list: Twinkle.shared.standardList,		event: function( e ) {			Twinkle.shared.callback.change_shared( e );			e.stopPropagation;		}	} );

var whois = {host:,org:} // We assume the wgTitle is still an IP, because it has to be for the link to appear, and I'd like to stay sane. getIP = (function{console.log(mw.config.get('wgTitle'))		return $.get("https://ipinfo.io/" + mw.config.get('wgTitle'), function(response) { //console.log(response) if(response.hostname !== "No Hostname" // Why don't they return a more sane "nothing" instead? The world may never know.		  && typeof(response.hostname) !== "undefined") { whois['host'] = response.hostname } 	 if(typeof(response.org) !== "undefined" // This being undefined does happen. Rarely and only in odd cases, but it does.		   && response.org.search(' ') > 0) { // We're chopping off the AS number, but we want to make sure there's a name after it. var orgname = response.org.split(' ').splice(1).join(' ') whois['org'] = orgname } 		}, "jsonp")}) $.when(getIP).done(function(ipreturn){		//console.log(ipreturn,whois)		var org = form.append( { type:'field', label:'Fill in other details (optional) and click \"Submit\"' } );		org.append( { type: 'input', name: 'organization', label: 'IP address owner/operator', disabled: true, value: whois['org'], tooltip: 'You can optionally enter the name of the organization that owns/operates the IP address. You can use wikimarkup if necessary.' }		);		org.append( { type: 'input', name: 'host', label: 'Host name (optional)', value: whois['host'], disabled: true, tooltip: 'The host name (for example, proxy.example.com) can be optionally entered here and will be linked by the template.' }		);		org.append( { type: 'input', name: 'contact', label: 'Contact information (only if requested)', disabled: true, tooltip: 'You can optionally enter some contact details for the organization. Use this parameter only if the organization has specifically requested that it be added. You can use wikimarkup if necessary.' }		);

form.append( { type:'submit' } );

var result = form.render; Window.setContent( result ); Window.display; }); };

function changeSharedIPCallback { // below copies all the functions from Twinkle's shared ip callback to mine Object.keys(Twinkle.shared.callback).forEach(function(key) {	 sharedcallback[key] = Twinkle.shared.callback[key];	}); Twinkle.shared.callback = sharedcallback; }

function waitForTwinkleSIP{ if(typeof(Twinkle) !== "undefined" && typeof(Twinkle.shared) !== "undefined") { clearInterval(window.waitForTwinkleSIP.interval) changeSharedIPCallback; } } window.waitForTwinkleSIP.interval = setInterval(waitForTwinkleSIP, 100)