Difference between revisions of "User:Argent/common.js"

From The Coppermind
Jump to navigation Jump to search
(Created page with ";mw.hook('wikipage.editform').add(function () { const page_name = mw.config.get('wgPageName') if (!page_name.startsWith('Coppermind:Artists/')) { return; } const edit = docu...")
 
m
Line 1: Line 1:
  +
// 2019-03-03 autofill artist pages
 
;mw.hook('wikipage.editform').add(function () {
 
;mw.hook('wikipage.editform').add(function () {
   

Revision as of 15:12, 3 April 2019

// 2019-03-03 autofill artist pages
;mw.hook('wikipage.editform').add(function () {

const page_name = mw.config.get('wgPageName')
if (!page_name.startsWith('Coppermind:Artists/')) { return; }

const edit = document.querySelector('textarea')
// show pending progress 
mw.notify('scanning for new files linking to '+ page_name).then(function(note) {
// perform API query
;(new mw.Api()).get({
  action: "query",
  format: "json",
  prop: "linkshere",
  titles: page_name,
  lhnamespace: "6", // File: namespace only
}).then(function(resp) {
  const pid = Object.keys(resp.query.pages)[0]
  const new_files = []
  // hide progress notification
  note.close()
  // determine which new files link here
  if (!resp.query.pages[pid].linkshere) { mw.notify('no files link to '+ page_name); return; }
  const pages = resp.query.pages[pid].linkshere.map(function(page) { return page.title.split(':')[1] })
  for (var i in pages) {
    const page = pages[i]
    if (edit.value.includes(page)) { continue; }
    new_files.push(pages[i] + ' | ')
  }
  // don't change the page if there's nothing to change
  if (new_files.length == 0) { mw.notify('no new files link to '+ page_name); return; }
  mw.notify(new_files.length +' new files link to '+ page_name);
  if (edit.value.includes('</gallery>')) {
    // add the lines to the end of the current gallery
    edit.value = edit.value.replace('</gallery>', new_files.join("\n") +"\n</gallery>")
  } else {
    // create a gallery at the bottom of the page
    edit.value += "\n\n<gallery>\n"+ new_files.join("\n") +"\n</gallery>\n"
  }
}) // api.get.then
}) // note.close
});