MediaWiki:Populate-category.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
// Tool to easily add the Populate category template to categories.
// <pre>
// Configuration

// Should the edits be saved automatically?
if(window.pc_autosave == false){}else if(window.pc_autosave){}else{ pc_autosave = true; }

// String constants
pc_text = "Populate category";
pc_tooltip = "Populate this category from a gallery";
pc_prompt = "Please enter a gallery hint or leave emtpy if the gallery name is the same:";

function pc_nomPopulateCategory() {
  var galleryhint = prompt(pc_prompt, '');
  if (galleryhint==null) return;
  var editlk = document.getElementById('ca-edit').getElementsByTagName('a')[0].href;
  document.location = editlk + '&fakeaction=pc_add&galleryhint=' + encodeURIComponent(galleryhint);
}

function pc_addPopulateCategoryTemplate(galleryhint) {
  if(galleryhint==''){
   var txt = '{{Populate category}}';
  } else {
   var txt = '{{Populate category|gallery=' + galleryhint + '}}'; 
  }
  document.editform.wpTextbox1.value = txt + document.editform.wpTextbox1.value;
  document.editform.wpSummary.value = 'Adding ' + txt + ' using [[MediaWiki:Populate-category.js|Populate-category.js]]';
  if (nfd_autosave) document.editform.wpSave.click();
}

function pc_onload() {
  if (mw.config.get('wgNamespaceNumber') == 14) { //NS_CATEGORY
    addLink('p-tb', 'javascript:pc_nomPopulateCategory()', pc_text, 'pop-cat', pc_tooltip);
  }
  var fakeaction = getParamValue('fakeaction');
  if (fakeaction == 'pc_add')
    pc_addPopulateCategoryTemplate(decodeURIComponent(getParamValue('galleryhint')));
}

$(document).ready(pc_onload);
// </pre>