thedesk/app/js/platform/first.js

312 lines
7.0 KiB
JavaScript
Raw Normal View History

2020-11-28 07:57:11 +11:00
window.onload = function () {
console.log('loaded')
initPostbox()
connection()
initPlugin(plugins)
2021-04-09 17:35:40 +10:00
if (localStorage.getItem('control-center-np')) $('#ccnp').removeClass('hide')
2020-11-28 07:57:11 +11:00
}
2020-09-09 00:35:30 +10:00
$.strip_tags = function (str, allowed) {
2019-05-19 17:39:30 +10:00
if (!str) {
2019-11-09 00:52:54 +11:00
return ''
2019-05-19 17:39:30 +10:00
}
2019-11-09 00:52:54 +11:00
allowed = (((allowed || '') + '').toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join('')
2019-05-19 17:39:30 +10:00
var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>?/gi,
2019-11-09 00:52:54 +11:00
commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi
2020-09-09 00:35:30 +10:00
return str.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
2019-11-09 00:52:54 +11:00
return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : ''
})
}
2019-05-19 17:39:30 +10:00
function escapeHTML(str) {
if (!str) {
2019-11-09 00:52:54 +11:00
return ''
2018-12-09 05:46:01 +11:00
}
2019-11-09 00:52:54 +11:00
return str
.replace(/&/g, '&amp;')
2019-05-19 17:39:30 +10:00
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
2019-11-09 00:52:54 +11:00
.replace(/'/g, '&#039;')
2019-05-19 17:39:30 +10:00
}
//PHPのnl2brと同様
2018-12-09 05:46:01 +11:00
function nl2br(str) {
2019-05-19 17:39:30 +10:00
if (!str) {
2019-11-09 00:52:54 +11:00
return ''
2018-12-09 05:46:01 +11:00
}
2019-11-09 00:52:54 +11:00
str = str.replace(/\r\n/g, '<br />')
str = str.replace(/(\n|\r)/g, '<br />')
return str
2019-01-22 03:25:40 +11:00
}
2019-04-14 02:42:18 +10:00
function br2nl(str) {
2019-05-19 17:39:30 +10:00
if (!str) {
2019-11-09 00:52:54 +11:00
return ''
2019-04-14 02:42:18 +10:00
}
2019-11-09 00:52:54 +11:00
str = str.replace(/<br \/>/g, '\r\n')
return str
2019-04-14 02:42:18 +10:00
}
2019-05-19 17:39:30 +10:00
function formattime(date) {
2019-11-09 00:52:54 +11:00
var str = date.getFullYear() + '-'
2019-05-19 17:39:30 +10:00
if (date.getMonth() + 1 < 10) {
2019-11-09 00:52:54 +11:00
str = str + '0' + (date.getMonth() + 1) + '-'
2019-05-19 17:39:30 +10:00
} else {
2019-11-09 00:52:54 +11:00
str = str + (date.getMonth() + 1) + '-'
2019-05-19 17:39:30 +10:00
}
if (date.getDate() < 10) {
2019-11-09 00:52:54 +11:00
str = str + '0' + date.getDate()
2019-05-19 17:39:30 +10:00
} else {
str = str + date.getDate()
}
2019-11-09 00:52:54 +11:00
str = str + 'T'
2019-05-19 17:39:30 +10:00
if (date.getHours() < 10) {
2019-11-09 00:52:54 +11:00
str = str + '0' + date.getHours() + ':'
2019-05-19 17:39:30 +10:00
} else {
2019-11-09 00:52:54 +11:00
str = str + date.getHours() + ':'
2019-05-19 17:39:30 +10:00
}
if (date.getMinutes() < 10) {
2019-11-09 00:52:54 +11:00
str = str + '0' + date.getMinutes()
2019-05-19 17:39:30 +10:00
} else {
str = str + date.getMinutes()
2019-01-22 03:25:40 +11:00
}
2019-11-09 00:52:54 +11:00
return escapeHTML(str)
2019-01-22 03:25:40 +11:00
}
2019-05-19 17:39:30 +10:00
function formattimeutc(date) {
2019-11-09 00:52:54 +11:00
var str = date.getUTCFullYear() + '-'
2019-05-19 17:39:30 +10:00
if (date.getUTCMonth() + 1 < 10) {
2019-11-09 00:52:54 +11:00
str = str + '0' + (date.getUTCMonth() + 1) + '-'
2019-05-19 17:39:30 +10:00
} else {
2019-11-09 00:52:54 +11:00
str = str + (date.getUTCMonth() + 1) + '-'
2019-05-19 17:39:30 +10:00
}
if (date.getUTCDate() < 10) {
2019-11-09 00:52:54 +11:00
str = str + '0' + date.getUTCDate()
2019-05-19 17:39:30 +10:00
} else {
str = str + date.getUTCDate()
}
2019-11-09 00:52:54 +11:00
str = str + 'T'
2019-05-19 17:39:30 +10:00
if (date.getUTCHours() < 10) {
2019-11-09 00:52:54 +11:00
str = str + '0' + date.getUTCHours() + ':'
2019-05-19 17:39:30 +10:00
} else {
2019-11-09 00:52:54 +11:00
str = str + date.getUTCHours() + ':'
2019-05-19 17:39:30 +10:00
}
if (date.getUTCMinutes() < 10) {
2019-11-09 00:52:54 +11:00
str = str + '0' + date.getUTCMinutes()
2019-05-19 17:39:30 +10:00
} else {
str = str + date.getUTCMinutes()
2019-01-22 03:25:40 +11:00
}
2019-11-09 00:52:54 +11:00
return escapeHTML(str)
2019-03-08 05:19:26 +11:00
}
2019-11-09 00:52:54 +11:00
postMessage(['sendSinmpleIpc', 'custom-css-request'], '*')
2019-05-19 17:39:30 +10:00
function makeCID() {
2020-09-09 00:35:30 +10:00
let chars = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".split("")
for (let i = 0, len = chars.length; i < len; i++) {
switch (chars[i]) {
case "x":
chars[i] = Math.floor(Math.random() * 16).toString(16)
break
case "y":
chars[i] = (Math.floor(Math.random() * 4) + 8).toString(16)
break
}
}
return chars.join("")
2019-03-08 05:19:26 +11:00
}
2019-05-19 17:39:30 +10:00
function randomStr(l) {
2019-03-08 05:19:26 +11:00
// 生成する文字列に含める文字セット
2019-11-09 00:52:54 +11:00
var c = 'abcdefghijklmnopqrstuvwxyz0123456789'
var cl = c.length
var r = ''
2019-05-19 17:39:30 +10:00
for (var i = 0; i < l; i++) {
2019-11-09 00:52:54 +11:00
r += c[Math.floor(Math.random() * cl)]
2019-03-08 05:19:26 +11:00
}
2019-11-09 00:52:54 +11:00
return r
2019-03-08 05:19:26 +11:00
}
2019-05-19 17:39:30 +10:00
function rgbToHex(color) {
// HEXに変換したものを代入する変数
2019-11-09 00:52:54 +11:00
var hex = ''
2019-05-19 17:39:30 +10:00
// 第1引数がHEXのとき変換処理は必要ないのでそのままreturn
// IE8の場合はjQueryのcss()関数でHEXを返すので除外
if (color.match(/^#[a-f\d]{3}$|^#[a-f\d]{6}$/i)) {
2019-11-09 00:52:54 +11:00
return color
2019-05-19 17:39:30 +10:00
}
// 正規表現
2019-11-09 00:52:54 +11:00
var regex = color.match(/^rgb\(([0-9.]+),\s*([0-9.]+),\s*([0-9.]+)\)$/)
2019-05-19 17:39:30 +10:00
// 正規表現でマッチしたとき
if (regex) {
2019-11-09 00:52:54 +11:00
var rgb = [
// RGBからHEXへ変換
parseInt(regex[1]).toString(16),
parseInt(regex[2]).toString(16),
parseInt(regex[3]).toString(16)
]
2019-05-19 17:39:30 +10:00
for (var i = 0; i < rgb.length; ++i) {
// rgb(1,1,1)のようなときHEXに変換すると1桁になる
// 1桁のときは前に0を足す
if (rgb[i].length == 1) {
2019-11-09 00:52:54 +11:00
rgb[i] = '0' + rgb[i]
2019-05-19 17:39:30 +10:00
}
2019-11-09 00:52:54 +11:00
hex += rgb[i]
2019-05-19 17:39:30 +10:00
}
2019-11-09 00:52:54 +11:00
return hex
2019-05-19 17:39:30 +10:00
}
2019-11-09 00:52:54 +11:00
console.error(color + ':第1引数はRGB形式で入力')
2019-05-19 16:17:05 +10:00
}
2019-05-19 17:39:30 +10:00
/*マルチバイト用切り出し*/
2020-09-09 00:35:30 +10:00
$.isSurrogatePear = function (upper, lower) {
2019-11-09 00:52:54 +11:00
return 0xd800 <= upper && upper <= 0xdbff && 0xdc00 <= lower && lower <= 0xdfff
}
2020-09-09 00:35:30 +10:00
$.mb_strlen = function (str) {
2019-11-11 03:20:51 +11:00
var splitter = new GraphemeSplitter()
var arr = splitter.splitGraphemes(str)
return arr.length
2019-11-09 00:52:54 +11:00
}
2020-09-09 00:35:30 +10:00
$.mb_substr = function (str, begin, end) {
2019-11-11 03:20:51 +11:00
//配列にする
var splitter = new GraphemeSplitter()
var arr = splitter.splitGraphemes(str)
var newarr = []
2019-12-01 22:28:20 +11:00
for (var i = 0; i < arr.length; i++) {
if (i >= begin && i <= end) {
2019-11-11 03:20:51 +11:00
newarr.push(arr[i])
2019-05-19 17:39:30 +10:00
}
}
2019-11-11 03:20:51 +11:00
return newarr.join('')
2019-11-09 00:52:54 +11:00
}
2019-07-29 01:42:17 +10:00
//ソートするやつ
function object_array_sort(data, key, order, fn) {
2019-11-09 00:52:54 +11:00
var num_a = -1
var num_b = 1
2019-07-29 01:42:17 +10:00
if (order === 'asc') {
2019-11-09 00:52:54 +11:00
num_a = 1
num_b = -1
}
2020-09-09 00:35:30 +10:00
data = data.sort(function (a, b) {
2019-11-09 00:52:54 +11:00
var x = a[key]
var y = b[key]
if (x > y) return num_a
if (x < y) return num_b
return 0
})
var arrObj = {}
2019-07-29 01:42:17 +10:00
for (var i = 0; i < data.length; i++) {
2019-11-09 00:52:54 +11:00
arrObj[data[i]['family']] = data[i]
2019-07-29 01:42:17 +10:00
}
2019-11-09 00:52:54 +11:00
data = []
2019-07-29 01:42:17 +10:00
for (var key in arrObj) {
2019-11-09 00:52:54 +11:00
data.push(arrObj[key])
2019-07-29 01:42:17 +10:00
}
2019-11-09 00:52:54 +11:00
fn(data)
2019-07-29 01:42:17 +10:00
}
2019-11-09 00:52:54 +11:00
function setLog(txt1, txt2, txt3) {
2019-10-31 02:30:26 +11:00
//url,statuscode,responsetext
var text = new Date().toUTCString()
2019-11-09 00:52:54 +11:00
text = text + ',' + txt1 + ',' + txt2 + ',' + escapeCsv(txt3)
2019-10-31 02:30:26 +11:00
console.error(text)
2019-11-09 00:52:54 +11:00
postMessage(['log', text], '*')
2019-10-31 02:30:26 +11:00
}
function escapeCsv(str) {
2019-11-09 00:52:54 +11:00
if (!str) {
return str
}
var result
result = str.toString().replace(/\"/g, '""')
2019-11-09 00:52:54 +11:00
if (result.indexOf(',') >= 0) {
result = '"' + result + '"'
}
return result
}
2019-12-12 02:06:40 +11:00
function evalAttr(json, attr, lenCk) {
if (json[attr]) {
if (lenCk) {
if (json[attr][0]) {
return true
} else {
return false
}
} else {
return true
}
} else {
return false
}
}
2019-12-01 22:28:20 +11:00
function statusModel(now) {
2019-12-12 02:06:40 +11:00
if (!now) {
2019-12-01 22:28:20 +11:00
var now = new Date().toString()
}
return {
id: '',
created_at: now,
in_reply_to_id: null,
in_reply_to_account_id: null,
sensitive: false,
spoiler_text: '',
visibility: 'public',
language: 'en',
uri: '',
url: '',
replies_count: 0,
reblogs_count: 0,
favourites_count: 0,
favourited: false,
reblogged: false,
muted: false,
bookmarked: false,
pinned: false,
content: '<p><i>No status here</i></p>',
reblog: null,
application: {
name: null,
website: null
},
account: {
id: '',
username: '',
acct: '',
display_name: '',
locked: false,
bot: false,
created_at: now,
2019-12-12 02:06:40 +11:00
note: '',
2019-12-01 22:28:20 +11:00
url: '',
avatar: '',
2019-12-12 02:06:40 +11:00
avatar_static: '',
2019-12-01 22:28:20 +11:00
header: '',
header_static: '',
followers_count: 0,
following_count: 0,
statuses_count: 0,
last_status_at: now,
emojis: [],
fields: []
},
media_attachments: [],
mentions: [],
tags: [],
card: null,
poll: null
}
}
2021-04-09 17:35:40 +10:00
function webviewFinder() {
const webview = document.querySelector('webview')
webview.addEventListener('did-navigate', (e) => {
const url = webview.getURL()
if (url.match('https://mobile.twitter.com/login')) {
postMessage(['twitterLogin', null], '*')
} else if (url.match('https://mobile.twitter.com/logout')) {
postMessage(['twitterLogin', true], '*')
}
})
}
function initWebviewEvent() {
if (document.querySelector('webview')) { webviewFinder() } else {
const timerWV = setInterval(function () {
document.querySelector('webview')
? (webviewFinder(), clearInterval(timerWV))
: console.log('まだロード中')
}, 500)
}
}