const i=["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","button.remote-button","div.account__header button.button--follow"],u=["div.account__header__tabs__name small","div.public-account-header__tabs__name small","div.detailed-status span.display-name__account","div.display-name > span","a.user-screen-name","div.profile-info-panel small"],a=/^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$/,I=/^(?https?:\/\/(?:\.?[a-z0-9-]+)+(?:\.[a-z]+){1})?\/?@(?\w+)(?:@(?(?:[\w-]+\.)+?\w+))?(?:\/(?\d+))?\/?$/,q=/^(?https?:\/\/(?:\.?[a-z0-9-]+)+(?:\.[a-z]+){1})(?:\/users\/)(?\w+)(?:(?:\/statuses\/)(?\d+))?\/?$/,e=!1,n="[FediAct]",o="/api/v1/instance",d="/api/v1/statuses",s="/api/v2/search",l="/api/v1/accounts",r="/api/v1/mutes",c="/api/v1/blocks",f="/api/v1/domain_blocks",p="/api/v1/polls",h=500,m=200,k='
    ',w=15e3;var browser,chrome,M={};const y={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_redirects:!0,fediact_enabledelay:!0,fediact_hidemuted:!1,fediact_runifloggedin:!1,fediact_mutes:[],fediact_blocks:[],fediact_domainblocks:[]},z={fedireply:void 0,lasthomerequest:void 0,whitelist:void 0,blacklist:void 0,exturi:void 0,tokenheader:void 0,processed:[],processedFollow:[],isProcessing:[]};function E(t){e&&console.log(n+" "+t)}function x(){return new Promise(function(e){var t;$(document).find("script#initial-state").length?(t=$(document).find("script#initial-state").first(),JSON.parse($(t).text()).meta.access_token&&e(!0)):$(document).DOMNodeAppear(function(t){t=$(t.target);JSON.parse($(t).text()).meta.access_token&&e(!0)},"script#initial-state"),e(!1)})}!function(a){a.fn.DOMNodeAppear=function(e,i){if(!i)return!1;a(document).on("animationstart webkitAnimationStart oanimationstart MSAnimationStart",function(t){"nodeInserted"==t.originalEvent.animationName&&a(t.target).is(i)&&"function"==typeof e&&e(t)})},jQuery.fn.onAppear=jQuery.fn.DOMNodeAppear}(jQuery);var T=function(t){for(var e,i=window.location.search.substring(1).split("&"),a=0;a{setTimeout(function(){t()},h-e)}),z.lasthomerequest=t),new Promise(function(t){let e=new XMLHttpRequest;if(e.open(a,n),e.timeout=w,o)for(var i in o)e.setRequestHeader(i,o[i]);e.onload=function(){200<=this.status&&this.status<300?t(e.responseText):t(!1)},e.ontimeout=function(){E("Timed out"),t(!1)},e.onerror=function(){E("Request to "+n+" failed."),t(!1)},s?(e.setRequestHeader("Content-Type","application/json"),e.send(JSON.stringify(s))):e.send()})}function A(t){return t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function C(t,e,i){return t.replace(new RegExp(A(e),"g"),i)}function U(t){var e;M.fediact_redirects?M.fediact_alert&&!confirm("Redirecting to "+t)||(e=window.open(t,M.fediact_target),E("Redirected to "+t),e?e.focus():E("Could not open new window. Please allow popups for this website.")):E("Redirects disabled.")}async function j(t,e,i){var a,n,o,s,r="POST";switch(e){case"domainblock":a="https://"+M.fediact_homeinstance+f+"?domain="+t,n=function(t){if(t)return!0},s=function(){b()};break;case"domainunblock":a="https://"+M.fediact_homeinstance+f+"?domain="+t,n=function(t){if(t)return!0},r="DELETE",s=function(){b()};break;case"mute":a="https://"+M.fediact_homeinstance+l+"/"+t+"/mute",n=function(t){return t.muting},s=function(){b()};break;case"unmute":a="https://"+M.fediact_homeinstance+l+"/"+t+"/unmute",n=function(t){return!t.muting},s=function(){b()};break;case"block":a="https://"+M.fediact_homeinstance+l+"/"+t+"/block",n=function(t){return t.blocking},s=function(){b()};break;case"unblock":a="https://"+M.fediact_homeinstance+l+"/"+t+"/unblock",n=function(t){return!t.blocking},s=function(){b()};break;case"vote":a="https://"+M.fediact_homeinstance+p+"/"+t+"/votes",n=function(t){return t.voted},o=i;break;case"follow":a="https://"+M.fediact_homeinstance+l+"/"+t+"/follow",n=function(t){return t.following||t.requested};break;case"boost":a="https://"+M.fediact_homeinstance+d+"/"+t+"/reblog",n=function(t){return t.reblogged};break;case"favourite":a="https://"+M.fediact_homeinstance+d+"/"+t+"/favourite",n=function(t){return t.favourited};break;case"bookmark":a="https://"+M.fediact_homeinstance+d+"/"+t+"/bookmark",n=function(t){return t.bookmarked};break;case"unfollow":a="https://"+M.fediact_homeinstance+l+"/"+t+"/unfollow",n=function(t){return!t.following&&!t.requested};break;case"unboost":a="https://"+M.fediact_homeinstance+d+"/"+t+"/unreblog",n=function(t){return!t.reblogged};break;case"unfavourite":a="https://"+M.fediact_homeinstance+d+"/"+t+"/unfavourite",n=function(t){return!t.favourited};break;case"unbookmark":a="https://"+M.fediact_homeinstance+d+"/"+t+"/unbookmark",n=function(t){return!t.bookmarked};break;default:E("No valid action specified.")}if(a){var c=await g(r,a,z.tokenheader,o);if(c&&n(JSON.parse(c)))return void 0!==s&&s(),!0}E(e+" action failed.")}async function P(t){var e="https://"+M.fediact_homeinstance+l+"/relationships?";for(const o of t)e+="id[]="+o.toString()+"&";var i=await g("GET",e,z.tokenheader,null),a=Array(t.length).fill(!1);if(i)for(var i=JSON.parse(i),n=0;nt.json()),fetch("https://"+M.fediact_homeinstance+c,{headers:{Authorization:"Bearer "+M.fediact_token}}).then(t=>t.json()),fetch("https://"+M.fediact_homeinstance+f,{headers:{Authorization:"Bearer "+M.fediact_token}}).then(t=>t.json())]);t.length&&M.fediact_mutes.push(...t.map(t=>t.acct)),e.length&&M.fediact_blocks.push(...e.map(t=>t.acct)),i.length&&(M.fediact_domainblocks=i),Z()}function H(t){var i=$(k),e=$(i).find("ul");for(const n of t){var a=n[0].charAt(0).toUpperCase()+n[0].slice(1),a="
  • "+a+"
  • ";$(e).append($(a))}$("body").append($(i)),$("body").on("click",function(t){var e;!$(t.target).is(".fediactmodal li, .fediactmodal li a")||($(t.target).is(".fediactmodal li")&&(t.target=$(t.target).find("a")),e=$(t.target).attr("fediactaction"),j($(t.target).attr("fediactid"),e,null))?($(i).remove(),$("body").off()):alert("Failed to "+e)})}async function N(){$(document).DOMNodeAppear(function(t){$(t.target).find("button:has(i.fa-reply), button:has(i.fa-reply-all)").click()},"div.detailed-status__action-bar")}async function R(){function S(t){var e;return!t.startsWith("http")||(e=new URL(t),location.hostname==e.hostname)?[!1,(t=t.split("/")).pop()||t.pop()]:[!0,t]}async function e(e){$(e).is("div.detailed-status")&&$(e).closest("div.focusable").length&&(e=$(e).closest("div.focusable"));var t=function(t){if($(t).find("span.display-name__account").length)return $(t).find("span.display-name__account").first().text().trim()}($(e));if(!function(t,e){if(M.fediact_hidemuted){var i,a,n=[],o=($(t).siblings(".status__prepend").length&&(i=$(t).siblings(".status__prepend").first(),$(i).find("a").attr("href"))&&n.push($(i).find("a").attr("href").split("?")[0]),$(t).find("span.h-card").each(function(){n.push($(this).find("a").attr("href").split("?")[0])}),[]);for(a of n){var s,r=a.split("/"),r=(r=r.pop()||r.pop()).slice(1);a.startsWith("http")&&(s=new URL(a)).hostname!=z.exturi&&s.hostname!=location.hostname?o.push(r+"@"+s.hostname):o.push(r+"@"+z.exturi)}return o.some(t=>L(t))||L(e)?($(t).hide(),i&&$(i).hide(),1):void 0}}(e,t)){var i=function(t){if($(t).is(".detailed-status__wrapper"))return(e=window.location.href.split("?")[0].split("/")).pop()||e.pop();if($(t).attr("data-id"))return $(t).attr("data-id").split("-").slice(-1)[0];if($(t).closest("article[data-id], div[data-id]").length)return $(t).closest("article[data-id], div[data-id]").first().attr("data-id").split("-").slice(-1)[0];if($(t).find("a.icon-button:has(i.fa-star), a.detailed-status__link:has(i.fa-star)").length){var e=$(t).find("a.icon-button:has(i.fa-star), a.detailed-status__link:has(i.fa-star)").first();if($(e).attr("href")){t=$(e).attr("href");if(~t.indexOf("interact/"))return(e=t.split("?")[0].split("/")).pop()||e.pop()}}}($(e)),[a,n]=(a=$(e),$(a).find("a.status__relative-time").length?S($(a).find("a.status__relative-time").first().attr("href").split("?")[0]):$(a).find("a.detailed-status__datetime").length?S($(a).find("a.detailed-status__datetime").first().attr("href").split("?")[0]):$(a).find("a.modal-button").length?S($(a).find("a.modal-button").first().attr("href").split("?")[0]):[!1,void 0]),o=i||n;if(o){var s=[],r=X(o),c=$(e).find("button:has(i.fa-star)").first(),d=($(c).length||(c=$(e).find("a.icon-button:has(i.fa-star), a.detailed-status__link:has(i.fa-star)")),$("Resolving...").insertAfter($(c)),$(e).find("button:has(i.fa-retweet)").first()),l=($(d).length||(d=$(e).find("a.icon-button:has(i.fa-retweet), a.detailed-status__link:has(i.fa-retweet)")),$(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(),f=$(e).find("div.poll button").first(),p=$(e).find("button:has(i.fa-ellipsis-h)").first();async function h(t,e,i){if(M.fediact_autoaction){for(var a={choices:[]},n=$(i.currentTarget).closest("div.poll"),o=$(n).find("li"),s=0;sView the results on your home instance.

    "),r)&&(z.processed[r][10]=!0,z.processed[r][11]=!0),t}}async function m(t,e){if(!M.fediact_autoaction)return E("Auto-action disabled."),!0;a=e,i=!1,$(a.currentTarget).children("i.fa-retweet").length?i=$(a.currentTarget).children("i.fa-retweet").hasClass("fediactive")?"unboost":"boost":$(a.currentTarget).children("i.fa-star").length?i=$(a.currentTarget).hasClass("fediactive")?"unfavourite":"favourite":$(a.currentTarget).children("i.fa-bookmark").length?i=$(a.currentTarget).hasClass("fediactive")?"unbookmark":"bookmark":$(a.currentTarget).attr("href")&&(~$(a.currentTarget).attr("href").indexOf("type=reblog")?i=$(a.currentTarget).hasClass("fediactive")?"unboost":"boost":~$(a.currentTarget).attr("href").indexOf("type=favourite")&&(i=$(a.currentTarget).hasClass("fediactive")?"unfavourite":"favourite"));var i,a=i;if(a){if(await j(t,a,null))return r&&(z.processed[r][11]=!0),"boost"==a||"unboost"==a?(B($(e.currentTarget),[["color","!remove","rgb(140, 141, 255)"]],"fediactive"),B($(e.currentTarget).find("i"),[["transition-duration","!remove","0.9s"],["background-position","!remove","0px 100%"]],"fediactive"),r&&(z.processed[r][3]=!z.processed[r][3])):"favourite"==a||"unfavourite"==a?(B($(e.currentTarget),[["color","!remove","rgb(202, 143, 4)"]],"fediactive"),B($(e.currentTarget).find("i"),[["animation","spring-rotate-out 1s linear","spring-rotate-in 1s linear"]],"fediactive"),r&&(z.processed[r][4]=!z.processed[r][4])):(B($(e.currentTarget),[["color","!remove","rgb(255, 80, 80)"]],"fediactive"),r&&(z.processed[r][5]=!z.processed[r][5])),!0;E("Could not execute action on home instance.")}else E("Could not determine action.")}function g(t){$(e).find(".fediactunresolved").remove(),$(e).find(".fediactprocessing").remove(),t[1]?($(l).removeClass("disabled").removeAttr("disabled"),$(p).removeClass("disabled").removeAttr("disabled"),$(f).removeAttr("disabled"),t[4]&&!$(c).hasClass("fediactive")&&(B($(c),[["color","!remove","rgb(202, 143, 4)"]],"fediactive"),B($(c).find("i"),[["animation","spring-rotate-out 1s linear","spring-rotate-in 1s linear"]],"fediactive")),t[3]&&!$(d).find("i.fediactive").length&&(B($(d),[["color","!remove","rgb(140, 141, 255)"]],"fediactive"),B($(d).find("i"),[["transition-duration","!remove","0.9s"],["background-position","!remove","0px 100%"]],"fediactive")),t[5]&&!$(l).hasClass("fediactive")&&B($(l),[["color","!remove","rgb(255, 80, 80)"]],"fediactive"),t[10]&&($(f).hide(),$(f).closest("div.poll").find("ul").replaceWith("

    View the results on your home instance.

    "))):$("Unresolved").insertAfter($(c))}function v(n){var t=n[1].split("@"),e=t.pop()||t.pop();$(u).on("click",function(t){t.preventDefault(),t.stopImmediatePropagation(),U(n[7]+"?fedireply")}),$(p).on("click",function(t){t.preventDefault(),t.stopImmediatePropagation();t=[];F(n[1])?t.push(["unblock",n[6]]):t.push(["block",n[6]]),J(n[1])?t.push(["unmute",n[6]]):t.push(["mute",n[6]]),W(n[1])?t.push(["domainunblock",e]):t.push(["domainblock",e]),H(t)}),$([c,d,l,f]).each(function(){$(f).length&&$(f).get(0).isEqualNode($(this).get(0))&&(e=!0);var e,i,a=0;$(this).on("click",async function(t){t.preventDefault(),t.stopImmediatePropagation(),1==++a?i=setTimeout(async function(){(e&&!n[10]?h(n[9],n[7],t):await m(n[2],t))||E("Action failed."),a=0},350):(clearTimeout(i),(e?h(n[9],n[7],t):await m(n[2],t))?U(n[7]):E("Action failed."),a=0)}).on("dblclick",function(t){t.preventDefault(),t.stopImmediatePropagation()})})}if(r){var b=z.processed[r];g(b),b[1]&&v(b)}else{if(a&&s.push(n),t){var _,k,w,y=t.match(I),[x,T]=[!1,!1],A=(!y.groups.handledomain||~location.hostname.indexOf(y.groups.handledomain)||(x=!0),[i]);a||A.push(n);for(_ of A=A.filter((t,e)=>void 0!==t&&A.indexOf(t)==e))x?T||((k=await V(location.protocol+"//"+location.hostname+"/"+t+"/"+_))&&(T=!0,s.push(k),q.test(k)?(w=k.match(q)).groups.handle&&w.groups.tootid&&w.groups.domain&&s.push(w.groups.domain+"/@"+w.groups.handle+"/"+w.groups.tootid):I.test(k)&&(w=k.match(I)).groups.handle&&w.groups.tootid&&w.groups.domain&&s.push(w.groups.domain+"/users/"+w.groups.handle+"/statuses/"+w.groups.tootid)),s.push(location.protocol+"//"+location.hostname+"/"+t+"/"+_)):(s.push(location.protocol+"//"+location.hostname+"/users/"+y.groups.handle+"/statuses/"+_),s.push(location.protocol+"//"+location.hostname+"/@"+y.groups.handle+"/"+_))}if(s.length){var C,P,O,N,R,D=!1;for(C of s=s.filter((t,e)=>s.indexOf(t)==e))D||([P,O]=await Q(C),P&&(D=!0,N="https://"+M.fediact_homeinstance+"/@"+P[0]+"/"+P[1],R=[o,...P,N,!0,...O,!1]));D?(r=G(R),g(R),v(R)):(E("Failed to resolve: "+s),r=G([o,!1]),g([o,!1]))}else E("Could not identify a post URI for home resolving."),r=G([o,!1]),g([o,!1])}}else E("Could not get toot data.")}}$(document).DOMNodeAppear(async function(t){z.isProcessing.includes($(t.target).get(0))||(z.isProcessing.push($(t.target).get(0)),e($(t.target)))},"div.status, div.detailed-status"),$(document).find("div.status, div.detailed-status").each(function(){z.isProcessing.includes($(this).get(0))||(z.isProcessing.push($(this).get(0)),e($(this)))})}async function D(){async function e(a){var e,n,t,i,o,s,r="follow",c=$(a).siblings("button:has(i.fa-ellipsis-fw,i.fa-ellipsis-v)");async function d(t){return M.fediact_autoaction?(t=await j(t,r,null),"follow"==r&&t?($(a).text("Unfollow"),r="unfollow",!0):"unfollow"==r&&t?($(a).text("Follow"),r="follow",!0):void 0):(E("Auto-action disabled."),!0)}if($(c).length&&$(c).removeClass("disabled").removeAttr("disabled"),$(a).closest("div.account-card").length)e=$(a).closest("div.account-card").find("div.display-name > span").text().trim();else for(const l of u)if($(l).length){(e=$(l).text().trim()).split("@").length-1==1&&(e=e+"@"+z.exturi);break}e&&!z.processedFollow.includes(e)&&($("Resolving...  ").insertBefore($(a)),(n=await O(e))?(z.processedFollow.push(e),t=e.split("@"),i=t.pop()||t.pop(),(await P([n[0]]))[0]&&($(a).text("Unfollow"),r="unfollow"),$(c).on("click",function(t){t.preventDefault(),t.stopImmediatePropagation();t=[];F(e)?t.push(["unblock",n[0]]):t.push(["block",n[0]]),J(e)?t.push(["unmute",n[0]]):t.push(["mute",n[0]]),W(e)?t.push(["domainunblock",i]):t.push(["domainblock",i]),H(t)}),o=0,$(a).on("click",async function(t){var e,i;t.preventDefault(),t.stopImmediatePropagation(),1==++o?s=setTimeout(async function(){d(n[0]),o=0},350):(clearTimeout(s),await d(n[0])?(e=$(a).text(),i="https://"+M.fediact_homeinstance+"/@"+n[1],$(a).text("Redirecting..."),setTimeout(function(){U(i),$(a).text(e)},1e3)):E("Action failed."),o=0)}).on("dblclick",function(t){t.preventDefault(),t.stopImmediatePropagation()})):E("Could not resolve user home ID."),$(a).siblings(".fediactprocessing").remove())}var t=i.join(",");$(document).DOMNodeAppear(async function(t){z.isProcessing.includes($(t.target).get(0))||(z.isProcessing.push($(t.target).get(0)),e($(t.target)))},t),$(document).find(t).each(function(){z.isProcessing.includes($(this).get(0))||(z.isProcessing.push($(this).get(0)),e($(this)))})}function t(t){var e,i=[];for(e of t.split(/\r?\n/))(e=e.trim()).length&&(a.test(e)?i.push(e):E("Removed invalid domain "+e+" from blacklist/whitelist."));return[...new Set(i)]}function S(){if(null==M.fediact_homeinstance||!M.fediact_homeinstance)return E("Mastodon home instance is not set."),!1;if(!M.fediact_token)return E("No API token available. Are you logged in to your home instance? If yes, wait for 1-2 minutes and reload page."),!1;if(z.tokenheader={Authorization:"Bearer "+M.fediact_token},!a.test(M.fediact_homeinstance))return E("Instance setting is not a valid domain name."),!1;if("whitelist"==M.fediact_mode){if(z.whitelist=t(M.fediact_whitelist),z.whitelist.length<1)return E("Whitelist is empty or invalid."),!1}else z.blacklist=t(M.fediact_blacklist);return!0}async function K(){if(location.hostname==M.fediact_homeinstance&&(z.fedireply=T("fedireply"),!z.fedireply))return E("Current site is your home instance."),!1;if("whitelist"==M.fediact_mode){if($.inArray(location.hostname,z.whitelist)<0)return E("Current site is not in whitelist."),!1}else if(-1<$.inArray(location.hostname,z.blacklist))return E("Current site is in blacklist."),!1;var t=await g("GET",location.protocol+"//"+location.hostname+o,null,null);return!!t&&!(!(t=JSON.parse(t).uri)||(t.startsWith("http")?(t=new URL(t),z.exturi=t.hostname):z.exturi=t,Y()?!M.fediact_runifloggedin&&!z.fedireply&&await x()&&(E("Already logged in to this external instance."),1):(E("Could not start background process"),1)))}async function Y(){chrome.runtime.onMessage.addListener(async function(t,e,i){t.urlchanged&&(z.processed=[],z.processedFollow=[],z.isProcessing=[],await _()||location.reload()),t.updatedfedisettings&&location.reload()});try{return await chrome.runtime.sendMessage({running:!0}),!0}catch(t){E(t)}return!1}function _(){return new Promise(async function(e){try{M=await(browser||chrome).storage.local.get(y)}catch(t){E(t),e(!1)}M&&S()?e(!0):e(!1)})}async function Z(){await(browser||chrome).storage.local.set(M),await _()||location.reload()}async function tt(){await _()?await K()?(z.fedireply?N:(D(),R))():E("Will not process this site."):E("Could not load settings.")}tt();