Content deleted Content added
mNo edit summary |
maintenance: more info wikitext is array. startsWith is a string method. |
||
(28 intermediate revisions by 2 users not shown) | |||
Line 1:
// [[Category:Wikipedia scripts]]
// <nowiki>
if(typeof(window.us_InsertShortcuts) == 'undefined')
Line 4 ⟶ 5:
window.us_InsertShortcuts =
{
DEBUG:
inserts:
setup: function()
{
// Setup key handler.
window.addEventListener("
{
if(e.altKey && e.key.match(/\d/g))
{
if(!this.inserts[number - 1]) return;
this.insert(number - 1);
}
else if(e.ctrlKey && (e.key == "i" || e.key == "b"))
{
this.insert(e.key);
}
});
// Load user's settings.
var page =
var loadFrom = window.us_InsertShortcuts_loadFrom === undefined ? null : `https://${us_InsertShortcuts_loadFrom}.org/w/api.php`;
this.getWikitext(page, wikitext =>
▲ url : mw.util.getUrl(`User:${mw.config.get("wgUserName")}/InsertShortcutsSettings.js`) + "?action=raw", dataType : "text"
{
if(typeof wikitext === 'string' && wikitext.startsWith("!!REDIRECT"))
{
project = wikitext.split(" ")[1];
this.getWikitext(page, wikitext =>
{
// Remove escaped newlines. Escape 'comments'. Split into tags.
this.inserts = wikitext.
}, null, null, `https://${project}.org/w/api.php`);
}
else
{
// Remove escaped newlines. Escape 'comments'. Split into tags.
this.inserts = wikitext.replace(/\\\n/g, "").replace(/^\/\//gm, "").split("\n");
}
}, null, null, loadFrom);
},
insert: function(
{
const txtarea = document.getElementById("wpTextbox1");
Line 51 ⟶ 65:
}
var tag;
if(id == "i") tag = "''_''";
var result = this.inserts[number][0] + txtarea.value.substring(start, end) + this.inserts[number][1];▼
else if(id == "b") tag = "'''_'''";
else tag = this.inserts[id];
// Put selected text into the tag. Replace \n's with actual newlines.
▲ var result =
// Update txtarea.
txtarea.value = txtarea.value.substring(0, start) + result + txtarea.value.substr(end
txtarea.selectionStart = end;
txtarea.selectionEnd = end;
},
getWikitext: function(sTitles, fDone, fFail=null, fThen=null, href=null)
{
var settings =
{
"action": "query",
"prop": "revisions",
"rvprop": "content",
"rvslots": "main",
"rvlimit": 1,
"titles": sTitles
};
var api = href === null ? new mw.Api() : new mw.ForeignApi(href);
api.get(settings)
.done(data =>
{
var wikitext = [];
for(var id in data.query.pages)
{
var revisions = data.query.pages[id].revisions || [];
if(revisions.length) wikitext.push(revisions[0].slots.main['*']);
}
if(wikitext.length == 1)
{
fDone(wikitext[0]);
}
else
{
fDone(wikitext);
}
}).fail(fFail).then(fThen);
}
};
|