kopia lustrzana https://github.com/lartsch/FediAct
fix modal related stuff
rodzic
89f4d7d423
commit
999b0ded7b
|
@ -9,7 +9,8 @@ const timeout = 15000
|
|||
const tokenRegex = /"access_token":".*?",/gm
|
||||
// required settings keys with defauls
|
||||
const settingsDefaults = {
|
||||
fediact_homeinstance: null
|
||||
fediact_homeinstance: null,
|
||||
fediact_token: null
|
||||
}
|
||||
|
||||
// wrapper to prepend to log messages
|
||||
|
@ -126,51 +127,59 @@ async function fetchBearerToken() {
|
|||
// this is only done here in the bg script so we have data available on load of pages without first performing 3 (!) requests
|
||||
// otherwise this would lead to problems with element detection / low performance (espcially v3 instances)
|
||||
// mutes/blocks are updated in content script on page context changes and after performing mutes/block actions
|
||||
async function fetchMutesAndBlocks() {
|
||||
function fetchMutesAndBlocks() {
|
||||
return new Promise(async function(resolve) {
|
||||
// set empty initially
|
||||
[settings.fediact_mutes, settings.fediact_blocks, settings.fediact_domainblocks] = [[],[],[]]
|
||||
var [mutes, blocks, domainblocks] = await Promise.all([
|
||||
fetch("https://" + settings.fediact_homeinstance + mutesApi, {headers: {"Authorization": "Bearer "+settings.fediact_token}}).then((response) => response.json()),
|
||||
fetch("https://" + settings.fediact_homeinstance + blocksApi, {headers: {"Authorization": "Bearer "+settings.fediact_token}}).then((response) => response.json()),
|
||||
fetch("https://" + settings.fediact_homeinstance + domainBlocksApi, {headers: {"Authorization": "Bearer "+settings.fediact_token}}).then((response) => response.json())
|
||||
])
|
||||
if (mutes.length) {
|
||||
settings.fediact_mutes.push(...mutes.map(acc => acc.acct))
|
||||
try {
|
||||
// set empty initially
|
||||
[settings.fediact_mutes, settings.fediact_blocks, settings.fediact_domainblocks] = [[],[],[]]
|
||||
var [mutes, blocks, domainblocks] = await Promise.all([
|
||||
fetch("https://" + settings.fediact_homeinstance + mutesApi, {headers: {"Authorization": "Bearer "+settings.fediact_token}}).then((response) => response.json()),
|
||||
fetch("https://" + settings.fediact_homeinstance + blocksApi, {headers: {"Authorization": "Bearer "+settings.fediact_token}}).then((response) => response.json()),
|
||||
fetch("https://" + settings.fediact_homeinstance + domainBlocksApi, {headers: {"Authorization": "Bearer "+settings.fediact_token}}).then((response) => response.json())
|
||||
])
|
||||
if (mutes.length) {
|
||||
settings.fediact_mutes.push(...mutes.map(acc => acc.acct))
|
||||
}
|
||||
if (blocks.length) {
|
||||
settings.fediact_blocks.push(...blocks.map(acc => acc.acct))
|
||||
}
|
||||
if (domainblocks.length) {
|
||||
settings.fediact_domainblocks = domainblocks
|
||||
}
|
||||
resolve(true)
|
||||
} catch {
|
||||
resolve(false)
|
||||
}
|
||||
if (blocks.length) {
|
||||
settings.fediact_blocks.push(...blocks.map(acc => acc.acct))
|
||||
}
|
||||
if (domainblocks.length) {
|
||||
settings.fediact_domainblocks = domainblocks
|
||||
}
|
||||
resolve(true)
|
||||
})
|
||||
}
|
||||
|
||||
async function fetchData() {
|
||||
async function fetchData(token, mutesblocks) {
|
||||
return new Promise(async function(resolve) {
|
||||
var resolved = false
|
||||
try {
|
||||
settings = await (browser || chrome).storage.local.get(settingsDefaults)
|
||||
if (settings.fediact_homeinstance) {
|
||||
if (token || mutesblocks) {
|
||||
if (token || !(settings.fediact_token)) {
|
||||
await fetchBearerToken()
|
||||
}
|
||||
if (mutesblocks) {
|
||||
await fetchMutesAndBlocks()
|
||||
}
|
||||
try {
|
||||
await (browser || chrome).storage.local.set(settings)
|
||||
resolved = true
|
||||
} catch {
|
||||
log(e)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log("Home instance not set")
|
||||
}
|
||||
} catch(e) {
|
||||
log(e)
|
||||
resolve(false)
|
||||
return
|
||||
}
|
||||
if (settings.fediact_homeinstance) {
|
||||
await fetchBearerToken()
|
||||
await fetchMutesAndBlocks()
|
||||
} else {
|
||||
log("Home instance not set")
|
||||
resolve(false)
|
||||
return
|
||||
}
|
||||
try {
|
||||
await (browser || chrome).storage.local.set(settings)
|
||||
resolve(true)
|
||||
} catch {
|
||||
log(e)
|
||||
}
|
||||
resolve(resolved)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -188,10 +197,10 @@ async function reloadListeningScripts() {
|
|||
}
|
||||
|
||||
// fetch api token right after install (mostly for debugging, when the ext. is reloaded)
|
||||
chrome.runtime.onInstalled.addListener(fetchData)
|
||||
chrome.runtime.onInstalled.addListener(fetchData(true, true))
|
||||
// and also every 3 minutes
|
||||
chrome.alarms.create('refresh', { periodInMinutes: tokenInterval })
|
||||
chrome.alarms.onAlarm.addListener(fetchData)
|
||||
chrome.alarms.onAlarm.addListener(fetchData(true, true))
|
||||
|
||||
// different listeners for inter-script communication
|
||||
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
||||
|
@ -207,11 +216,11 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
|||
}
|
||||
// immediately fetch api token after settings are updated
|
||||
if (request.updatedsettings) {
|
||||
fetchData().then(reloadListeningScripts)
|
||||
fetchData(true, true).then(reloadListeningScripts)
|
||||
return true
|
||||
}
|
||||
if (request.updatemutedblocked) {
|
||||
fetchMutesAndBlocks().then((browser || chrome).storage.local.set(settings)).then(sendResponse)
|
||||
fetchData(false, true).then(sendResponse)
|
||||
return true
|
||||
}
|
||||
// when the content script starts to process on a site, listen for tab changes (url)
|
||||
|
|
|
@ -1 +1 @@
|
|||
var browser,chrome,i;const a=!0,n="[FediAct]",t=1,s="/api/v1/mutes",c="/api/v1/blocks",o="/api/v1/domain_blocks",r=15e3,d=/"access_token":".*?",/gm,u={fediact_homeinstance:null};function h(t){a&&console.log(n+" "+t)}async function l(i){return new Promise(async function(e){try{const n=new AbortController;var t=setTimeout(()=>{h("Timed out"),n.abort()},r),a=await fetch(i,{method:"HEAD",signal:n.signal});clearTimeout(t),a.redirected?e(a.url):e(!1)}catch(t){h(t),e(!1)}})}async function f(i){return new Promise(async function(e){try{const n=new AbortController;var t,a=setTimeout(()=>{h("Timed out"),n.abort()},r);t=i[3]?(i[2]["Content-Type"]="application/json",await fetch(i[1],{method:i[0],signal:n.signal,headers:i[2],body:JSON.stringify(i[3])})):i[2]?await fetch(i[1],{method:i[0],signal:n.signal,headers:i[2]}):await fetch(i[1],{method:i[0],signal:n.signal}),clearTimeout(a),200<=t.status&&t.status<300?e(await t.text()):e(!1)}catch(t){h(t),e(!1)}})}async function m(){return new Promise(async function(e){var t="https://"+i.fediact_homeinstance;try{var a=await(await fetch(t)).text()}catch(t){return h(t),void e(!1)}if(a){t=a.match(d);if(t){var a=t[0].search(/"access_token":"/),n=t[0].search(/",/);if(-1<a&&-1<n){t=t[0].substring(a+=16,n);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 g(){return new Promise(async function(t){[i.fediact_mutes,i.fediact_blocks,i.fediact_domainblocks]=[[],[],[]];var[e,a,n]=await Promise.all([fetch("https://"+i.fediact_homeinstance+s,{headers:{Authorization:"Bearer "+i.fediact_token}}).then(t=>t.json()),fetch("https://"+i.fediact_homeinstance+c,{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)),a.length&&i.fediact_blocks.push(...a.map(t=>t.acct)),n.length&&(i.fediact_domainblocks=n),t(!0)})}async function y(){return new Promise(async function(a){try{i=await(browser||chrome).storage.local.get(u)}catch(t){return h(t),void a(!1)}if(i.fediact_homeinstance){await m(),await g();try{await(browser||chrome).storage.local.set(i),a(!0)}catch{h(e)}}else h("Home instance not set"),a(!1)})}async function w(){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(y),chrome.alarms.create("refresh",{periodInMinutes:t}),chrome.alarms.onAlarm.addListener(y),chrome.runtime.onMessage.addListener((t,n,e)=>t.externaltoot?(l(t.externaltoot).then(e),!0):t.requestdata?(f(t.requestdata).then(e),!0):t.updatedsettings?(y().then(w),!0):t.updatemutedblocked?(g().then((browser||chrome).storage.local.set(i)).then(e),!0):void(t.running&&chrome.tabs.onUpdated.addListener(async function(t,e,a){if(t===n.tab.id&&e.url)try{await chrome.tabs.sendMessage(t,{urlchanged:e.url})}catch(t){h(t)}})));
|
||||
var browser,chrome,c;const a=!0,n="[FediAct]",t=1,i="/api/v1/mutes",s="/api/v1/blocks",o="/api/v1/domain_blocks",r=15e3,d=/"access_token":".*?",/gm,u={fediact_homeinstance:null,fediact_token:null};function h(t){a&&console.log(n+" "+t)}async function l(i){return new Promise(async function(e){try{const n=new AbortController;var t=setTimeout(()=>{h("Timed out"),n.abort()},r),a=await fetch(i,{method:"HEAD",signal:n.signal});clearTimeout(t),a.redirected?e(a.url):e(!1)}catch(t){h(t),e(!1)}})}async function f(i){return new Promise(async function(e){try{const n=new AbortController;var t,a=setTimeout(()=>{h("Timed out"),n.abort()},r);t=i[3]?(i[2]["Content-Type"]="application/json",await fetch(i[1],{method:i[0],signal:n.signal,headers:i[2],body:JSON.stringify(i[3])})):i[2]?await fetch(i[1],{method:i[0],signal:n.signal,headers:i[2]}):await fetch(i[1],{method:i[0],signal:n.signal}),clearTimeout(a),200<=t.status&&t.status<300?e(await t.text()):e(!1)}catch(t){h(t),e(!1)}})}async function m(){return new Promise(async function(e){var t="https://"+c.fediact_homeinstance;try{var a=await(await fetch(t)).text()}catch(t){return h(t),void e(!1)}if(a){t=a.match(d);if(t){var a=t[0].search(/"access_token":"/),n=t[0].search(/",/);if(-1<a&&-1<n){t=t[0].substring(a+=16,n);if(16<t.length)return c.fediact_token=t,void e(!0)}}}c.fediact_token=null,h("Token could not be found."),e(!1)})}function g(){return new Promise(async function(t){try{[c.fediact_mutes,c.fediact_blocks,c.fediact_domainblocks]=[[],[],[]];var[e,a,n]=await Promise.all([fetch("https://"+c.fediact_homeinstance+i,{headers:{Authorization:"Bearer "+c.fediact_token}}).then(t=>t.json()),fetch("https://"+c.fediact_homeinstance+s,{headers:{Authorization:"Bearer "+c.fediact_token}}).then(t=>t.json()),fetch("https://"+c.fediact_homeinstance+o,{headers:{Authorization:"Bearer "+c.fediact_token}}).then(t=>t.json())]);e.length&&c.fediact_mutes.push(...e.map(t=>t.acct)),a.length&&c.fediact_blocks.push(...a.map(t=>t.acct)),n.length&&(c.fediact_domainblocks=n),t(!0)}catch{t(!1)}})}async function y(n,i){return new Promise(async function(t){var a=!1;try{if((c=await(browser||chrome).storage.local.get(u)).fediact_homeinstance){if(n||i){!n&&c.fediact_token||await m(),i&&await g();try{await(browser||chrome).storage.local.set(c),a=!0}catch{h(e)}}}else h("Home instance not set")}catch(t){h(t)}t(a)})}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(y(!0,!0)),chrome.alarms.create("refresh",{periodInMinutes:t}),chrome.alarms.onAlarm.addListener(y(!0,!0)),chrome.runtime.onMessage.addListener((t,n,e)=>t.externaltoot?(l(t.externaltoot).then(e),!0):t.requestdata?(f(t.requestdata).then(e),!0):t.updatedsettings?(y(!0,!0).then(_),!0):t.updatemutedblocked?(y(!1,!0).then(e),!0):void(t.running&&chrome.tabs.onUpdated.addListener(async function(t,e,a){if(t===n.tab.id&&e.url)try{await chrome.tabs.sendMessage(t,{urlchanged:e.url})}catch(t){h(t)}})));
|
|
@ -125,26 +125,26 @@ var getUrlParameter = function getUrlParameter(sParam) {
|
|||
}
|
||||
|
||||
// promisified xhr for api calls
|
||||
async function makeRequest(method, url, extraheaders, jsonbody) {
|
||||
// try to prevent error 429 too many request by delaying home instance requests
|
||||
if (~url.indexOf(settings.fediact_homeinstance) && settings.fediact_enabledelay) {
|
||||
// get current time
|
||||
var currenttime = Date.now()
|
||||
// get difference of current time and time of last request
|
||||
var difference = currenttime - tmpSettings.lasthomerequest
|
||||
// if difference is smaller than our set api delay value...
|
||||
if (difference < apiDelay) {
|
||||
// ... then wait the time required to reach the api delay value...
|
||||
await new Promise(resolve => {
|
||||
setTimeout(function() {
|
||||
resolve()
|
||||
}, apiDelay-difference)
|
||||
})
|
||||
}
|
||||
// TODO: move this to the top? or get new Date.now() here?
|
||||
tmpSettings.lasthomerequest = currenttime
|
||||
}
|
||||
function makeRequest(method, url, extraheaders, jsonbody) {
|
||||
return new Promise(async function(resolve) {
|
||||
// try to prevent error 429 too many request by delaying home instance requests
|
||||
if (~url.indexOf(settings.fediact_homeinstance) && settings.fediact_enabledelay) {
|
||||
// get current time
|
||||
var currenttime = Date.now()
|
||||
// get difference of current time and time of last request
|
||||
var difference = currenttime - tmpSettings.lasthomerequest
|
||||
// if difference is smaller than our set api delay value...
|
||||
if (difference < apiDelay) {
|
||||
// ... then wait the time required to reach the api delay value...
|
||||
await new Promise(resolve => {
|
||||
setTimeout(function() {
|
||||
resolve()
|
||||
}, apiDelay-difference)
|
||||
})
|
||||
}
|
||||
// TODO: move this to the top? or get new Date.now() here?
|
||||
tmpSettings.lasthomerequest = currenttime
|
||||
}
|
||||
try {
|
||||
await chrome.runtime.sendMessage({requestdata: [method, url, extraheaders, jsonbody]}, function(response) {
|
||||
if(response) {
|
||||
|
@ -209,37 +209,37 @@ async function executeAction(data, action, polldata) {
|
|||
case 'copy':
|
||||
// special action. only copy to clipboard and return
|
||||
navigator.clipboard.writeText(data)
|
||||
return
|
||||
return true
|
||||
case 'domainblock':
|
||||
requestUrl = 'https://' + settings.fediact_homeinstance + domainBlocksApi + "?domain=" + data
|
||||
condition = function(response) {if(response){return true}}
|
||||
after = function() {updateMutedBlocked()}
|
||||
after = async function() {await updateMutedBlocked()}
|
||||
break
|
||||
case 'domainunblock':
|
||||
requestUrl = 'https://' + settings.fediact_homeinstance + domainBlocksApi + "?domain=" + data
|
||||
condition = function(response) {if(response){return true}}
|
||||
method = "DELETE"
|
||||
after = function() {updateMutedBlocked()}
|
||||
after = async function() {await updateMutedBlocked()}
|
||||
break
|
||||
case 'mute':
|
||||
requestUrl = 'https://' + settings.fediact_homeinstance + accountsApi + "/" + data + "/mute"
|
||||
condition = function(response) {return response.muting}
|
||||
after = function() {updateMutedBlocked()}
|
||||
after = async function() {await updateMutedBlocked()}
|
||||
break
|
||||
case 'unmute':
|
||||
requestUrl = 'https://' + settings.fediact_homeinstance + accountsApi + "/" + data + "/unmute"
|
||||
condition = function(response) {return !response.muting}
|
||||
after = function() {updateMutedBlocked()}
|
||||
after = async function() {await updateMutedBlocked()}
|
||||
break
|
||||
case 'block':
|
||||
requestUrl = 'https://' + settings.fediact_homeinstance + accountsApi + "/" + data + "/block"
|
||||
condition = function(response) {return response.blocking}
|
||||
after = function() {updateMutedBlocked()}
|
||||
after = async function() {await updateMutedBlocked()}
|
||||
break
|
||||
case 'unblock':
|
||||
requestUrl = 'https://' + settings.fediact_homeinstance + accountsApi + "/" + data + "/unblock"
|
||||
condition = function(response) {return !response.blocking}
|
||||
after = function() {updateMutedBlocked()}
|
||||
after = async function() {await updateMutedBlocked()}
|
||||
break
|
||||
case 'vote':
|
||||
requestUrl = 'https://' + settings.fediact_homeinstance + pollsApi + "/" + data + "/votes"
|
||||
|
@ -279,7 +279,8 @@ async function executeAction(data, action, polldata) {
|
|||
condition = function(response) {return !response.bookmarked}
|
||||
break
|
||||
default:
|
||||
log("No valid action specified."); break
|
||||
log("No valid action specified.")
|
||||
return
|
||||
}
|
||||
if (requestUrl) {
|
||||
var response = await makeRequest(method, requestUrl, tmpSettings.tokenheader, jsonbody)
|
||||
|
@ -288,7 +289,7 @@ async function executeAction(data, action, polldata) {
|
|||
response = JSON.parse(response)
|
||||
if (condition(response)) {
|
||||
if (after !== undefined) {
|
||||
after()
|
||||
await after()
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
@ -497,12 +498,17 @@ function addToProcessedToots(toot) {
|
|||
return tmpSettings.processed.length - 1
|
||||
}
|
||||
|
||||
async function updateMutedBlocked() {
|
||||
var res = await new Promise(async function(resolve) {
|
||||
function updateMutedBlocked() {
|
||||
return new Promise(async function(resolve) {
|
||||
try {
|
||||
await chrome.runtime.sendMessage({updatemutedblocked: true}, function(response) {
|
||||
await chrome.runtime.sendMessage({updatemutedblocked: true}, async function(response) {
|
||||
if (response) {
|
||||
resolve(response)
|
||||
if (!await getSettings()) {
|
||||
// but reload if settings are invalid
|
||||
location.reload()
|
||||
} else {
|
||||
resolve(true)
|
||||
}
|
||||
} else {
|
||||
resolve(false)
|
||||
}
|
||||
|
@ -514,12 +520,6 @@ async function updateMutedBlocked() {
|
|||
location.reload()
|
||||
}
|
||||
})
|
||||
if (res) {
|
||||
if (!await getSettings()) {
|
||||
// but reload if settings are invalid
|
||||
location.reload()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function showModal(settings) {
|
||||
|
@ -533,13 +533,17 @@ function showModal(settings) {
|
|||
$("body").append($(baseEl))
|
||||
async function handleModalEvent(e) {
|
||||
if (e.originalEvent.isTrusted) {
|
||||
if ($(e.target).is(".fediactmodal li, .fediactmodal li a")) {
|
||||
if ($(e.target).is(".fediactmodal li")) {
|
||||
e.target = $(e.target).find("a")
|
||||
if ($(e.target).is(".fediactmodal li, .fediactmodal li *")) {
|
||||
if (!$(e.target).is(".fediactmodal li a")) {
|
||||
if ($(e.target).find("a").length) {
|
||||
e.target = $(e.target).find("a")
|
||||
} else {
|
||||
e.target = $(e.target).closest("a")
|
||||
}
|
||||
}
|
||||
var action = $(e.target).attr("fediactaction")
|
||||
var data = $(e.target).attr("fediactdata")
|
||||
var done = executeAction(data, action, null)
|
||||
var done = await executeAction(data, action, null)
|
||||
if (done) {
|
||||
$(e.target).addClass("activated")
|
||||
$(e.target).append("<span>Done!</span>")
|
||||
|
@ -566,6 +570,8 @@ function showModal(settings) {
|
|||
$(baseEl).remove()
|
||||
$("body").off("click", handleModalEvent)
|
||||
}
|
||||
} else if ($(e.target).is(".fediactmodalinner")) {
|
||||
$.noop()
|
||||
} else {
|
||||
$(baseEl).remove()
|
||||
$("body").off("click", handleModalEvent)
|
||||
|
@ -1532,6 +1538,7 @@ function getSettings() {
|
|||
} catch(e) {
|
||||
log(e)
|
||||
resolve(false)
|
||||
return
|
||||
}
|
||||
if (settings) {
|
||||
// validate settings
|
||||
|
|
File diff suppressed because one or more lines are too long
Ładowanie…
Reference in New Issue