# Hide link formatting Firefox extension

I'm building a Mozilla Firefox extension to hide link's formatting and show them as regular text. It consists of a script that runs on web-pages (content.js) that's activated by clicking on the extension (handled with background.js). On activation, a CSS rule is injected to hide link's formatting. Activating it again will shut changes off.

background.js additionally updates the extension's icon (based on links being currently hidden or not) every time the active tab is changed.

I'm new to JavaScript. I'd really appreciate your comments and observations to improve it (best practices, code structure, etc).

Script that runs on web pages (content.js):

'use strict';

const hideLinksRule = 
a,
a:hover,
a:focus,
a:active,
a:visited {
text-decoration: none !important;
color: inherit !important;
background-color: inherit !important;
border-bottom: initial !important;
};

let styleSheet = (function () {
let style = document.createElement('style');
return style.sheet;
})();

function handleRequest(request) {

// Do not modify if request is only a query
if (request.isQuery) {
return Promise.resolve({
});
}

styleSheet.deleteRule(0);
} else {
}

return Promise.resolve({
});

}



Script to handle the extension on the browser (background.js):

// Icons paths
};
};

function onError(error) {
}

function updateIcon(response) {
} else {
}
}

function verifyTabStatus(activeInfo) {
browser.tabs.sendMessage(
activeInfo.tabId, {
isQuery: true
}
).then(updateIcon).catch(onError);
}

browser.tabs.query({
currentWindow: true,
active: true
}

if (!(tabs === undefined || tabs.length == 0)) {
const activeTab = tabs[0];
browser.tabs.sendMessage(
activeTab.id, {
isQuery: false
}
).then(updateIcon).catch(onError);
}

}

// Update Icon on tab change

// Request toggle link formatting on click


Configuration file to put it together (manifest.json):

{

"manifest_version": 2,
"version": "1.0",

"icons": {
},

"permissions": [
"tabs"
],

"browser_action": {
},

"background": {
"scripts": ["background.js"]
},

"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"]
}
]

}


You can also get the source code here. Thank you very much for your time.

Always prefer const when declaring variables - only use let when you have to reassign a variable. const requires less cognitive overhead, when you know at a glance that something's never going to be reassigned.

Style toggling You're currently toggling the style with styleSheet.insertRule(rule, 0) and styleSheet.deleteRule(0). I think it would be a little bit better to take a different approach.

• Who knows what insertRule(rule, 0) and deleteRule(0) do at a glance? Probably not a whole lot of people; it's not a very common API that's used.
• What if you need to expand the extension to add additional rules? Then using multiple rule indicies could become repetitive.

One alternative would be to, on toggle, either append or remove the style tag from the <head>. Another alternative would be to assign or clear its textContent. See below for example.

hideLinks is not an altogether precise name. At first glance, someone seeing a variable named hideLinks would probably think it sounds like a function that, when called, hides links. Maybe call it something else, like linksAreHidden. You can also make its toggling more DRY:

if (hideLinks) {
styleSheet.deleteRule(0);
} else {
}


can be

if (hideLinks) {
styleSheet.deleteRule(0);
} else {
}


Or, even better, rather than having a persistent outside variable, you could simply check to see whether the style element is attached or has content. See below for example. This is what I'd prefer - let the style node (which must exist regardless) be the source of truth, rather than introducing another variable that needs to stay in sync.

Request object You currently have

if (request.isQuery) {
return Promise.resolve({
});
}
// otherwise, insert or remove style rule


I think it'd be better to be more explicit. What if you add a 3rd request command, or a 4th request command? Instead of having an implicit "Else, if this isn't a query, then do THIS instead", I'd prefer to always send a property indicating what the command should be.

Perhaps like this, using a isToggle property:

const hideLinksStyleText = 
a,
a:hover,
a:focus,
a:active,
a:visited {
text-decoration: none !important;
color: inherit !important;
background-color: inherit !important;
border-bottom: initial !important;
};

function handleRequest(request) {
// Do not modify if request is only a query
if (request.isQuery) {
return Promise.resolve({
});
}
if (request.isToggle) {
: '';
return Promise.resolve({
});
}
}


The conditional operator can be used to make conditional expressions tersely. This:

const showLinksIconPath = {
};
};
function updateIcon(response) {
} else {
}
}


can turn into

function updateIcon(response) {
browser.browserAction.setIcon({
path: icons/\${response.hideLinks ? 'broken-' : ''}link-icon.png
});
}


Or, if you don't like the interpolation:

function updateIcon(response) {
browser.browserAction.setIcon({
});
}


Tab checking You have:

if (!(tabs === undefined || tabs.length == 0)) {
const activeTab = tabs[0];


The negations are slightly tougher to parse logically at a glance than they can be. Consider instead:

if (tabs && tabs.length) {


In a larger project, you could use optional chaining and Babel:

if (tabs?.length) {


But optional chaining is only supported in very new versions of FF, and Babel isn't worth it just for this. (Babel is a good idea if you want to write a reasonably sized script and want to permit older browser versions to be able to understand the JS syntax you use. For a FF extension, it's less important, because almost all users will be on reasonably up-to-date browsers anyway - no need to transpile down to ES5 for those crummy IE11 users)

• Thank you very much for your recommendations. One question, the ! operator here const linksOriginallyHidden = !styleTag.textContent; is to verify if the string is empty? – fabrizzio_gz Sep 24 at 21:47
• Yep, that inverts the truthyness of the following expression. !someText evaluates to true if someText contains the empty string. Otherwise, it'll evaluate to false if someText contains any other string. – CertainPerformance Sep 24 at 21:50
• Right. Since links are hidden when the textContent isn't empty, I believe the condition should be inverted then: const linksOriginallyHidden = !!styleTag.textContent;. However, I ran across the formatting not changing anymore when I used a double !!. I don't know if I have a bug somewhere else or if it has anything to do with the style tag being an object and casting its properties to a boolean. The funny thing is that when I use a single ! as in your script, it works but the icons are inverted (it shows a hidden-links icon when they aren't and vice-versa). – fabrizzio_gz Sep 24 at 22:06
• Oops, linksOriginallyHidden should be assigned the truthyness of the original textContent, not the other way around. Make sure that the object property names match up, eg linksAreHidden in both the content script and background script. – CertainPerformance Sep 24 at 22:11
• Yes, I found the problem. This assignment needed to be inverted as well: styleTag.textContent = linksOriginallyHidden ? '', hideLinksStyleText';. Thanks again for all of your observations! I'm working on implementing them. It's a nice improvement and I learned new things. – fabrizzio_gz Sep 24 at 22:24