chore: fix stylistic issues
This commit is contained in:
parent
b61dbc4a57
commit
9a1f476a0b
2 changed files with 34 additions and 32 deletions
|
@ -145,12 +145,13 @@ function getInteractionType(url) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles changes to the URL of a tab
|
* Handles changes to the URL of a tab.
|
||||||
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
function onTabUpdate() {
|
function onTabUpdate() {
|
||||||
browser.tabs.executeScript(null, {
|
browser.tabs.executeScript(null, {
|
||||||
file: `/content_script/mastodonInject.js`
|
file: "/content_script/mastodonInject.js"
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -10,9 +10,9 @@
|
||||||
* @returns {VersionNumber}
|
* @returns {VersionNumber}
|
||||||
*/
|
*/
|
||||||
function parseVersionNumber() {
|
function parseVersionNumber() {
|
||||||
const versionElement = document.querySelector(`.link-footer`).textContent
|
const versionElement = document.querySelector(`.link-footer`).textContent;
|
||||||
const versionNumber = versionElement.match(/v?(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)$/)
|
const versionNumber = versionElement.match(/v?(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)$/);
|
||||||
return versionNumber.groups
|
return versionNumber.groups;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -20,81 +20,82 @@ function parseVersionNumber() {
|
||||||
* Replacement onClick handler for Follow button
|
* Replacement onClick handler for Follow button
|
||||||
* @param {Event} event
|
* @param {Event} event
|
||||||
*/
|
*/
|
||||||
function onClickFollow (event) {
|
function onClickFollow(event) {
|
||||||
event.stopPropagation()
|
event.stopPropagation();
|
||||||
event.preventDefault()
|
event.preventDefault();
|
||||||
const username = window.location.pathname.split(`/`).slice(-1)[0]
|
const username = window.location.pathname.split(`/`).slice(-1)[0];
|
||||||
// activate AutoRemoteFollow
|
// 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 {string} selector
|
||||||
* @param {number} timeout
|
* @param {number} timeout
|
||||||
|
* @see {@link https://github.com/storybookjs/test-runner/blob/6d41927154e8dd1e4c9e7493122e24e2739a7a0f/src/setup-page.ts#L134} from which this was adapted
|
||||||
*/
|
*/
|
||||||
function waitForElement (selector, timeout) {
|
function waitForElement(selector, timeout) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
function getElement() {
|
function getElement() {
|
||||||
return document.querySelector(selector)
|
return document.querySelector(selector);
|
||||||
}
|
}
|
||||||
|
|
||||||
const element = getElement()
|
const element = getElement();
|
||||||
if(element){
|
if(element){
|
||||||
return resolve(element)
|
return resolve(element);
|
||||||
}
|
}
|
||||||
|
|
||||||
const observer = new MutationObserver(() => {
|
const observer = new MutationObserver(() => {
|
||||||
const element = getElement()
|
const element = getElement();
|
||||||
if(element){
|
if(element){
|
||||||
resolve(element)
|
resolve(element);
|
||||||
observer.disconnect()
|
observer.disconnect();
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
observer.observe(document.body, {
|
observer.observe(document.body, {
|
||||||
childList: true,
|
childList: true,
|
||||||
subtree: true
|
subtree: true
|
||||||
})
|
});
|
||||||
|
|
||||||
window.setTimeout(() => {
|
window.setTimeout(() => {
|
||||||
reject()
|
reject();
|
||||||
}, timeout)
|
}, timeout);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inject replacement onClick handler for Follow button
|
* Inject replacement onClick handler for Follow button
|
||||||
*/
|
*/
|
||||||
async function injectFollowButton () {
|
async function injectFollowButton() {
|
||||||
try {
|
try {
|
||||||
const followButton = await waitForElement(`.account__header__tabs__buttons button.button`, 20000)
|
const followButton = await waitForElement(`.account__header__tabs__buttons button.button`, 20000);
|
||||||
followButton.addEventListener(`click`, onClickFollow)
|
followButton.addEventListener(`click`, onClickFollow);
|
||||||
} catch {
|
} catch {
|
||||||
// Follow button failed to appear
|
// Follow button failed to appear
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function init () {
|
async function init() {
|
||||||
let versionNumber
|
let versionNumber;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const initialStateObject = JSON.parse(document.getElementById(`initial-state`).innerHTML)
|
const initialStateObject = JSON.parse(document.getElementById(`initial-state`).innerHTML);
|
||||||
const version = initialStateObject?.meta?.version
|
const version = initialStateObject?.meta?.version;
|
||||||
if(!initialStateObject || !version){
|
if(!initialStateObject || !version){
|
||||||
// not a Mastodon server
|
// not a Mastodon server
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
versionNumber = parseVersionNumber(version)
|
versionNumber = parseVersionNumber(version);
|
||||||
} catch {
|
} catch {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if(Number.parseInt(versionNumber.major) >= 4){
|
if(Number.parseInt(versionNumber.major) >= 4){
|
||||||
await injectFollowButton()
|
await injectFollowButton();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
init()
|
init();
|
Loading…
Add table
Add a link
Reference in a new issue