thedesk/app/js/login/login.js

347 lines
9.1 KiB
JavaScript
Raw Normal View History

2018-01-28 21:22:43 +09:00
/*ログイン処理・認証までのJS*/
//最初に読むやつ
2018-03-31 11:39:06 +09:00
localStorage.removeItem('kirishima')
localStorage.removeItem('quoters')
localStorage.removeItem('imas')
2020-07-10 22:33:12 +09:00
//stable, 固定タグのことらしい。ふざけるな。
localStorage.removeItem('stable')
2020-07-10 22:33:12 +09:00
2018-01-28 21:22:43 +09:00
function ck() {
2020-07-10 22:33:12 +09:00
const main = localStorage.getItem('main')
2019-05-19 16:39:30 +09:00
if (!main) {
localStorage.setItem('main', 0)
2018-05-20 15:17:10 +09:00
}
2019-11-04 01:10:06 +09:00
2018-02-18 14:43:11 +09:00
//コード受信
2019-05-19 16:39:30 +09:00
if (location.search) {
2020-07-10 22:33:12 +09:00
const m = location.search.match(/\?mode=([a-zA-Z-0-9]+)\&code=(.+)/)
const mode = m[1]
const codex = m[2]
if (mode == 'manager' || mode == 'login') {
code(codex, mode)
2019-05-19 16:39:30 +09:00
} else {
2018-02-18 14:43:11 +09:00
}
}
2020-07-10 22:33:12 +09:00
const multi = localStorage.getItem('multi')
if (!multi || multi == '[]') {
2020-07-10 22:33:12 +09:00
const date = new Date()
localStorage.setItem('showSupportMe', date.getMonth() + 2)
location.href = 'acct.html?mode=first&code=true'
2019-05-19 16:39:30 +09:00
} else {
2020-07-10 22:33:12 +09:00
const obj = JSON.parse(multi)
const keymap = Object.keys(obj)
for (let i = 0; i < keymap.length; i++) {
const key = keymap[i]
const acct = obj[key]
2019-05-19 16:39:30 +09:00
if (acct.domain) {
refresh(key, true)
2019-05-19 16:39:30 +09:00
}
2020-07-10 22:33:12 +09:00
}
2019-05-19 16:39:30 +09:00
if (obj[0].domain) {
2020-07-10 22:33:12 +09:00
showElm('#tl')
ticker()
multiSelector(false)
2020-07-10 22:33:12 +09:00
verck(ver)
showElm('.stw')
const tips = localStorage.getItem('tips')
if (tips) {
tips(tips)
2020-04-08 15:27:47 +09:00
}
2020-07-10 22:33:12 +09:00
document.querySelector('#something-wrong img').setAttribute('src', '../../img/thinking.svg')
2019-02-05 11:33:32 +09:00
}
2018-01-28 21:22:43 +09:00
}
}
ck()
2020-07-10 22:33:12 +09:00
//ユーザーデータ更新
async function refresh(target, loadskip) {
const multi = localStorage.getItem('multi')
let obj = JSON.parse(multi)
const {mode, domain, at, background, text} = obj[target]
if (mode == 'misskey') {
return
2018-02-26 00:32:10 +09:00
}
2020-07-10 22:33:12 +09:00
const start = `https://${domain}/api/v1/accounts/verify_credentials`
const json = await getApi(start, at)
if (json.error) {
console.error('Error:' + json.error)
M.toast({ html: lang.lang_fatalerroroccured + 'Error:' + json.error, displayLength: 5000 })
return
}
2020-07-10 22:33:12 +09:00
let avatar = json['avatar']
//missingがmissingなやつ
if (avatar == '/avatars/original/missing.png' || !avatar) {
avatar = './img/missing.svg'
}
let ref = {
at: at,
name: json['display_name'],
domain: domain,
user: json['acct'],
prof: avatar,
id: json['id'],
vis: json['source']['privacy']
}
if (background) {
ref.background = background
2018-02-18 14:43:11 +09:00
}
2020-07-10 22:33:12 +09:00
if (text) {
ref.text = text
}
if (json['source']['sensitive']) {
localStorage.setItem('nsfw_' + target, 'true')
2019-05-19 16:39:30 +09:00
} else {
2020-07-10 22:33:12 +09:00
localStorage.removeItem('nsfw_' + target)
2018-02-18 14:43:11 +09:00
}
2020-07-10 22:33:12 +09:00
obj[target] = ref
const save = JSON.stringify(obj)
localStorage.setItem('multi', save)
if (!loadskip) {
load()
2018-08-17 02:21:40 +09:00
}
}
2018-03-27 11:39:35 +09:00
//MarkdownやBBCodeの対応、文字数制限をチェック
2018-08-17 02:21:40 +09:00
//絶対ストリーミングを閉じさせないマン
2018-02-09 01:43:11 +09:00
function ckdb(acct_id) {
2020-07-10 22:33:12 +09:00
const domain = localStorage.getItem(`domain_${acct_id}`)
if (domain == 'kirishima.cloud') {
localStorage.setItem('kirishima', 'true')
} else if (domain == 'imastodon.net') {
localStorage.setItem('imas', 'true')
2020-07-10 22:33:12 +09:00
showElm('.imasonly')
2018-02-09 01:43:11 +09:00
}
2020-07-10 22:33:12 +09:00
const at = localStorage.getItem(`acct_${acct_id}_at`)
const letters = `${domain}_letters`
const quoteMarker = `${domain}_quote`
2020-07-10 14:16:39 +09:00
if (idata) {
2020-07-10 14:21:24 +09:00
//check and replace json to idata
var json = idata
if (json[quoteMarker] == 'enabled') {
localStorage.setItem('quoters', 'true')
localStorage.setItem('quote_' + acct_id, 'true')
2019-09-08 01:33:01 +09:00
}
2019-05-19 16:39:30 +09:00
if (json[bbcode]) {
if (json[bbcode] == 'enabled') {
localStorage.setItem('bb_' + acct_id, 'true')
2018-02-25 17:41:34 +09:00
} else {
localStorage.removeItem('bb_' + acct_id)
$("[data-activates='bbcode']").addClass('disabled')
$("[data-activates='bbcode']").prop('disabled', true)
}
2019-05-19 16:39:30 +09:00
} else {
localStorage.removeItem('bb_' + acct_id)
$("[data-activates='bbcode']").addClass('disabled')
$("[data-activates='bbcode']").addClass('disabled', true)
2019-05-19 16:39:30 +09:00
}
if (json[domain + '_markdown'] == 'enabled') {
localStorage.setItem('md_' + acct_id, 'true')
$('.markdown').show()
2019-05-19 16:39:30 +09:00
} else {
$('.anti-markdown').hide()
$('.markdown').hide()
localStorage.removeItem('bb_' + acct_id)
2019-05-19 16:39:30 +09:00
}
if (json[domain + '_home']) {
localStorage.setItem('home_' + acct_id, json[domain + '_home'])
2019-05-19 16:39:30 +09:00
}
if (json[domain + '_local']) {
localStorage.setItem('local_' + acct_id, json[domain + '_local'])
2019-05-19 16:39:30 +09:00
}
if (json[domain + '_public']) {
localStorage.setItem('public_' + acct_id, json[domain + '_public'])
2019-05-19 16:39:30 +09:00
}
if (json[domain + '_notification']) {
localStorage.setItem('notification_' + acct_id, json[domain + '_notification'])
2019-05-19 16:39:30 +09:00
}
if (json[domain + '_post']) {
localStorage.setItem('post_' + acct_id, json[domain + '_post'])
2019-05-19 16:39:30 +09:00
}
if (json[domain + '_fav']) {
localStorage.setItem('fav_' + acct_id, json[domain + '_fav'])
2019-05-19 16:39:30 +09:00
}
if (json[domain + '_bt']) {
localStorage.setItem('bt_' + acct_id, json[domain + '_bt'])
2019-05-19 16:39:30 +09:00
}
if (json[domain + '_follow']) {
localStorage.setItem('followlocale_' + acct_id, json[domain + '_follow'])
2019-05-19 16:39:30 +09:00
}
2018-02-25 17:41:34 +09:00
}
if (localStorage.getItem('mode_' + domain) != 'misskey') {
var start = 'https://' + domain + '/api/v1/instance'
2018-08-17 02:21:40 +09:00
fetch(start, {
method: 'GET',
2018-08-17 02:21:40 +09:00
headers: {
'content-type': 'application/json'
2018-08-17 02:21:40 +09:00
}
2019-11-04 01:10:06 +09:00
})
2020-07-10 22:33:12 +09:00
.then(function (response) {
return response.json()
2019-11-04 01:10:06 +09:00
})
2020-07-10 22:33:12 +09:00
.catch(function (error) {
console.error(error)
2019-11-04 01:10:06 +09:00
})
2020-07-10 22:33:12 +09:00
.then(function (json) {
2019-11-04 01:10:06 +09:00
if (json.error) {
console.error(json.error)
return
2018-09-17 20:55:00 +09:00
}
2019-11-04 01:10:06 +09:00
if (json) {
if (json['max_toot_chars']) {
localStorage.setItem('letters_' + acct_id, json['max_toot_chars'])
2019-11-04 01:10:06 +09:00
}
if (json['urls']['streaming_api']) {
localStorage.setItem('streaming_' + acct_id, json['urls']['streaming_api'])
2019-11-04 01:10:06 +09:00
} else {
localStorage.removeItem('streaming_' + acct_id)
2019-11-04 01:10:06 +09:00
}
2018-09-17 20:55:00 +09:00
}
})
2019-05-19 16:39:30 +09:00
} else {
2018-08-17 02:21:40 +09:00
}
2018-01-28 21:22:43 +09:00
}
//アカウントを選択…を実装
2019-07-06 23:16:41 +09:00
function multiSelector(parseC) {
var multi = localStorage.getItem('multi')
2018-01-28 21:22:43 +09:00
if (!multi) {
var obj = []
var json = JSON.stringify(obj)
localStorage.setItem('multi', json)
2018-01-28 21:22:43 +09:00
} else {
var obj = JSON.parse(multi)
2018-01-28 21:22:43 +09:00
}
var templete
if (localStorage.getItem('mainuse') == 'main') {
var last = localStorage.getItem('main')
} else if (localStorage.getItem('last-use')) {
var last = localStorage.getItem('last-use')
if (last == 'webview' || last == 'noauth') {
last = '0'
2019-05-20 22:34:58 +09:00
}
2019-05-19 16:39:30 +09:00
} else {
var last = '0'
2018-05-20 15:17:10 +09:00
}
last = last + ''
var sel
2019-05-19 16:39:30 +09:00
if (obj.length < 1) {
$('#src-acct-sel').html('<option value="tootsearch">Tootsearch</option>')
$('#add-acct-sel').html('<option value="noauth">' + lang.lang_login_noauth + '</option>')
2019-05-19 16:39:30 +09:00
} else {
2020-07-10 22:33:12 +09:00
Object.keys(obj).forEach(function (key) {
var acct = obj[key]
var list = key * 1 + 1
if (key + '' === last) {
sel = 'selected'
var domain = acct.domain
localStorage.setItem('domain_' + key, domain)
if (idata[domain + '_letters']) {
$('#textarea').attr('data-length', idata[domain + '_letters'])
2019-05-19 16:39:30 +09:00
} else {
var maxletters = localStorage.getItem('letters_' + key)
2019-05-19 16:39:30 +09:00
if (maxletters > 0) {
$('#textarea').attr('data-length', maxletters)
2019-05-19 16:39:30 +09:00
} else {
$('#textarea').attr('data-length', 500)
2019-05-19 16:39:30 +09:00
}
}
if (idata[domain + '_glitch']) {
$('#local-button').removeClass('hide')
2019-05-19 16:39:30 +09:00
}
var profimg = acct.prof
2019-08-11 02:52:32 +09:00
//localStorage.setItem("prof_" + key, profimg);
2019-05-19 16:39:30 +09:00
if (!profimg) {
profimg = '../../img/missing.svg'
2019-05-19 16:39:30 +09:00
}
$('#acct-sel-prof').attr('src', profimg)
2019-05-19 16:39:30 +09:00
if (domain) {
var cc = '(' + domain + ')'
2019-05-19 16:39:30 +09:00
} else {
var cc = ''
2019-05-19 16:39:30 +09:00
}
$('#toot-post-btn').text(lang.lang_toot + cc)
if (acct.background && acct.background != 'def' && acct.text && acct.text != 'def') {
$('#toot-post-btn').removeClass('indigo')
$('#toot-post-btn').css('background-color', '#' + acct.background)
$('#toot-post-btn').css('color', acct.text)
2019-05-19 16:39:30 +09:00
} else {
}
if (domain == 'kirishima.cloud') {
$('#faicon-btn').show()
2019-05-19 16:39:30 +09:00
} else {
$('#faicon-btn').hide()
2019-05-19 16:39:30 +09:00
}
if (domain == 'imastodon.net') {
trendTag()
2019-05-19 16:39:30 +09:00
} else {
$('#trendtag').html('')
2019-05-19 16:39:30 +09:00
}
} else {
sel = ''
2018-04-16 22:58:14 +09:00
}
templete =
'<option value="' +
key +
'" data-icon="' +
acct.prof +
'" class="left circle" ' +
sel +
'>' +
acct.user +
'@' +
acct.domain +
'</option>'
$('.acct-sel').append(templete)
})
$('#src-acct-sel').append('<option value="tootsearch">Tootsearch</option>')
$('#add-acct-sel').append(
'<option value="noauth">' +
2020-07-10 22:33:12 +09:00
lang.lang_login_noauth +
'</option><option value="webview">Twitter</option>'
)
$('#dir-acct-sel').append('<option value="noauth">' + lang.lang_login_noauth + '</option>')
2018-03-27 11:39:35 +09:00
}
$('select').formSelect()
2019-11-04 01:10:06 +09:00
if (!parseC) {
parseColumn(null, true)
2019-07-06 23:16:41 +09:00
}
2018-01-28 21:22:43 +09:00
}
2018-02-18 14:43:11 +09:00
2018-03-14 03:31:31 +09:00
//バージョンエンコ
2019-05-19 16:39:30 +09:00
function enc(ver) {
var ver = ver.replace(/\s/g, '')
var ver = ver.replace(/\(/g, '-')
var ver = ver.replace(/\)/g, '')
var ver = ver.replace(/\[/g, '_')
var ver = ver.replace(/\]/g, '')
return ver
2018-03-31 11:39:06 +09:00
}
2018-12-09 03:46:01 +09:00
//インスタンスティッカー
function ticker() {
2020-07-10 14:22:26 +09:00
var start = 'https://toot-app.thedesk.top/toot/index.php'
2018-12-09 03:46:01 +09:00
fetch(start, {
method: 'GET',
2020-05-03 22:46:55 +09:00
cors: true,
2018-12-09 03:46:01 +09:00
headers: {
'content-type': 'application/json'
2018-12-09 03:46:01 +09:00
}
2019-11-04 01:10:06 +09:00
})
2020-07-10 22:33:12 +09:00
.then(function (response) {
2019-11-04 01:10:06 +09:00
if (!response.ok) {
2020-07-10 22:33:12 +09:00
response.text().then(function (text) {
setLog(response.url, response.status, text)
})
2019-11-04 01:10:06 +09:00
}
return response.json()
2019-11-04 01:10:06 +09:00
})
2020-07-10 22:33:12 +09:00
.catch(function (error) {
console.error(error)
2019-11-04 01:10:06 +09:00
})
2020-07-10 22:33:12 +09:00
.then(function (json) {
2019-11-04 01:10:06 +09:00
if (json) {
localStorage.setItem('ticker', JSON.stringify(json))
2019-11-04 01:10:06 +09:00
}
})
2019-11-04 01:10:06 +09:00
}