jscher2000 / dont-accept-webp

Don't "Accept" image/webP - extension for Firefox
Mozilla Public License 2.0
82 stars 0 forks source link

Version 0.8 is broken, `originUrl` is undefined #4

Closed Loirooriol closed 2 years ago

Loirooriol commented 2 years ago

https://github.com/jscher2000/dont-accept-webp/blob/f63ee124c6ed57ee759270072b0b1fae4d55063b/background.js#L50

details.originUrl is undefined for top-level resources.

https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/onBeforeSendHeaders#originurl

The originUrl is often but not always the same as the documentUrl.

https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/onBeforeSendHeaders#documenturl

For a top-level document, documentUrl is undefined.

Anyways, new URL(undefined) throws and then the extension doesn't change the headers, and the server sends me a webp image.

jscher2000 commented 2 years ago

Thanks. It seemed fine in testing but I'll post a fix in a few minutes.

jscher2000 commented 2 years ago

My bad, I forgot to test stand-alone images. Updated version 0.8.1 is making its way through the approval process.

Loirooriol commented 2 years ago

https://github.com/jscher2000/dont-accept-webp/blob/5808628f3589ca122612b28e6e489035124c3c34/background.js#L51

I think it should be details.originUrl? Also, != null covers both null and undefined, see https://tc39.es/ecma262/#sec-islooselyequal, no need to check them both. And consider something like

function isExempt(url) {
  if (url == null) return false;
  let { hostname } = new URL(url);
  return oPrefs.exemptSites.includes(hostname);
}

if (isExempt(details.url) || isExempt(details.documentUrl) || isExempt(details.originUrl)) {
  return { requestHeaders: details.requestHeaders };
}
jscher2000 commented 2 years ago

I was too busy to reply at that time, but hopefully by now you got the update with the fixes.