2019-12-11 14:48:38 +01:00
|
|
|
export function findLink(target, parentClass = 'toot') {
|
2018-06-02 18:06:31 +02:00
|
|
|
if (target.localName === 'a') {
|
|
|
|
return target.href
|
|
|
|
}
|
|
|
|
if (target.parentNode === undefined || target.parentNode === null) {
|
|
|
|
return null
|
|
|
|
}
|
2018-10-29 16:54:11 +01:00
|
|
|
if (target.parentNode.getAttribute('class') === parentClass) {
|
2018-06-02 18:06:31 +02:00
|
|
|
return null
|
|
|
|
}
|
2018-10-29 16:54:11 +01:00
|
|
|
return findLink(target.parentNode, parentClass)
|
2018-06-02 18:06:31 +02:00
|
|
|
}
|
|
|
|
|
2019-12-11 14:48:38 +01:00
|
|
|
export function findTag(target, parentClass = 'toot') {
|
2018-06-02 18:06:31 +02:00
|
|
|
if (target.getAttribute('class') && target.getAttribute('class').includes('hashtag')) {
|
2018-11-03 04:35:03 +01:00
|
|
|
return parseTag(target.href)
|
2018-06-02 18:06:31 +02:00
|
|
|
}
|
2018-10-10 18:13:31 +02:00
|
|
|
// In Pleroma, link does not have class.
|
|
|
|
// So I have to check URL.
|
|
|
|
if (target.href && target.href.match(/^https:\/\/[a-zA-Z0-9-.]+\/(tag|tags)\/.+/)) {
|
2018-11-03 04:35:03 +01:00
|
|
|
return parseTag(target.href)
|
2018-10-10 18:13:31 +02:00
|
|
|
}
|
2018-06-02 18:06:31 +02:00
|
|
|
if (target.parentNode === undefined || target.parentNode === null) {
|
2018-11-03 04:35:03 +01:00
|
|
|
return null
|
2018-06-02 18:06:31 +02:00
|
|
|
}
|
2018-10-29 16:54:11 +01:00
|
|
|
if (target.parentNode.getAttribute('class') === parentClass) {
|
2018-11-03 04:35:03 +01:00
|
|
|
return null
|
2018-06-02 18:06:31 +02:00
|
|
|
}
|
2018-11-03 04:35:03 +01:00
|
|
|
return findTag(target.parentNode, parentClass)
|
|
|
|
}
|
|
|
|
|
2019-12-11 14:48:38 +01:00
|
|
|
export function parseTag(tagURL) {
|
2018-11-03 04:35:03 +01:00
|
|
|
const res = tagURL.match(/^https:\/\/([a-zA-Z0-9-.]+)\/(tag|tags)\/(.+)/)
|
|
|
|
return res[3]
|
2018-06-02 18:06:31 +02:00
|
|
|
}
|
2018-06-05 17:42:15 +02:00
|
|
|
|
2019-12-11 14:48:38 +01:00
|
|
|
export function findAccount(target, parentClass = 'toot') {
|
2018-06-05 17:42:15 +02:00
|
|
|
if (target.getAttribute('class') && target.getAttribute('class').includes('u-url')) {
|
2019-12-11 14:48:38 +01:00
|
|
|
if (target.href && target.href.match(/^https:\/\/[a-zA-Z0-9-.]+\/users\/[a-zA-Z0-9-_.]+$/)) {
|
2018-12-12 00:29:54 +01:00
|
|
|
return parsePleromaAccount(target.href)
|
|
|
|
} else {
|
|
|
|
return parseMastodonAccount(target.href)
|
|
|
|
}
|
2018-10-10 18:13:31 +02:00
|
|
|
}
|
|
|
|
// In Pleroma, link does not have class.
|
2018-11-13 15:30:30 +01:00
|
|
|
// So we have to check URL.
|
2019-12-11 14:48:38 +01:00
|
|
|
if (target.href && target.href.match(/^https:\/\/[a-zA-Z0-9-.]+\/@[a-zA-Z0-9-_.]+$/)) {
|
2018-11-13 15:30:30 +01:00
|
|
|
return parseMastodonAccount(target.href)
|
|
|
|
}
|
|
|
|
// Toot URL of Pleroma does not contain @.
|
2019-12-11 14:48:38 +01:00
|
|
|
if (target.href && target.href.match(/^https:\/\/[a-zA-Z0-9-.]+\/users\/[a-zA-Z0-9-_.]+$/)) {
|
2018-11-13 15:30:30 +01:00
|
|
|
return parsePleromaAccount(target.href)
|
2018-06-05 17:42:15 +02:00
|
|
|
}
|
|
|
|
if (target.parentNode === undefined || target.parentNode === null) {
|
|
|
|
return null
|
|
|
|
}
|
2018-10-29 16:54:11 +01:00
|
|
|
if (target.parentNode.getAttribute('class') === parentClass) {
|
2018-06-05 17:42:15 +02:00
|
|
|
return null
|
|
|
|
}
|
2018-10-29 16:54:11 +01:00
|
|
|
return findAccount(target.parentNode, parentClass)
|
2018-06-05 17:42:15 +02:00
|
|
|
}
|
2018-10-10 18:13:31 +02:00
|
|
|
|
2019-12-11 14:48:38 +01:00
|
|
|
export function parseMastodonAccount(accountURL) {
|
|
|
|
const res = accountURL.match(/^https:\/\/([a-zA-Z0-9-.]+)\/(@[a-zA-Z0-9-_.]+)$/)
|
2018-10-10 18:13:31 +02:00
|
|
|
const domainName = res[1]
|
|
|
|
const accountName = res[2]
|
2018-10-20 14:55:25 +02:00
|
|
|
return {
|
|
|
|
username: accountName,
|
2018-12-12 00:29:54 +01:00
|
|
|
acct: `${accountName}@${domainName}`,
|
|
|
|
url: accountURL
|
2018-10-20 14:55:25 +02:00
|
|
|
}
|
2018-10-10 18:13:31 +02:00
|
|
|
}
|
2018-11-13 15:30:30 +01:00
|
|
|
|
2019-12-11 14:48:38 +01:00
|
|
|
export function parsePleromaAccount(accountURL) {
|
|
|
|
const res = accountURL.match(/^https:\/\/([a-zA-Z0-9-.]+)\/users\/([a-zA-Z0-9-_.]+)$/)
|
2018-11-13 15:30:30 +01:00
|
|
|
const domainName = res[1]
|
|
|
|
const accountName = res[2]
|
|
|
|
return {
|
2018-12-12 02:07:22 +01:00
|
|
|
username: `@${accountName}`,
|
|
|
|
acct: `@${accountName}@${domainName}`,
|
2018-12-12 00:29:54 +01:00
|
|
|
url: accountURL
|
2018-11-13 15:30:30 +01:00
|
|
|
}
|
|
|
|
}
|