kopia lustrzana https://github.com/lartsch/FediAct
add "resolving" indicator, increase timeouts for bad network performance situations
rodzic
318b08a189
commit
d9beb0a657
|
@ -5,6 +5,7 @@ const tokenInterval = 1 // minutes
|
|||
const mutesApi = "/api/v1/mutes"
|
||||
const blocksApi = "/api/v1/blocks"
|
||||
const domainBlocksApi = "/api/v1/domain_blocks"
|
||||
const timeout = 15000
|
||||
|
||||
const tokenRegex = /"access_token":".*?",/gm
|
||||
|
||||
|
@ -26,8 +27,9 @@ async function resolveToot(url) {
|
|||
try {
|
||||
const controller = new AbortController()
|
||||
const timeoutId = setTimeout(() => {
|
||||
log("Timed out")
|
||||
controller.abort()
|
||||
}, 5000)
|
||||
}, timeout)
|
||||
var res = await fetch(url, {method: 'HEAD', signal: controller.signal})
|
||||
clearTimeout(timeoutId)
|
||||
if (res.redirected) {
|
||||
|
|
|
@ -1 +1 @@
|
|||
var browser,chrome,i;const n=!0,a="[FediAct]",t=1,c="/api/v1/mutes",s="/api/v1/blocks",o="/api/v1/domain_blocks",r=/"access_token":".*?",/gm,d={fediact_homeinstance:null};function u(t){n&&console.log(a+" "+t)}async function h(i){return new Promise(async function(e){try{const a=new AbortController;var t=setTimeout(()=>{a.abort()},5e3),n=await fetch(i,{method:"HEAD",signal:a.signal});clearTimeout(t),n.redirected?e(n.url):e(!1)}catch(t){u(t),e(!1)}})}async function f(){return new Promise(async function(e){var t="https://"+i.fediact_homeinstance;try{var n=await(await fetch(t)).text()}catch(t){return u(t),void e(!1)}if(n){t=n.match(r);if(t){var n=t[0].search(/"access_token":"/),a=t[0].search(/",/);if(-1<n&&-1<a){t=t[0].substring(n+=16,a);if(16<t.length)return i.fediact_token=t,void e(!0)}}}i.fediact_token=null,u("Token could not be found."),e(!1)})}async function l(){return new Promise(async function(t){[i.fediact_mutes,i.fediact_blocks,i.fediact_domainblocks]=[[],[],[]];var[e,n,a]=await Promise.all([fetch("https://"+i.fediact_homeinstance+c,{headers:{Authorization:"Bearer "+i.fediact_token}}).then(t=>t.json()),fetch("https://"+i.fediact_homeinstance+s,{headers:{Authorization:"Bearer "+i.fediact_token}}).then(t=>t.json()),fetch("https://"+i.fediact_homeinstance+o,{headers:{Authorization:"Bearer "+i.fediact_token}}).then(t=>t.json())]);e.length&&i.fediact_mutes.push(...e.map(t=>t.acct)),n.length&&i.fediact_blocks.push(...n.map(t=>t.acct)),a.length&&(i.fediact_domainblocks=a),t(!0)})}async function m(){return new Promise(async function(n){try{i=await(browser||chrome).storage.local.get(d)}catch(t){return u(t),void n(!1)}if(i.fediact_homeinstance){await f(),await l();try{await(browser||chrome).storage.local.set(i),n(!0)}catch{u(e)}}else u("Home instance not set"),n(!1)})}async function g(){chrome.tabs.query({},async function(t){for(var e=0;e<t.length;++e)try{chrome.tabs.sendMessage(t[e].id,{updatedfedisettings:!0})}catch(t){continue}})}chrome.runtime.onInstalled.addListener(m),chrome.alarms.create("refresh",{periodInMinutes:t}),chrome.alarms.onAlarm.addListener(m),chrome.runtime.onMessage.addListener((t,a,e)=>t.url?(h(t.url).then(e),!0):t.updatedsettings?(m().then(g),!0):void(t.running&&chrome.tabs.onUpdated.addListener(async function(t,e,n){if(t===a.tab.id&&e.url)try{await chrome.tabs.sendMessage(t,{urlchanged:e.url})}catch(t){u(t)}})));
|
||||
var browser,chrome,i;const n=!0,a="[FediAct]",t=1,c="/api/v1/mutes",s="/api/v1/blocks",o="/api/v1/domain_blocks",r=15e3,d=/"access_token":".*?",/gm,u={fediact_homeinstance:null};function h(t){n&&console.log(a+" "+t)}async function f(i){return new Promise(async function(e){try{const a=new AbortController;var t=setTimeout(()=>{h("Timed out"),a.abort()},r),n=await fetch(i,{method:"HEAD",signal:a.signal});clearTimeout(t),n.redirected?e(n.url):e(!1)}catch(t){h(t),e(!1)}})}async function l(){return new Promise(async function(e){var t="https://"+i.fediact_homeinstance;try{var n=await(await fetch(t)).text()}catch(t){return h(t),void e(!1)}if(n){t=n.match(d);if(t){var n=t[0].search(/"access_token":"/),a=t[0].search(/",/);if(-1<n&&-1<a){t=t[0].substring(n+=16,a);if(16<t.length)return i.fediact_token=t,void e(!0)}}}i.fediact_token=null,h("Token could not be found."),e(!1)})}async function m(){return new Promise(async function(t){[i.fediact_mutes,i.fediact_blocks,i.fediact_domainblocks]=[[],[],[]];var[e,n,a]=await Promise.all([fetch("https://"+i.fediact_homeinstance+c,{headers:{Authorization:"Bearer "+i.fediact_token}}).then(t=>t.json()),fetch("https://"+i.fediact_homeinstance+s,{headers:{Authorization:"Bearer "+i.fediact_token}}).then(t=>t.json()),fetch("https://"+i.fediact_homeinstance+o,{headers:{Authorization:"Bearer "+i.fediact_token}}).then(t=>t.json())]);e.length&&i.fediact_mutes.push(...e.map(t=>t.acct)),n.length&&i.fediact_blocks.push(...n.map(t=>t.acct)),a.length&&(i.fediact_domainblocks=a),t(!0)})}async function g(){return new Promise(async function(n){try{i=await(browser||chrome).storage.local.get(u)}catch(t){return h(t),void n(!1)}if(i.fediact_homeinstance){await l(),await m();try{await(browser||chrome).storage.local.set(i),n(!0)}catch{h(e)}}else h("Home instance not set"),n(!1)})}async function _(){chrome.tabs.query({},async function(t){for(var e=0;e<t.length;++e)try{chrome.tabs.sendMessage(t[e].id,{updatedfedisettings:!0})}catch(t){continue}})}chrome.runtime.onInstalled.addListener(g),chrome.alarms.create("refresh",{periodInMinutes:t}),chrome.alarms.onAlarm.addListener(g),chrome.runtime.onMessage.addListener((t,a,e)=>t.url?(f(t.url).then(e),!0):t.updatedsettings?(g().then(_),!0):void(t.running&&chrome.tabs.onUpdated.addListener(async function(t,e,n){if(t===a.tab.id&&e.url)try{await chrome.tabs.sendMessage(t,{urlchanged:e.url})}catch(t){h(t)}})));
|
|
@ -7,7 +7,7 @@ const profileNamePaths = ["div.account__header__tabs__name small", "div.public-a
|
|||
const domainRegex = /^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$/
|
||||
const handleExtractUrlRegex = /^(?<domain>https?:\/\/(?:\.?[a-z0-9-]+)+(?:\.[a-z]+){1})?\/?@(?<handle>\w+)(?:@(?<handledomain>(?:[\w-]+\.)+?\w+))?(?:\/(?<tootid>\d+))?\/?$/
|
||||
const handleExtractUriRegex = /^(?<domain>https?:\/\/(?:\.?[a-z0-9-]+)+(?:\.[a-z]+){1})(?:\/users\/)(?<handle>\w+)(?:(?:\/statuses\/)(?<tootid>\d+))?\/?$/
|
||||
const enableConsoleLog = true
|
||||
const enableConsoleLog = false
|
||||
const logPrepend = "[FediAct]"
|
||||
const instanceApi = "/api/v1/instance"
|
||||
const statusApi = "/api/v1/statuses"
|
||||
|
@ -26,6 +26,7 @@ const modalHtml = `
|
|||
</ul>
|
||||
</div>
|
||||
</div>`
|
||||
const timeout = 15000
|
||||
|
||||
// settings keys with defauls
|
||||
var settings = {}
|
||||
|
@ -157,7 +158,7 @@ async function makeRequest(method, url, extraheaders, jsonbody) {
|
|||
// open it with the method and url specified
|
||||
xhr.open(method, url)
|
||||
// set timeout
|
||||
xhr.timeout = 5000
|
||||
xhr.timeout = timeout
|
||||
// set extra headers if any were given
|
||||
if (extraheaders) {
|
||||
for (var key in extraheaders) {
|
||||
|
@ -175,6 +176,7 @@ async function makeRequest(method, url, extraheaders, jsonbody) {
|
|||
}
|
||||
}
|
||||
xhr.ontimeout = function() {
|
||||
log("Timed out")
|
||||
resolve(false)
|
||||
}
|
||||
// on any error, resolve false
|
||||
|
@ -771,6 +773,7 @@ async function processToots() {
|
|||
if (!$(favButton).length) {
|
||||
favButton = $(el).find("a.icon-button:has(i.fa-star), a.detailed-status__link:has(i.fa-star)")
|
||||
}
|
||||
$("<span class='fediactprocessing' style='color: white; padding-right: 10px; padding-left: 10px'>Resolving...</span>").insertAfter($(favButton))
|
||||
var boostButton = $(el).find("button:has(i.fa-retweet)").first()
|
||||
if (!$(boostButton).length) {
|
||||
boostButton = $(el).find("a.icon-button:has(i.fa-retweet), a.detailed-status__link:has(i.fa-retweet)")
|
||||
|
@ -854,11 +857,12 @@ async function processToots() {
|
|||
// handles initialization of element styles
|
||||
function initStyles(tootdata) {
|
||||
// always remove any existing "Unresolved" indicator from the element first
|
||||
$(el).find(".feditriggered").remove()
|
||||
$(el).find(".fediactunresolved").remove()
|
||||
$(el).find(".fediactprocessing").remove()
|
||||
// is the toot unresolved?
|
||||
if (!tootdata[1]) {
|
||||
// yes, then add the Unresolved indicator
|
||||
$("<span class='feditriggered' style='color: orange; padding-right: 10px; padding-left: 10px'>Unresolved</span>").insertAfter($(favButton))
|
||||
$("<span class='fediactunresolved' style='color: orange; padding-right: 10px; padding-left: 10px'>Unresolved</span>").insertAfter($(favButton))
|
||||
} else {
|
||||
// otherwise start processing button styles (if enabled OR if the toot was already interacted with, to restore the state while still on the same page)
|
||||
// first enable the bookmark button (is disabled on external instances)
|
||||
|
|
File diff suppressed because one or more lines are too long
Ładowanie…
Reference in New Issue