Benutzer:Deus Figendi/HotCat simcat extension.js
Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.
- Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
- Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
- Internet Explorer/Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
- Opera: Strg+F5
function strip_namespace (s) {
//This function trims the Namespace-Prefix off a string.
//Returns: Boolean
//Called in: ajax_catlist_return()
return(s.substr(s.indexOf(':')+1));
}
function is_in_array(element2search,array2search) {
// This function checks if the element2search is in the array2search
// used to remove the categories allready in the article from the
// list of similar categories.
// Returns:Boolean
// Might be replaced by a function of jQuery or other framework.
//This function is called by ajax_catlist_return()
var did_find = false;
for(var i=0; i<array2search.length; i++) {
if (array2search[i] == element2search) did_find = true;
}
return did_find;
}
//CHANGE ME
//Can be removed if the API always returns sorted results.
function countsort_callback(a,b) {
// A callback-function to sort arrays of objects by the property
// "count" (highest to lowest)
//Returns:integer (signed)
//This function is called by ajax_catlist_return()
return b.count-a.count;
}
function add_simcat_a(cat_name,count_value,li_caption,hidden) {
//Adds an a- and an br-element to the list of similar categories
//Returns nothing
//This function is called by ajax_catlist_return()
var new_simcat_a = document.createElement('a');
new_simcat_a.title="auswählen";
new_simcat_a.href="javascript:hotcat_edit('vorschlagclick',new Array('createcatname','"+cat_name+"'));"
var new_simcat_count_label = document.createElement('span');
new_simcat_count_label.style.verticalAlign = 'text-top';
new_simcat_count_label.style.fontSize = 'xx-small';
new_simcat_count_label.appendChild(document.createTextNode(' ('+count_value+')'));
new_simcat_a.appendChild(document.createTextNode('↴ '+cat_name));
new_simcat_a.appendChild(new_simcat_count_label);
var new_simcat_br = document.createElement('br');
document.getElementById('vorschlaegecreatecatname').appendChild(new_simcat_br);
document.getElementById('vorschlaegecreatecatname').appendChild(new_simcat_a);
}
function ajax_catlist_return(request_object) {
// Callback-function for a request to get the similar categories
// of a list of given categories.
// Called by request_simcats
//First searches the object for categories and add them to a
// simcat_array (its an object, I just called it an array).
// While this it does not add categories given to the page
// or baned by the user.
//
//The simcat_array-object is an array of objects. This way (json):
// simcat_array:[
// { count:2 , catname:"catname1" , id:11},
// { count:25 , catname:"catname2" , id:22},
// { count:6 , catname:"catname3" , id:44}
// ]
//
//Second sorts that array high2low by "count"
//
//Third step is to clear the simcat_list
//
//Fourth the function adds the twenty items with the highest count.
if (request_object.readyState == 4) {
var my_responseObject = request_object.responseXML;
//CHANGE ME ===================================================
//This is just for testing... 'cause I'll never get the correct
//response ^^ just remove it.
my_responseObject = '<?xml version="1.0"?>';
my_responseObject += "\n"+'<api>';
my_responseObject += "\n"+' <query>';
my_responseObject += "\n"+' <simcats>';
my_responseObject += "\n"+' <cl ns="14" title="Kategorie:Jahre" count="123" pageid="100"/>';
my_responseObject += "\n"+' <cl ns="14" title="Kategorie:Jahre der Galaktischen Republik" count="120" pageid="200"/>';
my_responseObject += "\n"+' <cl ns="14" title="Kategorie:B-Serie Kampfdroiden" count="111" pageid="300"/>';
my_responseObject += "\n"+' <cl ns="14" title="Kategorie:Jahre der Galaktischen Föderation freier Allianzen" count="102" pageid="400"/>';
my_responseObject += "\n"+' <cl ns="14" title="Kategorie:GR-75 Transporter" count="96" pageid="600"/>';
my_responseObject += "\n"+' <cl ns="14" title="Kategorie:Raumschiffe" count="86" pageid="800"/>';
my_responseObject += "\n"+' <cl ns="14" title="Kategorie:Raumschiffe der Rebellen-Allianz" count="84" pageid="1100"/>';
my_responseObject += "\n"+' <cl ns="14" title="Kategorie:Khormai" count="72" pageid="1200"/>';
my_responseObject += "\n"+' <cl ns="14" title="Kategorie:Personen" count="70" pageid="1400"/>';
my_responseObject += "\n"+' <cl ns="14" title="Kategorie:Baustellen" count="66" pageid="1500"/>';
my_responseObject += "\n"+' <cl ns="14" title="Kategorie:Hybriden" count="62" pageid="1600"/>';
my_responseObject += "\n"+' <cl ns="14" title="Kategorie:Kiffar" count="57" pageid="1700"/>';
my_responseObject += "\n"+' <cl ns="14" title="Kategorie:Menschen" count="51" pageid="1800"/>';
my_responseObject += "\n"+' <cl ns="14" title="Kategorie:Personen" count="47" pageid="1900"/>';
my_responseObject += "\n"+' <cl ns="14" title="Kategorie:Unvollständige Artikel" count="41" pageid="1834"/> ';
my_responseObject += "\n"+' <cl ns="14" title="Kategorie:Planeten" count="37" pageid="1737"/>';
my_responseObject += "\n"+' <cl ns="14" title="Kategorie:Planeten des Korporationssektors" count="33" pageid="1762"/>';
my_responseObject += "\n"+' <cl ns="14" title="Kategorie:Planeten des Äußeren Randes" count="22" pageid="1234"/>';
my_responseObject += "\n"+' <cl ns="14" title="Kategorie:Planeten" count="21" pageid="1463"/>';
my_responseObject += "\n"+' <cl ns="14" title="Kategorie:Planeten des Sanbra-Sektors" count="20000" pageid="1122"/>';
my_responseObject += "\n"+' </simcats>';
my_responseObject += "\n"+' </query>';
my_responseObject += "\n"+'</api>';
my_responseObject = new DOMParser().parseFromString(my_responseObject, "text/xml");
//Remove 'til here ! ===================================================
var simcat_array = new Array();
var cats_not2list = getGivenCategories();
for (var i = 0; i < my_responseObject.getElementsByTagName('cl').length && i < 20; i++) {
if (!is_in_array(strip_namespace(my_responseObject.getElementsByTagName('cl')[i].getAttribute('title')),cats_not2list)) {
simcat_array[i] = new Object();
simcat_array[i].count = my_responseObject.getElementsByTagName('cl')[i].getAttribute('count');
simcat_array[i].catname = strip_namespace(my_responseObject.getElementsByTagName('cl')[i].getAttribute('title'));
simcat_array[i].id = my_responseObject.getElementsByTagName('cl')[i].getAttribute('pageid');
}
}
//CHANGE ME:
//Sorting can be dropped if the API always returns sorted results
simcat_array.sort(countsort_callback);
//Clear the list:
var simcat_list = document.getElementById('vorschlaegecreatecatname');
while (simcat_list.getElementsByTagName("a").length > 0) {
simcat_list.removeChild(simcat_list.getElementsByTagName("a")[0]);
}
while (simcat_list.getElementsByTagName("br").length > 0) {
simcat_list.removeChild(simcat_list.getElementsByTagName("br")[0]);
}
//Fill the box:
for (var i = 0; i < 20 && i < simcat_array.length; i++) {
add_simcat_a(simcat_array[i].catname,
simcat_array[i].count,
simcat_array[i].catname+' ('+simcat_array[i].count+')',
Math.round(i/20)
);
}
}
}
function getGivenCategories() {
//This function is to get alle categories the article belongs to
//Returns: array of strings
//Might be replaced by a global variable or existing function...
//This function is called by ajax_catlist_return and request_simcats
var return_array = new Array();
var cat_div = document.getElementById("mw-normal-catlinks");
var cat_spans = cat_div.getElementsByTagName("span");
var this_href = "";
for (var i = 0; i < cat_spans.length; i++) {
if (cat_spans[i].getElementsByTagName("a").length > 0) {
return_array.push(cat_spans[i].getElementsByTagName("a")[0].firstChild.data);
}
}
return return_array;
}
function request_simcats(query_type) {
//This function creates an API-request for "similar categories"
//and sends it.
//called by add_simcatlist (or events of the input-box for categories)
if(document.getElementById('createcatname').value.length == 0) {
//CHANGE ME!!!
// getGivenCategories() tells a list of categories the article belongs
// to. Might be replaces by a global variable or an existing function.
var given_category_list = getGivenCategories();
// create a query of these categories...
var api_query = "action=query&list=simcats";
if (query_type == 'inline') {
//The given cats are noted in a list and requested by GET
api_query+= "&sclist="+encodeURIComponent(given_category_list.join("|"));
} else if (query_type == 'array') {
//The given cats are noted in an array and requested by POST
for (var i = 0; i < given_category_list.length; i++) {
api_query+= "&scarray[]="+encodeURIComponent(given_category_list[i]);
}
}
api_query+= "&sclimit=20&format=xml";
//Create and send the request
var ajax_pagerequest = new XMLHttpRequest();
if (query_type == 'inline') {
ajax_pagerequest.open('GET', "http://www.jedipedia.net/w/api.php", true);
} else {
ajax_pagerequest.open('POST', "http://www.jedipedia.net/w/api.php", true);
}
ajax_pagerequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
ajax_pagerequest.onreadystatechange = function() { ajax_catlist_return(this); }
ajax_pagerequest.send(api_query);
}
}
function add_simcatlist() {
//This function does not do anything ^^
//these events can also be added otherway.
document.getElementById('createcatname').addEventListener("focus", function(e) { request_simcats('inline'); } ,false);
document.getElementById('createcatname').addEventListener("keyup", function(e) { request_simcats('inline'); } ,false);
}
// CHANGE ME!!!
// this is where everything begins ^^
// A button is created to request similar categories.
// But add_simcatlist() might also be called otherways but it should be
// called only one time (kinda initiation).
//if (document.getElementById("catlinks")) {
var hce_link = document.createElement("input");
hce_link.type = "button";
hce_link.style.display = "block";
hce_link.value = "HC erweitern";
hce_link.style.position = "fixed";
hce_link.style.right = "1em";
hce_link.style.top = "1em";
hce_link.addEventListener("click", function(e) { add_simcatlist(); } ,false);
// document.getElementById("catlinks").parentNode.insertBefore(hce_link,document.getElementById("catlinks"));
document.getElementsByTagName("body")[0].appendChild(hce_link);
//}