Jump to content

User:X!/vermont.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.
function formatResponse(response) { //This bit was stolen from Gracenotes. Thanks, Gracenotes!
	try {
		response = response.query.pages;
		for (var property in response)
			return response[property];
	} catch (e) {
	}
}
 
function zeroPad(str) {
 return ("0" + str).slice(-2);
}
 
function tagArticle(classType, importance) {
 var req = new sajax_init_object();
 var page = mw.config.get('wgPageName');
  req.open("GET", mw.config.get('wgScriptPath') + "/api.php?action=query&prop=info|revisions&format=json&intoken=edit&rvprop=content|timestamp&titles=" + encodeURIComponent(page), false);
  req.send(null);
  var info = formatResponse(eval("(" + req.responseText + ")"));
  var date = new Date();
  var startTime = date.getUTCFullYear() + zeroPad(date.getUTCMonth() + 1) + zeroPad(date.getUTCDate()) + zeroPad(date.getUTCHours()) + zeroPad(date.getUTCMinutes()) + zeroPad(date.getUTCSeconds());
  var editTime = (info.revisions[0] ? info.revisions[0].timestamp.replace(/[^0-9]/g, "") : startTime); 
  var content = (info.revisions[0]["*"] ? info.revisions[0]["*"] : "");
  var editToken = info.edittoken;
   content = content.replace(/\{\{(Project Vermont|WPVT|WPVERMONT|WikiProject Vermont).*?\}\}\n/i, ""); //A *very* cheap hack.  Works nonetheless.
 
    var postdata = "wpTextbox1=" + encodeURIComponent("\{\{Project Vermont\|class=" + classType + "\|importance=" + importance + "\}\}\n" + content)
                 + "&wpSummary=" + encodeURIComponent("Assessing for WikiProject Vermont.")
                 + "&wpSave=save"
                 + "&wpEditToken=" + encodeURIComponent(editToken)
                 + "&wpEdittime=" + editTime
                 + "&wpStarttime=" + startTime;
    var req = sajax_init_object();
    req.open("POST", mw.config.get('wgServer') + mw.config.get('wgScript') + "?title=" + encodeURIComponent(page) + "&action=submit", false);
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    req.setRequestHeader("Content-Length", postdata.length);
    req.send(postdata);
  return;
}
 
function submitData() {
 document.getElementById("submitData").disabled =  true;
 document.getElementById("submitData").value = "Submitting...";
 tagArticle(document.getElementById("classType").value, document.getElementById("importanceType").value);
 document.getElementById("submitData").value = "Submitting... done!";
}
 
function buildTagForm() {
    jsMsg('<fieldset style="font-size:10pt">'
            + '<legend>'
                + 'WikiProject Vermont'
            + '</legend>'
            + '<table style="background-color:transparent;color:#000000;font-size:10pt"><br />'
                + '<tr><td>Class <small>(see <a href="http://en.wikipedia.org/wiki/Wikipedia:Version_1.0_Editorial_Team/Assessment#Quality_scale">here</a> for a list of available options)</small>: </td><td><input type="text" name="classType" id="classType" /></td></tr>'
                + '<tr><td>Importance <small>(see <a href="http://en.wikipedia.org/wiki/Wikipedia:Version_1.0_Editorial_Team/Assessment#Importance_scale">here</a> for a list of available options)</small>: </td><td><input type="text" name="importanceType" id="importanceType" /></td></tr>'
                + '<tr><td><input type="submit" id="submitData" onclick="submitData()" value="Submit" /></td></tr>'
        + '</table>'
    + '</fieldset>');
}
 
addOnloadHook(function() {
 if(mw.config.get('wgNamespaceNumber') == 1) mw.util.addPortletLink("p-cactions", "javascript:buildTagForm()", "vt", "ca-vt");
});