Skip to content

Commit

Permalink
add loggedin check, fix bug with v3, improve v3 support, disable log
Browse files Browse the repository at this point in the history
  • Loading branch information
lartsch committed Dec 14, 2022
1 parent 21113d5 commit db33da9
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 12 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.6.1",
"version": "0.9.6.2",
"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.6.1",
"version": "0.9.6.2",
"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
50 changes: 43 additions & 7 deletions src/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -75,6 +75,28 @@ function log(text) {
jQuery.fn.onAppear = jQuery.fn.DOMNodeAppear
})(jQuery)

// for checking if logged in on an external instance
function isLoggedIn() {
return new Promise(function(resolve) {
if ($(document).find("script#initial-state").length) {
var initialState = $(document).find("script#initial-state").first()
var stateJson = JSON.parse($(initialState).text())
if (stateJson.meta.access_token) {
resolve(true)
}
} else {
$(document).DOMNodeAppear(function(e) {
var initialState = $(e.target)
var stateJson = JSON.parse($(initialState).text())
if (stateJson.meta.access_token) {
resolve(true)
}
}, "script#initial-state")
}
resolve(false)
})
}

// extract given url parameter value
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = window.location.search.substring(1),
Expand Down Expand Up @@ -505,6 +527,16 @@ async function processToots() {
// otherwise do the same for any closest article or div with the data-id attribute
} else if ($(el).closest("article[data-id], div[data-id]").length) {
return $(el).closest("article[data-id], div[data-id]").first().attr("data-id").split("-").slice(-1)[0]
} else if ($(el).find("a.icon-button:has(i.fa-star), a.detailed-status__link:has(i.fa-star)").length) {
var hrefEl = $(el).find("a.icon-button:has(i.fa-star), a.detailed-status__link:has(i.fa-star)").first()
if ($(hrefEl).attr("href")) {
var hrefAttr = $(hrefEl).attr("href")
if (~hrefAttr.indexOf("interact/")) {
var splitted = hrefAttr.split("?")[0].split("/")
var lastpart = splitted.pop() || splitted.pop()
return lastpart
}
}
}
}
// check elements that can contain an href (either resolved external link or internal reference)
Expand All @@ -517,6 +549,7 @@ async function processToots() {
} else if ($(el).find("a.modal-button").length) {
return tootHrefCheck($(el).find("a.modal-button").first().attr("href").split("?")[0])
}
return [false,undefined]
}
// check toot author, mentions and toot prepend mentions for applying mutes
function processMutes(el, tootAuthor) {
Expand All @@ -534,7 +567,7 @@ async function processToots() {
hrefs.push($(this).find("a").attr("href").split("?")[0])
})
var processedHrefs = []
for (href of hrefs) {
for (var href of hrefs) {
var splitted = href.split("/")
var lastpart = splitted.pop() || splitted.pop()
lastpart = lastpart.slice(1)
Expand Down Expand Up @@ -562,7 +595,7 @@ async function processToots() {
// main function to process each detected toot element
async function process(el) {
// extra step for detailed status elements to select the correct parent
if ($(el).is("div.detailed-status")) {
if ($(el).is("div.detailed-status") && $(el).closest("div.focusable").length) {
el = $(el).closest("div.focusable")
}
// get toot data
Expand All @@ -582,8 +615,8 @@ async function processToots() {
// check if id is already cached
var cacheIndex = isInProcessedToots(internalIdentifier)
// get all button elements of this toot
var favButton = $(el).find("button:has(i.fa-star), a.icon-button:has(i.fa-star)").first()
var boostButton = $(el).find("button:has(i.fa-retweet), a.icon-button:has(i.fa-retweet)").first()
var favButton = $(el).find("button:has(i.fa-star), a.icon-button:has(i.fa-star), a.detailed-status__link:has(i.fa-star)").first()
var boostButton = $(el).find("button:has(i.fa-retweet), a.icon-button:has(i.fa-retweet), a.detailed-status__link:has(i.fa-retweet)").first()
var bookmarkButton = $(el).find("button:has(i.fa-bookmark)").first()
var replyButton = $(el).find("button:has(i.fa-reply), button:has(i.fa-reply-all), a.icon-button:has(i.fa-reply), a.icon-button:has(i.fa-reply-all)").first()
// handles process when a button is clicked
Expand Down Expand Up @@ -1067,10 +1100,13 @@ async function checkSite() {
} else {
settings.fediact_exturi = uri
}
return true
if (await isLoggedIn()) {
log("Already logged in to this external instance.")
} else {
return true
}
}
}
log("Does not look like a Mastodon instance.")
return false
}

Expand Down
Loading

0 comments on commit db33da9

Please sign in to comment.