Skip to content

Commit

Permalink
Use URL interface in apply-css example
Browse files Browse the repository at this point in the history
By using the URL interface, the user is ascertained that the piece of
code does not manipulate the DOM.  Keeping them in the right mental
context for the example.

The apply-css example uses the protocol of the URL of the tab to
determine if a pageAction should be shown.  Any page that is hosted
over http or https will receive the page action.

The former implementation creates an anchor tag, assigns the URL to
that, and fetches the protocol from there.  This PR alters that to
using the URL interface.  This makes it clear to the user that we're
staying in JavaScript land and will not start manipulating the visible
DOM.  I would argue this is cleaner all together, but I'm very open to
learning about a different opinion.

The URL interface is more broadly supported than the
pageAction.setIcon which this example is also dependent on, so this
change should not change compatibility of this Browser Extension.
  • Loading branch information
madnificent committed Dec 31, 2020
1 parent 2184fee commit 4cddd84
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions apply-css/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ function toggleCSS(tab) {
Returns true only if the URL's protocol is in APPLICABLE_PROTOCOLS.
*/
function protocolIsApplicable(url) {
var anchor = document.createElement('a');
anchor.href = url;
return APPLICABLE_PROTOCOLS.includes(anchor.protocol);
const protocol = (new URL(url)).protocol;
return APPLICABLE_PROTOCOLS.includes(protocol);
}

/*
Expand Down

0 comments on commit 4cddd84

Please sign in to comment.