User:Theleekycauldron/Scripts/PSHAW/dependencies/Hook.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Theleekycauldron/Scripts/PSHAW/dependencies/Hook. |
/*<nowiki>
Copyright (c) 2023 theleekycauldron
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
class Hook {
constructor(id,content,credits){
this.id = id;
this.content = content;
this.articles = []
for (let m of this.content.matchAll(/'''[^']*?\[\[([^\]\|]+)[^\]]*\]\][^']*?'''|\[\[.*'''([^']+)'''\]\]/g)){
this.articles.push(capitalize(m[1]))
}
this.credits = credits
}
morebits(){
let t = this.content
while (t.includes("\'\'\'")){
t = t.replace(/'''/,"<b>");
t = t.replace(/'''/,"</b>");
}
while (t.includes("\'\'")){
t = t.replace(/''/,"<i>");
t = t.replace(/''/,"</i>");
}
return "<b>"+this.id+"</b>: "+t.slice()
}
async subpage(){ //returns as Nomination object
if (this.credits.length>0){
let nomination = await Nomination.create(`Template:Did you know nominations/${this.credits[0].subpage}`);
return nomination;
}
return null
}
give_credit(credit){
let res;
console.log(credit)
if (credit.make){
res = `{{subst:Template:DYKmake/DYKmakecredit
|article=${credit.article}
|optional=
|nompage=Template:Did you know nominations/${credit.subpage}
|hook=${this.content}
}} ~~~~`
} else {
res = `{{subst:DYKnom/DYKnomcredit
|article=${credit.article}
|optional=
|nompage=Template:Did you know nominations/${credit.subpage}
}} ~~~~`
}
var params = {
action: 'edit',
title: `User talk:${credit.user}`,
summary: `Giving credit for ${credit.article}`+tag,
section: 'new',
sectiontitle: `DYK for ${credit.article}`,
text: res,
format: 'json'
}
return api.postWithToken( 'csrf', params );
}
tag_talk(article,file){
let res;
let today = new Date()
today = `${today.getUTCDate()} ${["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"][today.getUTCMonth()]} ${today.getUTCFullYear()}`
res = `{{DYK talk|${today}|entry=${this.content}|${file}}}`
get_section(`Talk:${article}`,0).then((talk) => {
console.log(talk)
let talktext = talk.wikitext + `\n${res}`
console.log(talktext)
var params = {
action: 'edit',
title: `Talk:${article}`,
summary: `Article appeared on [[WP:DYK|DYK]] on ${today}, adding {{[[Template:DYK talk|DYK talk]]}}`+tag,
section: '0',
text: talktext,
format: 'json'
}
return api.postWithToken( 'csrf', params );
})
}
}
// </nowiki>