From 478a2b1101aa69ff30231eb048fe736ed1442e5a Mon Sep 17 00:00:00 2001 From: lartsch Date: Sun, 11 Dec 2022 09:55:09 -0500 Subject: [PATCH] attempt to fix element not being detected on v3 (caching issue, el. already on page on load so domnodeappear does not work) --- firefox/manifest.json | 2 +- manifest.json | 2 +- src/inject.js | 126 +++++++++++++++++++++++++----------------- src/inject.min.js | 2 +- 4 files changed, 79 insertions(+), 53 deletions(-) diff --git a/firefox/manifest.json b/firefox/manifest.json index da2cf91..3554267 100644 --- a/firefox/manifest.json +++ b/firefox/manifest.json @@ -1,6 +1,6 @@ { "name": "FediAct", - "version": "0.9.5.2", + "version": "0.9.5.3", "description": "Simplifies interactions on other Mastodon instances than your own. Visit https://github.com/lartsch/FediFollow-Chrome for more.", "manifest_version": 2, "content_scripts": [ diff --git a/manifest.json b/manifest.json index 97281ff..3975a3f 100644 --- a/manifest.json +++ b/manifest.json @@ -1,6 +1,6 @@ { "name": "FediAct", - "version": "0.9.5.2", + "version": "0.9.5.3", "description": "Simplifies interactions on other Mastodon instances than your own. Visit https://github.com/lartsch/FediFollow-Chrome for more.", "manifest_version": 3, "content_scripts": [ diff --git a/src/inject.js b/src/inject.js index c84fa84..fb0cca6 100644 --- a/src/inject.js +++ b/src/inject.js @@ -40,7 +40,7 @@ const settingsDefaults = { var browser, chrome, lasthomerequest, fedireply // currently, the only reliable way to detect all toots etc. has the drawback that the same element could be processed multiple times // this will store already processed elements to compare prior to processing and will reset as soon as the site context changes -var processed = [] +var [processed, processedFollow, isProcessing] = [[],[],[]] // =-=-=-=-==-=-=-=-==-=-=-=-=- @@ -832,6 +832,7 @@ async function processToots() { initStyles([internalIdentifier, false]) } } else { + log("ALREADY PROCESSED") // the toot is already in cache, so grab it var toot = processed[cacheIndex] // init stylings @@ -847,8 +848,18 @@ async function processToots() { } // One DOMNodeAppear to rule them all $(document).DOMNodeAppear(async function(e) { - process($(e.target)) + if (!isProcessing.includes($(e.target).get(0))) { + isProcessing.push($(e.target).get(0)) + process($(e.target)) + } }, "div.status, div.detailed-status") + // try to find all existing elements (fixes some elements not being detected by DOMNodeAppear in rare cases, esp. v3) + $(document).find("div.status, div.detailed-status").each(function(){ + if (!isProcessing.includes($(this).get(0))) { + isProcessing.push($(this).get(0)) + process($(this)) + } + }) } // main function to listen for the follow button pressed and open a new tab with the home instance @@ -892,55 +903,58 @@ async function processFollow() { } // do we have a full handle? if (fullHandle) { - // yes, so resolve it to a user id on our homeinstance - var resolvedHandle = await resolveHandleToHome(fullHandle) - if (resolvedHandle) { - // successfully resolved - // if showfollows is enabled... - if (settings.fediact_showfollows) { - // ... then check if user is already following - var isFollowing = await isFollowingHomeInstance([resolvedHandle[0]]) - // update button text and action if already following - if (isFollowing[0]) { - $(el).text("Unfollow") - action = "unfollow" - } - } - // single and double click handling (see toot processing for explanation, is the same basically) - var clicks = 0 - var timer - $(el).on("click", async function(e) { - // prevent default and immediate propagation - e.preventDefault() - e.stopImmediatePropagation() - clicks++ - if (clicks == 1) { - timer = setTimeout(async function() { - execFollow(resolvedHandle[0]) - clicks = 0 - }, 350) - } else { - clearTimeout(timer) - var done = await execFollow(resolvedHandle[0]) - if (done) { - var saveText = $(el).text() - var redirectUrl = 'https://' + settings.fediact_homeinstance + '/@' + resolvedHandle[1] - $(el).text("Redirecting...") - setTimeout(function() { - redirectTo(redirectUrl) - $(el).text(saveText) - }, 1000) - } else { - log("Action failed.") + if (!processedFollow.includes(fullHandle)) { + // yes, so resolve it to a user id on our homeinstance + var resolvedHandle = await resolveHandleToHome(fullHandle) + if (resolvedHandle) { + processedFollow.push(fullHandle) + // successfully resolved + // if showfollows is enabled... + if (settings.fediact_showfollows) { + // ... then check if user is already following + var isFollowing = await isFollowingHomeInstance([resolvedHandle[0]]) + // update button text and action if already following + if (isFollowing[0]) { + $(el).text("Unfollow") + action = "unfollow" } - clicks = 0 } - }).on("dblclick", function(e) { - e.preventDefault() - e.stopImmediatePropagation() - }) - } else { - log("Could not resolve user home ID.") + // single and double click handling (see toot processing for explanation, is the same basically) + var clicks = 0 + var timer + $(el).on("click", async function(e) { + // prevent default and immediate propagation + e.preventDefault() + e.stopImmediatePropagation() + clicks++ + if (clicks == 1) { + timer = setTimeout(async function() { + execFollow(resolvedHandle[0]) + clicks = 0 + }, 350) + } else { + clearTimeout(timer) + var done = await execFollow(resolvedHandle[0]) + if (done) { + var saveText = $(el).text() + var redirectUrl = 'https://' + settings.fediact_homeinstance + '/@' + resolvedHandle[1] + $(el).text("Redirecting...") + setTimeout(function() { + redirectTo(redirectUrl) + $(el).text(saveText) + }, 1000) + } else { + log("Action failed.") + } + clicks = 0 + } + }).on("dblclick", function(e) { + e.preventDefault() + e.stopImmediatePropagation() + }) + } else { + log("Could not resolve user home ID.") + } } } } @@ -948,8 +962,18 @@ async function processFollow() { var allFollowPaths = followButtonPaths.join(",") // one domnodeappear to rule them all $(document).DOMNodeAppear(async function(e) { - process($(e.target)) + if (!isProcessing.includes($(e.target).get(0))) { + isProcessing.push($(e.target).get(0)) + process($(e.target)) + } }, allFollowPaths) + // try to find all existing elements (fixes some elements not being detected by DOMNodeAppear in rare cases, esp. v3) + $(document).find(allFollowPaths).each(function(){ + if (!isProcessing.includes($(this).get(0))) { + isProcessing.push($(this).get(0)) + process($(this)) + } + }) } @@ -1057,6 +1081,8 @@ async function backgroundProcessor() { if (request.urlchanged) { // reset already processed elements processed = [] + processedFollow = [] + isProcessing = [] // rerun getSettings to keep mutes/blocks up to date while not reloading the page if (!await getSettings()) { // but reload if settings are invalid diff --git a/src/inject.min.js b/src/inject.min.js index 38d13d6..587e0ab 100644 --- a/src/inject.min.js +++ b/src/inject.min.js @@ -1 +1 @@ -const e=["div.account__header button.logo-button","div.public-account-header a.logo-button","div.account-card a.logo-button","div.directory-card a.icon-button","div.detailed-status a.logo-button"],c=["div.account__header__tabs__name small","div.public-account-header__tabs__name small","div.detailed-status span.display-name__account","div.display-name > span"],i=/^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$/,R=/^(?https?:\/\/(?:\.?[a-z0-9-]+)+(?:\.[a-z]+){1})?\/?@(?\w+)(?:@(?(?:[\w-]+\.)+?\w+))?(?:\/(?\d+))?\/?$/,D=/^(?https?:\/\/(?:\.?[a-z0-9-]+)+(?:\.[a-z]+){1})(?:\/users\/)(?\w+)(?:(?:\/statuses\/)(?\d+))?\/?$/,a=!0,n="[FediAct]",y=200,o="/api/v1/instance",r="/api/v1/statuses",s="/api/v2/search",d="/api/v1/accounts",l=500,u=200;var browser,chrome,f,h,N={};const t={fediact_homeinstance:null,fediact_alert:!1,fediact_mode:"blacklist",fediact_whitelist:null,fediact_blacklist:null,fediact_target:"_self",fediact_autoaction:!0,fediact_token:null,fediact_showfollows:!0,fediact_redirects:!0,fediact_enabledelay:!0,fediact_hidemuted:!1,fediact_mutesblocks:[],fediact_domainblocks:[]};var I=[];function M(t){a&&console.log(n+" "+t)}!function(i){i.fn.DOMNodeAppear=function(e,a){if(!a)return!1;i(document).on("animationstart webkitAnimationStart oanimationstart MSAnimationStart",function(t){"nodeInserted"==t.originalEvent.animationName&&i(t.target).is(a)&&"function"==typeof e&&e(t)})},jQuery.fn.onAppear=jQuery.fn.DOMNodeAppear}(jQuery);var p=function(t){for(var e,a=window.location.search.substring(1).split("&"),i=0;i{setTimeout(function(){t()},l-e)}),f=t),new Promise(function(t){let e=new XMLHttpRequest;if(e.open(i,n),e.timeout=3e3,o)for(var a in o)e.setRequestHeader(a,o[a]);e.onload=function(){200<=this.status&&this.status<300?t(e.responseText):t(!1)},e.onerror=function(){M("Request to "+n+" failed."),t(!1)},e.send()})}function g(t){return t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function v(t,e,a){return t.replace(new RegExp(g(e),"g"),a)}function P(t){var e;N.fediact_redirects?(N.fediact_alert&&alert("Redirecting..."),e=window.open(t,N.fediact_target),M("Redirected to "+t),e?e.focus():M("Could not open new window. Please allow popups for this website.")):M("Redirects disabled.")}async function S(t,e){var a,i;switch(e){case"follow":a="https://"+N.fediact_homeinstance+d+"/"+t+"/follow",i=function(t){return t.following||t.requested};break;case"boost":a="https://"+N.fediact_homeinstance+r+"/"+t+"/reblog",i=function(t){return t.reblogged};break;case"favourite":a="https://"+N.fediact_homeinstance+r+"/"+t+"/favourite",i=function(t){return t.favourited};break;case"bookmark":a="https://"+N.fediact_homeinstance+r+"/"+t+"/bookmark",i=function(t){return t.bookmarked};break;case"unfollow":a="https://"+N.fediact_homeinstance+d+"/"+t+"/unfollow",i=function(t){return!t.following&&!t.requested};break;case"unboost":a="https://"+N.fediact_homeinstance+r+"/"+t+"/unreblog",i=function(t){return!t.reblogged};break;case"unfavourite":a="https://"+N.fediact_homeinstance+r+"/"+t+"/unfavourite",i=function(t){return!t.favourited};break;case"unbookmark":a="https://"+N.fediact_homeinstance+r+"/"+t+"/unbookmark",i=function(t){return!t.bookmarked};break;default:M("No valid action specified.")}if(a){var n=await m("POST",a,N.tokenheader);if(n&&i(JSON.parse(n)))return!0}M(e+" action failed.")}async function b(t){var e="https://"+N.fediact_homeinstance+d+"/relationships?";for(const o of t)e+="id[]="+o.toString()+"&";var a=await m("GET",e,N.tokenheader),i=Array(t.length).fill(!1);if(a)for(var a=JSON.parse(a),n=0;nN.fediact_mutesblocks.includes(t))||E(e)||n.some(t=>N.fediact_domainblocks.includes(t.split("@")[1]))?($(t).hide(),a&&$(a).hide(),1):void 0}}(e,t)){i=$(e);var a=$(i).is(".detailed-status__wrapper")?(a=window.location.href.split("?")[0].split("/")).pop()||a.pop():$(i).attr("data-id")?$(i).attr("data-id").split("-").slice(-1)[0]:$(i).closest("article[data-id], div[data-id]").length?$(i).closest("article[data-id], div[data-id]").first().attr("data-id").split("-").slice(-1)[0]:void 0,[i,n]=(i=$(e),$(i).find("a.status__relative-time").length?O($(i).find("a.status__relative-time").first().attr("href").split("?")[0]):$(i).find("a.detailed-status__datetime").length?O($(i).find("a.detailed-status__datetime").first().attr("href").split("?")[0]):$(i).find("a.modal-button").length?O($(i).find("a.modal-button").first().attr("href").split("?")[0]):void 0),o=a||n;if(o){var r=[],s=j(o),c=$(e).find("button:has(i.fa-star), a.icon-button:has(i.fa-star)").first(),d=$(e).find("button:has(i.fa-retweet), a.icon-button:has(i.fa-retweet)").first(),l=$(e).find("button:has(i.fa-bookmark)").first(),u=$(e).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();async function f(t,e){if(!N.fediact_autoaction)return M("Auto-action disabled."),!0;i=e,a=!1,$(i.currentTarget).children("i.fa-retweet").length?a=$(i.currentTarget).children("i.fa-retweet").hasClass("fediactive")?"unboost":"boost":$(i.currentTarget).children("i.fa-star").length?a=$(i.currentTarget).hasClass("fediactive")?"unfavourite":"favourite":$(i.currentTarget).children("i.fa-bookmark").length?a=$(i.currentTarget).hasClass("fediactive")?"unbookmark":"bookmark":$(i.currentTarget).attr("href")&&(~$(i.currentTarget).attr("href").indexOf("type=reblog")?a=$(i.currentTarget).hasClass("fediactive")?"unboost":"boost":~$(i.currentTarget).attr("href").indexOf("type=favourite")&&(a=$(i.currentTarget).hasClass("fediactive")?"unfavourite":"favourite"));var a,i=a;if(i){if(await S(t,i))return"boost"==i||"unboost"==i?(U($(e.currentTarget).find("i"),[["color","!remove","rgb(140, 141, 255)"],["transition-duration","!remove","0.9s"],["background-position","!remove","0px 100%"]],"fediactive"),s&&(I[s][3]=!I[s][3])):"favourite"==i||"unfavourite"==i?(U($(e.currentTarget),[["color","!remove","rgb(202, 143, 4)"]],"fediactive"),s&&(I[s][4]=!I[s][4])):(U($(e.currentTarget),[["color","!remove","rgb(255, 80, 80)"]],"fediactive"),s&&(I[s][5]=!I[s][5])),!0;M("Could not execute action on home instance.")}else M("Could not determine action.")}function h(t){$(e).find(".feditriggered").remove(),t[1]?($(l).removeClass("disabled").removeAttr("disabled"),t[4]&&!$(c).hasClass("fediactive")&&U($(c),[["color","!remove","rgb(202, 143, 4)"]],"fediactive"),t[3]&&!$(d).find("i.fediactive").length&&U($(d).find("i"),[["color","!remove","rgb(140, 141, 255)"],["transition-duration","!remove","0.9s"],["background-position","!remove","0px 100%"]],"fediactive"),t[5]&&!$(l).hasClass("fediactive")&&U($(l),[["color","!remove","rgb(255, 80, 80)"]],"fediactive")):$("Unresolved").insertAfter($(c))}function p(i){$(u).on("click",function(t){t.preventDefault(),t.stopImmediatePropagation(),P(i[6]+"?fedireply")}),$([c,d,l]).each(function(){var e,a=0;$(this).on("click",async function(t){t.preventDefault(),t.stopImmediatePropagation(),1==++a?e=setTimeout(async function(){await f(i[2],t)||M("Action failed."),a=0},350):(clearTimeout(e),await f(i[2],t)?P(i[6]):M("Action failed."),a=0)}).on("dblclick",function(t){t.preventDefault(),t.stopImmediatePropagation()})})}if(s){var m=I[s];h(m),m[1]&&p(m)}else{if(i&&r.push(n),t){var g,v,b,_=t.match(R),[w,k]=[!1,!1],y=(!_.groups.handledomain||~location.hostname.indexOf(_.groups.handledomain)||(w=!0),[a]);i||y.push(n);for(g of y=y.filter((t,e)=>void 0!==t&&y.indexOf(t)==e))w?k||((v=await q(location.protocol+"//"+location.hostname+"/"+t+"/"+g))&&(k=!0,r.push(v),D.test(v)?(b=v.match(D)).groups.handle&&b.groups.tootid&&b.groups.domain&&r.push(b.groups.domain+"/@"+b.groups.handle+"/"+b.groups.tootid):R.test(v)&&(b=v.match(R)).groups.handle&&b.groups.tootid&&b.groups.domain&&r.push(b.groups.domain+"/users/"+b.groups.handle+"/statuses/"+b.groups.tootid)),r.push(location.protocol+"//"+location.hostname+"/"+t+"/"+g)):(r.push(location.protocol+"//"+location.hostname+"/users/"+_.groups.handle+"/statuses/"+g),r.push(location.protocol+"//"+location.hostname+"/@"+_.groups.handle+"/"+g))}if(r.length){var x,T,A,C=!1;for(x of r=r.filter((t,e)=>r.indexOf(t)==e))C||(T=await z(x))&&(C=!0,A="https://"+N.fediact_homeinstance+"/@"+T[0]+"/"+T[1],fullEntry=[o,...T,A,!0]);C?(J(fullEntry),p(fullEntry),h(fullEntry)):(M("Failed to resolve: "+r),J([o,!1]),h([o,!1]))}else M("Could not identify a post URI for home resolving."),J([o,!1]),h([o,!1])}}else M("Could not get toot data.")}}$(document).DOMNodeAppear(async function(t){e($(t.target))},"div.status, div.detailed-status")}async function C(){var t=e.join(",");$(document).DOMNodeAppear(async function(t){!async function(i){var t,n,o,r,e="follow";async function s(t){return N.fediact_autoaction?(t=await S(t,e),"follow"==e&&t?($(i).text("Unfollow"),e="unfollow",!0):"unfollow"==e&&t?($(i).text("Follow"),e="follow",!0):void 0):(M("Auto-action disabled."),!0)}if($(i).closest("div.account-card").length)t=$(i).closest("div.account-card").find("div.display-name > span").text().trim();else for(const a of c)if($(a).length){t=$(a).text().trim();break}t&&((n=await _(t))?(N.fediact_showfollows&&(await b([n[0]]))[0]&&($(i).text("Unfollow"),e="unfollow"),o=0,$(i).on("click",async function(t){var e,a;t.preventDefault(),t.stopImmediatePropagation(),1==++o?r=setTimeout(async function(){s(n[0]),o=0},350):(clearTimeout(r),await s(n[0])?(e=$(i).text(),a="https://"+N.fediact_homeinstance+"/@"+n[1],$(i).text("Redirecting..."),setTimeout(function(){P(a),$(i).text(e)},1e3)):M("Action failed."),o=0)}).on("dblclick",function(t){t.preventDefault(),t.stopImmediatePropagation()})):M("Could not resolve user home ID."))}($(t.target))},t)}function w(t){var e,a=[];for(e of t.split(/\r?\n/))(e=e.trim()).length&&(i.test(e)?a.push(e):M("Removed invalid domain "+e+" from blacklist/whitelist."));return[...new Set(a)]}function O(){if(null==N.fediact_homeinstance||!N.fediact_homeinstance)return M("Mastodon home instance is not set."),!1;if(!N.fediact_token)return M("No API token available. Are you logged in to your home instance? If yes, wait for 1-2 minutes and reload page."),!1;if(N.tokenheader={Authorization:"Bearer "+N.fediact_token},!i.test(N.fediact_homeinstance))return M("Instance setting is not a valid domain name."),!1;if("whitelist"==N.fediact_mode){if(N.fediact_whitelist=w(N.fediact_whitelist),N.fediact_whitelist.length<1)return M("Whitelist is empty or invalid."),!1}else N.fediact_blacklist=w(N.fediact_blacklist);return!0}async function F(){if(location.hostname!=N.fediact_homeinstance||(h=p("fedireply"))){if("whitelist"==N.fediact_mode){if($.inArray(location.hostname,N.fediact_whitelist)<0)return M("Current site is not in whitelist."),!1}else if(-1<$.inArray(location.hostname,N.fediact_blacklist))return M("Current site is in blacklist."),!1;var t=await m("GET",location.protocol+"//"+location.hostname+o,null);if(t){t=JSON.parse(t).uri;if(t)return N.fediact_exturi=t,!0}M("Does not look like a Mastodon instance.")}else M("Current site is your home instance.");return!1}async function G(){chrome.runtime.onMessage.addListener(async function(t,e,a){t.urlchanged&&(I=[],await k()||location.reload()),t.updatedfedisettings&&location.reload()});try{return await chrome.runtime.sendMessage({running:!0}),!0}catch(t){M(t)}return!1}function k(){return new Promise(async function(e){try{N=await(browser||chrome).storage.local.get(t)}catch(t){M(t),e(!1)}N&&O()?e(!0):e(!1)})}async function L(){await k()?await F()?h?T():G()?(C(),A()):M("Failed to initialize background script."):M("Will not process this site."):M("Could not load settings.")}L(); \ No newline at end of file +const a=["div.account__header button.logo-button","div.public-account-header a.logo-button","div.account-card a.logo-button","div.directory-card a.icon-button","div.detailed-status a.logo-button"],c=["div.account__header__tabs__name small","div.public-account-header__tabs__name small","div.detailed-status span.display-name__account","div.display-name > span"],i=/^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$/,R=/^(?https?:\/\/(?:\.?[a-z0-9-]+)+(?:\.[a-z]+){1})?\/?@(?\w+)(?:@(?(?:[\w-]+\.)+?\w+))?(?:\/(?\d+))?\/?$/,D=/^(?https?:\/\/(?:\.?[a-z0-9-]+)+(?:\.[a-z]+){1})(?:\/users\/)(?\w+)(?:(?:\/statuses\/)(?\d+))?\/?$/,e=!0,n="[FediAct]",T=200,o="/api/v1/instance",r="/api/v1/statuses",s="/api/v2/search",d="/api/v1/accounts",l=500,u=200;var browser,chrome,f,h,E={};const t={fediact_homeinstance:null,fediact_alert:!1,fediact_mode:"blacklist",fediact_whitelist:null,fediact_blacklist:null,fediact_target:"_self",fediact_autoaction:!0,fediact_token:null,fediact_showfollows:!0,fediact_redirects:!0,fediact_enabledelay:!0,fediact_hidemuted:!1,fediact_mutesblocks:[],fediact_domainblocks:[]};var[N,p,m]=[[],[],[]];function P(t){e&&console.log(n+" "+t)}!function(i){i.fn.DOMNodeAppear=function(e,a){if(!a)return!1;i(document).on("animationstart webkitAnimationStart oanimationstart MSAnimationStart",function(t){"nodeInserted"==t.originalEvent.animationName&&i(t.target).is(a)&&"function"==typeof e&&e(t)})},jQuery.fn.onAppear=jQuery.fn.DOMNodeAppear}(jQuery);var g=function(t){for(var e,a=window.location.search.substring(1).split("&"),i=0;i{setTimeout(function(){t()},l-e)}),f=t),new Promise(function(t){let e=new XMLHttpRequest;if(e.open(i,n),e.timeout=3e3,o)for(var a in o)e.setRequestHeader(a,o[a]);e.onload=function(){200<=this.status&&this.status<300?t(e.responseText):t(!1)},e.onerror=function(){P("Request to "+n+" failed."),t(!1)},e.send()})}function b(t){return t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function _(t,e,a){return t.replace(new RegExp(b(e),"g"),a)}function S(t){var e;E.fediact_redirects?(E.fediact_alert&&alert("Redirecting..."),e=window.open(t,E.fediact_target),P("Redirected to "+t),e?e.focus():P("Could not open new window. Please allow popups for this website.")):P("Redirects disabled.")}async function I(t,e){var a,i;switch(e){case"follow":a="https://"+E.fediact_homeinstance+d+"/"+t+"/follow",i=function(t){return t.following||t.requested};break;case"boost":a="https://"+E.fediact_homeinstance+r+"/"+t+"/reblog",i=function(t){return t.reblogged};break;case"favourite":a="https://"+E.fediact_homeinstance+r+"/"+t+"/favourite",i=function(t){return t.favourited};break;case"bookmark":a="https://"+E.fediact_homeinstance+r+"/"+t+"/bookmark",i=function(t){return t.bookmarked};break;case"unfollow":a="https://"+E.fediact_homeinstance+d+"/"+t+"/unfollow",i=function(t){return!t.following&&!t.requested};break;case"unboost":a="https://"+E.fediact_homeinstance+r+"/"+t+"/unreblog",i=function(t){return!t.reblogged};break;case"unfavourite":a="https://"+E.fediact_homeinstance+r+"/"+t+"/unfavourite",i=function(t){return!t.favourited};break;case"unbookmark":a="https://"+E.fediact_homeinstance+r+"/"+t+"/unbookmark",i=function(t){return!t.bookmarked};break;default:P("No valid action specified.")}if(a){var n=await v("POST",a,E.tokenheader);if(n&&i(JSON.parse(n)))return!0}P(e+" action failed.")}async function w(t){var e="https://"+E.fediact_homeinstance+d+"/relationships?";for(const o of t)e+="id[]="+o.toString()+"&";var a=await v("GET",e,E.tokenheader),i=Array(t.length).fill(!1);if(a)for(var a=JSON.parse(a),n=0;nE.fediact_mutesblocks.includes(t))||M(e)||n.some(t=>E.fediact_domainblocks.includes(t.split("@")[1]))?($(t).hide(),a&&$(a).hide(),1):void 0}}(e,t)){i=$(e);var a=$(i).is(".detailed-status__wrapper")?(a=window.location.href.split("?")[0].split("/")).pop()||a.pop():$(i).attr("data-id")?$(i).attr("data-id").split("-").slice(-1)[0]:$(i).closest("article[data-id], div[data-id]").length?$(i).closest("article[data-id], div[data-id]").first().attr("data-id").split("-").slice(-1)[0]:void 0,[i,n]=(i=$(e),$(i).find("a.status__relative-time").length?O($(i).find("a.status__relative-time").first().attr("href").split("?")[0]):$(i).find("a.detailed-status__datetime").length?O($(i).find("a.detailed-status__datetime").first().attr("href").split("?")[0]):$(i).find("a.modal-button").length?O($(i).find("a.modal-button").first().attr("href").split("?")[0]):void 0),o=a||n;if(o){var r=[],s=L(o),c=$(e).find("button:has(i.fa-star), a.icon-button:has(i.fa-star)").first(),d=$(e).find("button:has(i.fa-retweet), a.icon-button:has(i.fa-retweet)").first(),l=$(e).find("button:has(i.fa-bookmark)").first(),u=$(e).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();async function f(t,e){if(!E.fediact_autoaction)return P("Auto-action disabled."),!0;i=e,a=!1,$(i.currentTarget).children("i.fa-retweet").length?a=$(i.currentTarget).children("i.fa-retweet").hasClass("fediactive")?"unboost":"boost":$(i.currentTarget).children("i.fa-star").length?a=$(i.currentTarget).hasClass("fediactive")?"unfavourite":"favourite":$(i.currentTarget).children("i.fa-bookmark").length?a=$(i.currentTarget).hasClass("fediactive")?"unbookmark":"bookmark":$(i.currentTarget).attr("href")&&(~$(i.currentTarget).attr("href").indexOf("type=reblog")?a=$(i.currentTarget).hasClass("fediactive")?"unboost":"boost":~$(i.currentTarget).attr("href").indexOf("type=favourite")&&(a=$(i.currentTarget).hasClass("fediactive")?"unfavourite":"favourite"));var a,i=a;if(i){if(await I(t,i))return"boost"==i||"unboost"==i?(z($(e.currentTarget).find("i"),[["color","!remove","rgb(140, 141, 255)"],["transition-duration","!remove","0.9s"],["background-position","!remove","0px 100%"]],"fediactive"),s&&(N[s][3]=!N[s][3])):"favourite"==i||"unfavourite"==i?(z($(e.currentTarget),[["color","!remove","rgb(202, 143, 4)"]],"fediactive"),s&&(N[s][4]=!N[s][4])):(z($(e.currentTarget),[["color","!remove","rgb(255, 80, 80)"]],"fediactive"),s&&(N[s][5]=!N[s][5])),!0;P("Could not execute action on home instance.")}else P("Could not determine action.")}function h(t){$(e).find(".feditriggered").remove(),t[1]?($(l).removeClass("disabled").removeAttr("disabled"),t[4]&&!$(c).hasClass("fediactive")&&z($(c),[["color","!remove","rgb(202, 143, 4)"]],"fediactive"),t[3]&&!$(d).find("i.fediactive").length&&z($(d).find("i"),[["color","!remove","rgb(140, 141, 255)"],["transition-duration","!remove","0.9s"],["background-position","!remove","0px 100%"]],"fediactive"),t[5]&&!$(l).hasClass("fediactive")&&z($(l),[["color","!remove","rgb(255, 80, 80)"]],"fediactive")):$("Unresolved").insertAfter($(c))}function p(i){$(u).on("click",function(t){t.preventDefault(),t.stopImmediatePropagation(),S(i[6]+"?fedireply")}),$([c,d,l]).each(function(){var e,a=0;$(this).on("click",async function(t){t.preventDefault(),t.stopImmediatePropagation(),1==++a?e=setTimeout(async function(){await f(i[2],t)||P("Action failed."),a=0},350):(clearTimeout(e),await f(i[2],t)?S(i[6]):P("Action failed."),a=0)}).on("dblclick",function(t){t.preventDefault(),t.stopImmediatePropagation()})})}if(s){P("ALREADY PROCESSED");var m=N[s];h(m),m[1]&&p(m)}else{if(i&&r.push(n),t){var g,v,b,_=t.match(R),[w,k]=[!1,!1],y=(!_.groups.handledomain||~location.hostname.indexOf(_.groups.handledomain)||(w=!0),[a]);i||y.push(n);for(g of y=y.filter((t,e)=>void 0!==t&&y.indexOf(t)==e))w?k||((v=await J(location.protocol+"//"+location.hostname+"/"+t+"/"+g))&&(k=!0,r.push(v),D.test(v)?(b=v.match(D)).groups.handle&&b.groups.tootid&&b.groups.domain&&r.push(b.groups.domain+"/@"+b.groups.handle+"/"+b.groups.tootid):R.test(v)&&(b=v.match(R)).groups.handle&&b.groups.tootid&&b.groups.domain&&r.push(b.groups.domain+"/users/"+b.groups.handle+"/statuses/"+b.groups.tootid)),r.push(location.protocol+"//"+location.hostname+"/"+t+"/"+g)):(r.push(location.protocol+"//"+location.hostname+"/users/"+_.groups.handle+"/statuses/"+g),r.push(location.protocol+"//"+location.hostname+"/@"+_.groups.handle+"/"+g))}if(r.length){var x,T,A,C=!1;for(x of r=r.filter((t,e)=>r.indexOf(t)==e))C||(T=await U(x))&&(C=!0,A="https://"+E.fediact_homeinstance+"/@"+T[0]+"/"+T[1],fullEntry=[o,...T,A,!0]);C?(q(fullEntry),p(fullEntry),h(fullEntry)):(P("Failed to resolve: "+r),q([o,!1]),h([o,!1]))}else P("Could not identify a post URI for home resolving."),q([o,!1]),h([o,!1])}}else P("Could not get toot data.")}}$(document).DOMNodeAppear(async function(t){m.includes($(t.target).get(0))||(m.push($(t.target).get(0)),e($(t.target)))},"div.status, div.detailed-status"),$(document).find("div.status, div.detailed-status").each(function(){m.includes($(this).get(0))||(m.push($(this).get(0)),e($(this)))})}async function j(){async function e(i){var t,n,o,r,e="follow";async function s(t){return E.fediact_autoaction?(t=await I(t,e),"follow"==e&&t?($(i).text("Unfollow"),e="unfollow",!0):"unfollow"==e&&t?($(i).text("Follow"),e="follow",!0):void 0):(P("Auto-action disabled."),!0)}if($(i).closest("div.account-card").length)t=$(i).closest("div.account-card").find("div.display-name > span").text().trim();else for(const a of c)if($(a).length){t=$(a).text().trim();break}t&&!p.includes(t)&&((n=await k(t))?(p.push(t),E.fediact_showfollows&&(await w([n[0]]))[0]&&($(i).text("Unfollow"),e="unfollow"),o=0,$(i).on("click",async function(t){var e,a;t.preventDefault(),t.stopImmediatePropagation(),1==++o?r=setTimeout(async function(){s(n[0]),o=0},350):(clearTimeout(r),await s(n[0])?(e=$(i).text(),a="https://"+E.fediact_homeinstance+"/@"+n[1],$(i).text("Redirecting..."),setTimeout(function(){S(a),$(i).text(e)},1e3)):P("Action failed."),o=0)}).on("dblclick",function(t){t.preventDefault(),t.stopImmediatePropagation()})):P("Could not resolve user home ID."))}var t=a.join(",");$(document).DOMNodeAppear(async function(t){m.includes($(t.target).get(0))||(m.push($(t.target).get(0)),e($(t.target)))},t),$(document).find(t).each(function(){m.includes($(this).get(0))||(m.push($(this).get(0)),e($(this)))})}function y(t){var e,a=[];for(e of t.split(/\r?\n/))(e=e.trim()).length&&(i.test(e)?a.push(e):P("Removed invalid domain "+e+" from blacklist/whitelist."));return[...new Set(a)]}function F(){if(null==E.fediact_homeinstance||!E.fediact_homeinstance)return P("Mastodon home instance is not set."),!1;if(!E.fediact_token)return P("No API token available. Are you logged in to your home instance? If yes, wait for 1-2 minutes and reload page."),!1;if(E.tokenheader={Authorization:"Bearer "+E.fediact_token},!i.test(E.fediact_homeinstance))return P("Instance setting is not a valid domain name."),!1;if("whitelist"==E.fediact_mode){if(E.fediact_whitelist=y(E.fediact_whitelist),E.fediact_whitelist.length<1)return P("Whitelist is empty or invalid."),!1}else E.fediact_blacklist=y(E.fediact_blacklist);return!0}async function G(){if(location.hostname!=E.fediact_homeinstance||(h=g("fedireply"))){if("whitelist"==E.fediact_mode){if($.inArray(location.hostname,E.fediact_whitelist)<0)return P("Current site is not in whitelist."),!1}else if(-1<$.inArray(location.hostname,E.fediact_blacklist))return P("Current site is in blacklist."),!1;var t=await v("GET",location.protocol+"//"+location.hostname+o,null);if(t){t=JSON.parse(t).uri;if(t)return E.fediact_exturi=t,!0}P("Does not look like a Mastodon instance.")}else P("Current site is your home instance.");return!1}async function W(){chrome.runtime.onMessage.addListener(async function(t,e,a){t.urlchanged&&(N=[],p=[],m=[],await x()||location.reload()),t.updatedfedisettings&&location.reload()});try{return await chrome.runtime.sendMessage({running:!0}),!0}catch(t){P(t)}return!1}function x(){return new Promise(async function(e){try{E=await(browser||chrome).storage.local.get(t)}catch(t){P(t),e(!1)}E&&F()?e(!0):e(!1)})}async function Q(){await x()?await G()?h?C():W()?(j(),O()):P("Failed to initialize background script."):P("Will not process this site."):P("Could not load settings.")}Q(); \ No newline at end of file