fix: formatting and address other review comments

This commit is contained in:
Huey 2022-11-18 12:38:33 +08:00
parent e9c16cad10
commit 4d5c4a5904
No known key found for this signature in database
GPG key ID: 54C82E718C137231
2 changed files with 31 additions and 22 deletions

View file

@ -149,7 +149,7 @@ function getInteractionType(url) {
* @returns {void}
*/
function onTabUpdate() {
browser.tabs.executeScript(null, {
browser.tabs.executeScript({
file: "/content_script/mastodonInject.js"
});
}

View file

@ -1,38 +1,44 @@
"use strict";
/**
* Replacement onClick handler for Follow button
* Replacement onClick handler for Follow button.
* @param {Event} event
* @returns {void}
*/
function onClickFollow(event) {
event.stopPropagation();
event.preventDefault();
const username = window.location.pathname.split(`/`).slice(-1)[0];
const username = window.location.pathname.split("/").slice(-1)[0];
// activate AutoRemoteFollow
window.open(`/users/${username}/remote_follow`, `_blank`);
window.open(`/users/${username}/remote_follow`, "_blank");
}
/**
* wait for element to appear
* wait for element to appear.
* @param {string} selector
* @param {number} timeout
* @param {number} timeoutDuration
* @see {@link https://github.com/storybookjs/test-runner/blob/6d41927154e8dd1e4c9e7493122e24e2739a7a0f/src/setup-page.ts#L134}
* from which this was adapted
* @returns {Promise}
*/
function waitForElement(selector, timeout) {
function waitForElement(selector, timeoutDuration) {
return new Promise((resolve, reject) => {
function getElement() {
return document.querySelector(selector);
}
const getElement = () => document.querySelector(selector);
const timeout = window.setTimeout(() => {
reject(new Error("waitForElement timed out"));
}, timeoutDuration);
const element = getElement();
if(element){
window.clearTimeout(timeout);
return resolve(element);
}
const observer = new MutationObserver(() => {
const element = getElement();
if(element){
window.clearTimeout(timeout);
resolve(element);
observer.disconnect();
}
@ -43,24 +49,27 @@ function waitForElement(selector, timeout) {
subtree: true
});
window.setTimeout(() => {
reject();
}, timeout);
return null;
});
}
/**
* Inject replacement onClick handler for Follow button
* Inject replacement onClick handler for Follow button.
* @returns {void}
*/
async function injectFollowButton() {
try {
const followButton = await waitForElement(`.account__header__tabs__buttons button.button`, 20000);
followButton.addEventListener(`click`, onClickFollow);
} catch {
const followButton = await waitForElement(".account__header__tabs__buttons button:first-of-type", 20000);
followButton.addEventListener("click", onClickFollow);
} catch (error) {
// Follow button failed to appear
}
}
/**
* Initialise injection for Mastodon Follow button.
* @returns {void}
*/
async function init() {
await injectFollowButton();
}