Skip to content

Commit

Permalink
move all requests to background, support polls behind CWs, add import…
Browse files Browse the repository at this point in the history
…ant to some CSS
  • Loading branch information
lartsch committed Dec 21, 2022
1 parent 7d77407 commit 379b675
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 89 deletions.
2 changes: 1 addition & 1 deletion firefox/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "FediAct",
"version": "0.9.8.3",
"version": "0.9.8.4",
"description": "Simplifies interactions on other Mastodon instances than your own. Visit https://github.com/lartsch/FediAct for more.",
"manifest_version": 2,
"content_scripts": [
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "FediAct",
"version": "0.9.8.3",
"version": "0.9.8.4",
"description": "Simplifies interactions on other Mastodon instances than your own. Visit https://github.com/lartsch/FediAct for more.",
"manifest_version": 3,
"content_scripts": [
Expand Down
60 changes: 55 additions & 5 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ const mutesApi = "/api/v1/mutes"
const blocksApi = "/api/v1/blocks"
const domainBlocksApi = "/api/v1/domain_blocks"
const timeout = 15000

const tokenRegex = /"access_token":".*?",/gm

// required settings keys with defauls
const settingsDefaults = {
fediact_homeinstance: null
Expand All @@ -22,7 +20,7 @@ function log(text) {
}

// get redirect url (it will be the url on the toot authors home instance)
async function resolveToot(url) {
async function resolveExternalTootHome(url) {
return new Promise(async function(resolve) {
try {
const controller = new AbortController()
Expand All @@ -44,6 +42,49 @@ async function resolveToot(url) {
})
}

// get redirect url (it will be the url on the toot authors home instance)
async function generalRequest(data) {
return new Promise(async function(resolve) {
try {
const controller = new AbortController()
const timeoutId = setTimeout(() => {
log("Timed out")
controller.abort()
}, timeout)
if (data[3]) {
data[2]["Content-Type"] = "application/json"
var res = await fetch(data[1], {
method: data[0],
signal: controller.signal,
headers: data[2],
body: JSON.stringify(data[3])
})
} else if (data[2]) {
var res = await fetch(data[1], {
method: data[0],
signal: controller.signal,
headers: data[2]
})
} else {
var res = await fetch(data[1], {
method: data[0],
signal: controller.signal
})
}
clearTimeout(timeoutId)
if (res.status >= 200 && res.status < 300) {
var restext = await res.text()
resolve(restext)
} else {
resolve(false)
}
} catch(e) {
log(e)
resolve(false)
}
})
}

// fetch API token here (will use logged in session automatically)
async function fetchBearerToken() {
return new Promise(async function(resolve) {
Expand Down Expand Up @@ -155,15 +196,24 @@ chrome.alarms.onAlarm.addListener(fetchData)
// different listeners for inter-script communication
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
// the content script gave us an url to perform a 302 redirect with
if(request.url) {
resolveToot(request.url).then(sendResponse)
if(request.externaltoot) {
resolveExternalTootHome(request.externaltoot).then(sendResponse)
return true
}
// the content script gave us an url to perform a 302 redirect with
if(request.requestdata) {
generalRequest(request.requestdata).then(sendResponse)
return true
}
// immediately fetch api token after settings are updated
if (request.updatedsettings) {
fetchData().then(reloadListeningScripts)
return true
}
if (request.updatemutedblocked) {
fetchMutesAndBlocks().then((browser || chrome).storage.local.set(settings)).then(sendResponse)
return true
}
// when the content script starts to process on a site, listen for tab changes (url)
if (request.running) {
chrome.tabs.onUpdated.addListener(async function(tabId, changeInfo, tab) {
Expand Down
2 changes: 1 addition & 1 deletion src/background.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/content_styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@
}

.fediactvoted > a {
font-weight: bold;
color: orange
font-weight: bold !important;
color: orange !important;
}

/* Inserted in the bottom right of any external instance where FediAct is running */
Expand Down
Loading

0 comments on commit 379b675

Please sign in to comment.