Utilisateur:Phe/Works about.js
Note : après avoir enregistré vos modifications, il se peut que vous deviez forcer le rechargement complet du cache de votre navigateur pour voir les changements.
- Firefox / Safari : Maintenez la touche Maj (Shift) en cliquant sur le bouton Actualiser ou pressez Ctrl-F5 ou Ctrl-R (⌘-R sur un Mac) ;
- Google Chrome : Appuyez sur Ctrl-Maj-R (⌘-Shift-R sur un Mac) ;
- Internet Explorer : Maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5 ;
- Opera : Allez dans Menu → Settings (Opera → Préférences sur un Mac) et ensuite à Confidentialité & sécurité → Effacer les données d'exploration → Images et fichiers en cache.
// when acting on an Author: page try to find works about this author.
//
// An associative array of basename prefix associated with a two entry array,
// array[0] describe how to build a page title, array[1] describe how to build
// a link to such page.
// Parameters substitution is done this way:
// %0 is the target base pagename
// %A: lastname, firstname
// %B: firstname lastname
// %1: the title matching the subpage name
// Don't add more than 50 entry.
//
// Only one request for the full set of name is done, adding more entry doesn't
// require more request.
//
// Only article following the base_pagename/article_name convention can
// go in this array, other special case, like DBN00, are hard-coded.
var searched_pages = {
"1911 Encyclopædia Britannica" : [ "%A", "{" + "{EB1911 Link|%1}}" ],
"The Encyclopedia Americana (1920)" : [ "%A", "[[%0/%1|%1]]" ],
"The New International Encyclopædia" : [ "%A", "[[%0/%1|%1]]" ],
"A Short Biographical Dictionary of English Literature" : [ "%B", "{" + "{SBDEL link|%1}}" ],
"Collier's New Encyclopedia (1921)" : [ "%A", "{" + "{Collier's Link|%1}}" ],
"The New Student's Reference Work" : [ "%A", "{" + "{NSRW link|%1}}" ],
"Catholic Encyclopedia (1913)" : [ "%B", "{" + "{CE link|%1}}" ],
}
function works_about_cb(data)
{
var wpTextbox1 = document.getElementById("wpTextbox1");
if (!wpTextbox1)
return;
var result = '';
for (var page in data.query.pages) {
if (Number(page) > 0) {
var title = data.query.pages[page].title;
title = title.split('/', 2);
if (searched_pages[title[0]]) {
var link = searched_pages[title[0]][1];
link = link.replace(/%0/g, title[0]);
link = link.replace(/%1/g, title[1]);
result += '* ' + link + '\n';
} else {
var pos = title[0].search(' \\(DNB00\\)$');
if (pos != -1) {
result += '* {' + '{DNB link|' + title[0].slice(0, pos) + '}}\n';
} else if (wgUserName == 'Phe' || wgUserName == 'WikiSysop') {
// testbed
alert('unknown title type: ' + title[0]);
}
}
}
}
// Add the results at start of the text, placing it at end is prone error
if (result.length) {
var lastname = get_last_name(split_title());
wpTextbox1.value = '==Works about ' + lastname + '==\n' + result + wpTextbox1.value;
}
}
function create_script_obj(url)
{
var scriptObj = document.createElement("script");
scriptObj.setAttribute("type", "text/javascript");
scriptObj.setAttribute("src", url);
document.body.appendChild(scriptObj);
}
function format_pagename(firstname, lastname, key)
{
var result = key + '/';
if (searched_pages[key][0] == '%A')
result += lastname + ', ' + firstname;
else if (searched_pages[key][0] == '%B')
result += firstname + ' ' + lastname;
return encodeURIComponent(result);
}
function works_about()
{
if (wgNamespaceNumber != 102)
return;
// Doesn't work as expected, is importScript asynchronous?
//if (typeof(window['split_title']) == 'undefined')
// importScript('User:WikiSysop/Author fill.js');
var words = split_title();
var lastname = get_last_name(words);
var firstname = get_first_name(words);
var author_title = lastname + ', ' + firstname;
if (wgUserName != 'WikiSysop')
var url = wgServer + wgScriptPath;
else
var url = 'http://en.wikisource.org' + wgScriptPath; // testbed
url += "/api.php?format=json&callback=works_about_cb&action=query&prop=info&titles=";
var items = []
for (var srch in searched_pages)
items.push(format_pagename(firstname, lastname, srch));
items.push(encodeURIComponent(author_title + ' (DNB00)'));
url += items.join('|');
create_script_obj(url);
}
function add_works_about_button(){
var toolbar = document.getElementById("toolbar");
if(toolbar /*&& wgNamespaceNumber==0*/){
var image = document.createElement("img");
image.width = 23;
image.height = 22;
image.border = 0;
image.className = "mw-toolbar-editbutton";
image.style.cursor = "pointer";
image.alt = "regexp";
image.title = "Find works about an author";
image.src = "http://upload.wikimedia.org/wikipedia/commons/c/c4/Button_ref.png";
image.onclick = works_about;
toolbar.appendChild(image);
}
}
$(document).ready(add_works_about_button)