Skip to content

Fix incorrect pronouns in glitch-soc notifications #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions src/content_scripts/protoots.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import {
accountNameFromURL,
addTypeAttribute,
normaliseAccountName,
} from "../libs/protootshelpers";
} from "../libs/protootshelpers.js";
import { debug } from "../libs/logging.js";

//before anything else, check whether we're on a Mastodon page
checkSite();
Expand Down Expand Up @@ -200,13 +201,18 @@ async function addProplate(element) {

/**
* Generates a proplate and adds it as a sibling of the given nameTagEl
* @param {string} statusId Id of the target object
* @param {string} accountName Name of the account the plate is for
* @param {HTMLElement} nametagEl Element to add the proplate next to
* @param {string|undefined} statusId Id of the target object
* @param {string|null} accountName Name of the account the plate is for
* @param {HTMLElement|null} nametagEl Element to add the proplate next to
* @param {string} type type of the target object
* @returns
*/
async function generateProPlate(statusId, accountName, nametagEl, type) {
debug("generateProPlate called with params", { statusId, accountName, nametagEl, type });
if (!statusId) throw new Error("empty statusId passed to proplate generation, aborting.");
if (!accountName) throw new Error("empty accountName passed to proplate generation, aborting.");
if (!nametagEl) throw new Error("empty nametagEl passed to proplate generation, aborting.");

//create plate
const proplate = document.createElement("span");
const pronouns = await fetchPronouns(statusId, accountName, type);
Expand All @@ -228,7 +234,7 @@ async function addProplate(element) {
/**
* Gets the data-id from the given element
* @param {HTMLElement} element Element with data-id attribute
* @returns {string}
* @returns {string|undefined}
*/
function getID(element) {
let id = element.dataset.id;
Expand Down Expand Up @@ -264,17 +270,23 @@ async function addProplate(element) {

/**
* Gets the given element's textcontent or given attribute
* @param {HTMLElement} element Element which textcontent is the account name
* @param {HTMLElement|null} element Element which textcontent is the account name
* @param {string} attribute Attribute from which to pull the account name
* @returns {string} Normalised account name
* @returns {string|null} Normalised account name or null if it can't be found.
*/
function getAccountName(element, attribute = "textContent") {
if (!element) return null;
let accountName = element.textContent;
if (attribute != "textContent") {
accountName = element.getAttribute(attribute);
}

if (!accountName) {
warn("Could not extract the account name from the element.");
warn(
`Could not extract the account name from the element, using attribute ${attribute} aborting pronoun extraction:`,
element,
);
return null;
}

accountName = normaliseAccountName(accountName);
Expand Down Expand Up @@ -330,7 +342,9 @@ async function addProplate(element) {
if (!accountNameEl) accountNameEl = getAccountNameEl(element, ".status__display-name");

let accountName = getAccountName(accountNameEl, "title");
if (!accountName) accountName = accountNameFromURL(getAccountName(accountNameEl, "href"));
if (!accountName) {
accountName = accountNameFromURL(getAccountName(accountNameEl, "href"));
}

let nametagEl = getNametagEl(element, ".notification__display-name");
if (!nametagEl) return;
Expand Down
5 changes: 3 additions & 2 deletions src/libs/protootshelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { hasClasses } from "./domhelpers";
* @returns {string}
*/
export function normaliseAccountName(name) {
if (!name) return "null";
if (!name) throw new Error("null/undefined passed to normaliseAccountName");
if (name[0] == "@") name = name.substring(1);
// if the username doesn't contain an @ (i.e. the post we're looking at is from this instance)
// append the host name to it, to avoid cache overlap between instances
Expand All @@ -21,10 +21,11 @@ export function normaliseAccountName(name) {
* Turns a link to an account on a remote instance into a username.
*
* e.g. `https://example.com/@test` -> `@[email protected]`
* @param {string} url URL to an account on their own instance
* @param {string|null} url URL to an account on their own instance
* @returns {string} username (not normalised)
*/
export function accountNameFromURL(url) {
if (!url) return null;
const splitURL = url.split("/");

const username = [splitURL.pop(), splitURL.pop()].join("@");
Expand Down