User:Guywan/Scripts/InsertShortcuts.js: Difference between revisions

Content deleted Content added
mNo edit summary
maintenance: more info wikitext is array. startsWith is a string method.
 
(18 intermediate revisions by 2 users not shown)
Line 12:
{
// Setup key handler.
window.addEventListener("keyupkeydown", e =>
{
if(e.altKey && e.key.match(/\d/g))
{
if(var number = e.key === "0" ? 10 : parseInt(e.key);
{
if(!this.insert(0inserts[number - 1]) return;
}
this.insert(number - 1);
else
{}
thiselse if(e.insert(parseIntctrlKey && (e.key) -== 1"i" || e.key == "b"));
}{
this.insert(e.key);
}
});
// Load user's settings.
var page = url : mw.util.getUrl(`User:${mw.config.get("wgUserName")}/InsertShortcutsSettings.js`) + "?action=raw", dataType : "text";
$.ajax(
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"))
.fail(result => { mw.notify("Failed to load your settings: " + result, "error"); })
{
.done(text =>
project = wikitext.split(" ")[1];
{
this.getWikitext(page, wikitext =>
this.inserts = text.split("\n");
{
// Remove escaped newlines. Escape 'comments'. Split into tags.
if(this.DEBUG) console.log("Finished loading user settings." + this.inserts);
this.inserts = wikitext.replace(/\\\n/g, "").replace(/^\/\//gm, "").split("\n");
});
{
}, null, null, `https://${project}.org/w/api.php`);
}
else
{
// Remove escaped newlines. Escape 'comments'. Split into tags.
this.inserts = textwikitext.replace(/\\\n/g, "").replace(/^\/\//gm, "").split("\n");
}
}, null, null, loadFrom);
},
insert: function(numberid)
{
const txtarea = document.getElementById("wpTextbox1");
Line 55 ⟶ 65:
}
var tag;
// Enclose selected text.
if(id == "i") tag = "''_''";
var result = this.inserts[number].replace("_", txtarea.value.substring(start, end));
else if(id == "b") tag = "'''_'''";
else tag = this.inserts[id];
// Put selected text into the tag. Replace \n's with actual newlines.
var result = this.inserts[number]tag.replace("_", txtarea.value.substring(start, end)).replace(/\\n/g, "\n");
// Update txtarea.
Line 62 ⟶ 77:
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);
if(this.DEBUG) console.log("Inserted.");
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);
}
};