User:MC10/stubtagtab.js: Difference between revisions

Content deleted Content added
fix index
only add stub menu if not added already
 
(35 intermediate revisions by the same user not shown)
Line 2:
// Enhancements made on requests by [[User:Alai]] and [[User:jj137]].
// ([[User:ais523/stubtagtab2.js]])
// Edited version [[User:MC10/stubtagtab.js]]
// <source lang="javascript">
 
$(function () {
(function ($) { // Wrap with anonymous function
if (mw.config.get("wgNamespaceNumber") === 0 && mw.config.get("wgAction") === "view") {
function stubTagTab() {
if (document.getElementById("ca-edit")) mw.util.addPortletLink('p-cactions', 'javascript:stubtagtab()', 'Stub', 'ca-stubtag', 'Add a stub tag to this page', '');
var $h, $hb, $newa,
// A list of subpages of WikiProject Stub sorting/Stub types/ that are relevant to this
a = ['General', 'Commerce', 'Culture', 'Education', 'Geography', 'Government, law, and politics',
'History', 'Leisure', 'Military and weaponry', 'Organizations', 'People',
'Religion, mythology, faiths, and beliefs', 'Science', 'Sports', 'Technology', 'Transport',
'Miscellaneous'];
$h = $("<div></div>", { "id": "stubtagtab" })
.css({
"color": "#000000",
"background-color": "#fff8f8"
});
for (var i = 0; i < a.length; i++) {
$newa = $("<a href=\"#\"></a>");
$newa.click(function (e) {
e.preventDefault();
stubTagMenu($(this).text());
});
$newa.text(a[i]);
$h.append($newa);
if ((i + 1) < a.length) {
$h.append(" &bull; ");
}
}
$("#contentSub").append($h);
 
$hb = $("<div></div>", {
"color": "#000000",
"background-color": "#fffff8"
});
$hb.attr("id", "stubtagmenu");
$("#contentSub").append($hb);
}
 
if (___location.href.indexOf("&autoaddstubtag=") !== -1 && document.getElementById('wpTextbox1') != null) {
function stubTagMenuRender(data) {
var x = decodeURIComponent(___location.href.split("&autoaddstubtag=")[1]);
ifvar $parseData = $(xdata.indexOf(parse.text["-stub*"]) === -1) x += "-stub";
$parseData.find("a").each(function () {
document.getElementById('wpTextbox1').value += "\n{" + "{" + x + "}}"; // add to the end of the article
var link = $(this).attr("href"),
document.getElementById('wpSummary').value = "Tagging with {" + "{" + x + "}} using [[WP:US/S|user scripts]]";
index = link.indexOf("Template:");
document.getElementById('wpPreview').click();
if (index === -1) {
$(this).attr({
"href": null,
"title": null
});
$(this).css({
"color": "#000000",
"text-decoration": "none"
});
} else {
$(this).attr("href", mw.config.get("wgServer") + mw.config.get("wgScript")
+ "?title=" + encodeURIComponent(mw.config.get("wgPageName"))
+ "&action=edit&autoaddstubtag=" + encodeURIComponent(link.slice(index + 9)));
}
});
$("#stubtagmenu").html($parseData);
}
});
 
function stubtagtabstubTagMenuFail() {
$("#stubtagmenu").html("Failed to load stub tags.");
var h;
// A list of subpages of WikiProject Stub sorting/Stub types/ that are relevant to this
var a = ['Commerce', 'Culture', 'Education', 'Geography', 'Government, law, and politics', 'History', 'Leisure', 'Military and weaponry', 'Miscellaneous', 'Organizations', 'People', 'Religion, mythology, faiths, and beliefs', 'Science', 'Sports', 'Technology', 'Transport'];
var i = 0;
h = "<div style='color:#000000; background-color:#fff8f8;'>&bull; ";
while (i < a.length) {
h += "<a href='javascript:stubtagmenu(\"" + a[i] + "\");'>" + a[i] + "</a> &bull; ";
i++;
}
h += "</div><div id='stubtagmenudiv' style='color: #000000; background-color: #fffff8;'></div>";
document.getElementById("contentSub").innerHTML = h;
}
 
function stubtagmenurenderstubTagMenu(datastubType) {
// Add loading message
/*
$("#stubtagmenu").html("Loading stub tags, please wait...");
var s = data.split('<text xml:space="preserve">')[1].split("</text>")[0];
s = s.split("&lt;").join("<").split("&gt;").join(">");
s = s.split("&quot;").join('"').split("&amp;").join('&');
s = s.split('<a href="/wiki/Template:');
var i = s.length;
while (--i) s[i] = s[i].split('" title').join('\');" title');
s = s.join('<a temphref="javascript:stubtagwith(\'');
s = s.split('<a href=').join('<a style="color:#000000;" notanhref=');
s = s.split('<a temphref=').join('<a href=');
document.getElementById('stubtagmenudiv').innerHTML = s;
*/
 
// Fetch the relevant subpage of the WikiProject
var parseData = $(data.parse.text["*"]);
$.ajax({
parseData.find("a").each(function () {
url: mw.config.get("wgServer") + mw.config.get("wgScriptPath") + '/api.php?action=parse&prop=text&text=' + encodeURIComponent('__NOTOC____NOEDITSECTION__\{\{Wikipedia:WikiProject Stub sorting/Stub types/' + stubType + '}}') + '&format=json',
var link = $(this).attr("href"),
indexdataType: = link.indexOf("Template:json"),
if (index === -1) {success: stubTagMenuRender,
$(this).attr("href",error: "");stubTagMenuFail
} else {);
}
$(this).attr("href", link.slice(index + 9));
 
$(function () {
if (mw.config.get("wgNamespaceNumber") === 0 && mw.config.get("wgAction") === "view"
&& $("#ca-edit").length !== 0) {
var portletLink = mw.util.addPortletLink('p-cactions', '#', 'Stub', 'ca-stubtag', 'Add a stub tag to this page', '');
$(portletLink).click(function (e) {
e.preventDefault();
if (!$("#stubtagtab").length) {
stubTagTab();
}
});
}
if (___location.href.indexOf("&autoaddstubtag=") !== -1 && $("#wpTextbox1").length !== 0) {
var x = decodeURIComponent(___location.href.split("&autoaddstubtag=")[1]);
if (x.indexOf("-stub") === -1) {
x += "-stub";
}
$("#wpTextbox1").val($("#wpTextbox1").val() + "\n{" + "{" + x + "}}"); // add to the end of the article
$("#wpSummary").val("Tagging with {" + "{" + x + "}} using [[User:MC10/stubtagtab.js|stubtagtab.js]]");
$("#wpPreview").trigger("click");
}
});
} (jQuery)); // End wrap with anonymous function
$('#stubtagmenudiv').html(parseData);
}
 
function stubtagmenu(x) {
// Fetch the relevant subpage of the WikiProject
$.ajax({
url: mw.config.get("wgServer") + mw.config.get("wgScriptPath") + '/api.php?action=parse&prop=text&text=' + encodeURIComponent('__NOTOC____NOEDITSECTION__{{Wikipedia:WikiProject Stub sorting/Stub types/' + x + '}}') + '&format=json',
dataType: "json", // "text"
success: stubtagmenurender
});
}
 
function stubtagwith(x) {
if (x == null || x === "") return;
___location.href = mw.config.get("wgServer") + mw.config.get("wgScript") + "?title=" + encodeURIComponent(mw.config.get("wgPageName")) + "&action=edit&autoaddstubtag=" + encodeURIComponent(x);
}
 
// </source> <!--[[Category:Wikipedia scripts]]-->