该脚本旨在被注入维基百科网站。它将使用用户的登录凭证发布到编辑API。如何使用Wikipedia API在浏览器上编辑页面javascript?
这是我的尝试:
function getEditToken(){
return fetch(
'https://en.wikipedia.org/w/api.php?action=query&meta=tokens&format=json',
{credentials: 'include'}
)
.then(r => r.json())
.then(r => r.query.tokens.csrftoken)
}
function writeRevision(title, text, summary){
var url = `https://en.wikipedia.org/w/index.php?action=edit`
var formData = new FormData()
formData.append('title', title)
formData.append('text', text)
formData.append('summary', summary)
formData.append('contentmodel', 'wikitext')
var option = {
method: 'POST',
body: formData,
credentials: 'include',
}
return getEditToken()
.then(token => { formData.append('token', token); console.log(token) })
.then(x => fetch(url, option))
.then(r => r.text())
.then(console.log)
.catch(e => console.log(e))
}
writeRevision('User:eeeeeeeee/draft_1', 'foo wikitext', 'foo summary')
回应说:
编辑表单的某些部分没有到达服务器;请仔细检查 您的编辑是否完整,然后重试。
您是否包含编辑维基百科页面时通常发送的隐藏字段?比较使用脚本和常规维基百科时,chrome日志到“网络”选项卡时是否发送相同的字段? –