Skip to content

Instantly share code, notes, and snippets.

@Ultrabenosaurus
Last active April 10, 2020 22:43
Show Gist options
  • Save Ultrabenosaurus/b94d373f3a32d2b5d6ae5256e04784a7 to your computer and use it in GitHub Desktop.
Save Ultrabenosaurus/b94d373f3a32d2b5d6ae5256e04784a7 to your computer and use it in GitHub Desktop.
Proof-of-Concept Firefox Add-On to Redirect Before Page Loads
{
"description": "Redirect AMP addresses to their non-AMP sources",
"manifest_version": 2,
"name": "AMP Redirect",
"version": "0.1",
"author": "Ultrabenosaurus",
"_comments_": {
"signed_xpi": "https://drive.google.com/file/d/1Pg19uXregtxfENnKO3tcvR2SiTu01IYf/view?usp=sharing",
"source:": "https://gist.github.com/Ultrabenosaurus/b94d373f3a32d2b5d6ae5256e04784a7",
"based_on": "https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/onBeforeRequest",
"userscript": "https://greasyfork.org/en/scripts/400161-amp-redirect"
},
"permissions": [
"webRequest",
"webRequestBlocking",
"<all_urls>"
],
"background": {
"scripts": ["redirect.js"]
}
}
/**
*
* name AMP Redirect
* namespace ultrabenosaurus.AMP
* version 0.1
* description Redirect AMP addresses to their non-AMP sources
* author Ultrabenosaurus
* signed XPI https://drive.google.com/file/d/1Pg19uXregtxfENnKO3tcvR2SiTu01IYf/view?usp=sharing
* source https://gist.github.com/Ultrabenosaurus/b94d373f3a32d2b5d6ae5256e04784a7
* userscript https://greasyfork.org/en/scripts/400161-amp-redirect
*
* Based on MDN web docs: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/onBeforeRequest
*
* from: https://www-techradar-com.cdn.ampproject.org/v/s/www.techradar.com/amp/news/eliminating-vpns-for-more-secure-productive-remote-work?amp_js_v=a3&amp_gsa=1&usqp=mq331AQFKAGwASA%3D#referrer=https%3A%2F%2Fwww.google.com&amp_tf=From%20%251%24s
* to: https://www.techradar.com/news/eliminating-vpns-for-more-secure-productive-remote-work
*
*/
function UBredirect(requestDetails){
// console.log(requestDetails);
var a = requestDetails.url.split("/");
// console.log(a);
var al = a.length;
var d = a[2].split(".")[0].replace(/-/g,".");
// console.log(d);
if(d == a[5]){
var p = a[al-1].split("?")[0];
for (var i = al-2; i > 6; i--) {
if(a[i] !== "amp") { p = a[i]+"/"+p; }
// console.log(a[i]);
}
// console.log(p);
var s = a[0]+"//"+d+"/"+p;
// console.log(s);
return {
redirectUrl: s
};
}
}
browser.webRequest.onBeforeRequest.addListener(
UBredirect,
{urls: ["<all_urls>"]},
["blocking"]
);
@gwarser
Copy link

gwarser commented Apr 10, 2020

Re: https://addons.mozilla.org/en-US/firefox/addon/amp2html/reviews/1519663/

This is exactly what "Redirect AMP to HTML" is doing - source.

Yet on many pages there is no way to detect amp from the URL. On these pages page source (canonical link) must be checked.

@Ultrabenosaurus
Copy link
Author

Re: https://addons.mozilla.org/en-US/firefox/addon/amp2html/reviews/1519663/

This is exactly what "Redirect AMP to HTML" is doing - source.

Yet on many pages there is no way to detect amp from the URL. On these pages page source (canonical link) must be checked.

If that was exactly what the add-on was doing, why was it not redirecting me for the example URL I have included in the comment block in the JS file? This code works perfectly and redirects such URLs, while that add-on does not.

Also, neither of the source links in your comment work for me:

image

@gwarser
Copy link

gwarser commented Apr 10, 2020

why was it not redirecting me for the example URL I have included in the comment block in the JS file?

https://www-techradar-com.cdn.ampproject.org/v/s/www.techradar.com/amp/news/eliminating-...?

These are supported:

        'https://www.bing.com/amp/*',
        'https://www.google.com/amp/*'

        'https://cdn.ampproject.org/c/*',
        'https://*.cdn.ampproject.org/c/*',
        'https://bing-amp.com/c/*',
        'https://*.bing-amp.com/c/*'

        '*://t.co/*?amp=1'

You should probably report few samples to the author - maybe he will add them.

Also, neither of the source links in your comment work for me:

Hmmm, seems you have network issues. Works for me on two different browsers and two devices.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment