User:Billinghurst/Gadget-Fill Index.js

/* Adapted for enWS, updated the dict (terms from MediaWiki:Proofreadpage_index_template) and Index ns number * Author: w:fr:Phe https://fr.wikisource.org/wiki/MediaWiki:Gadget-Fill_Index.js * * Remplit les champs lors de la création d'un Livre: à partir des informations * contenues dans le modèle Book de Commons */ var fill_index = { ws_msg : function (name) { var m = self.ws_messages[name]; return m ? m : name; },   create_script_obj : function (url) { var scriptObj = document.createElement("script"); scriptObj.setAttribute("type", "text/javascript"); scriptObj.setAttribute("src", url); document.body.appendChild(scriptObj); },   extract_dict : { 'Editor'     : 'editor', 'Publisher'  : 'publisher', 'Author'     : 'author', 'Translator' : 'translator', 'Volume'     : 'volume', // FIXME: these fields must use ws_msg to auto-translate the field name // but actually these translation are not translated. 'Illustrator' : 'Illustrator', 'Image page' : 'Image', 'Title'      : 'Title', 'Date'       : 'Year', 'City'       : 'Address', 'Source'     : 'Source' },

setup_extract_dict : function { for (var idx in this.extract_dict) { this.extract_dict[idx] = this.ws_msg(this.extract_dict[idx]) }   },

extract_field : function (name, contents) { var regex = new RegExp('\\|[ ]*' + name + '[ ]*=[ ]*([^\n]*)'); var match = regex.exec(contents); if (match) { return { 'field name' : this.extract_dict[name], 'value' : match[1] }; }       return null; },   extract_content : function (data) { if (!data.query.pages["-1"]) { for (var ids in data.query.pages) { var content = data.query.pages[ids].revisions[0]['*']; for (var idx in this.extract_dict) { var dict = this.extract_field(idx, content); if (dict) { f = document.getElementsByName(dict['field name'])[0]; if (f) { var data = dict['value']; data = data.replace(//, "$1"); data = data.replace(//, "$1") f.value = data; }                   }                }                break; }           return true; } else { return false; }   },    common_content : function (data) { if (!this.extract_content(data)) { var url = mw.config.get('wgServer') + '/w/api.php' + "?format=json&callback=fill_index.extract_content" + "&action=query&prop=revisions&rvprop=content&titles=" + encodeURIComponent('Fichier:' + mw.config.get('wgTitle')); fill_index.create_script_obj(url); }   },    setup : function  { fill_index.setup_extract_dict; var wpTextbox1 = document.getElementById("wpTextbox1"); if (!wpTextbox1.value) { var url = '//commons.wikimedia.org/w/api.php' + "?format=json&callback=fill_index.common_content" + "&action=query&prop=revisions&rvprop=content&titles=" + encodeURIComponent('File:' + mw.config.get('wgTitle')); fill_index.create_script_obj(url); }   }, }; /* FIXME: don't use an hard-coded namespace number */ if (mw.config.get("wgNamespaceNumber") == 106 && mw.config.get("wgAction") == "edit") addOnloadHook(fill_index.setup);