URI: 
       background.js - firefox-fix-web - Firefox extension: fix web
  HTML git clone git://git.codemadness.org/firefox-fix-web
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       background.js (3102B)
       ---
            1 // !!!
            2 // NOTE: this file is disabled for now. It breaks many sites that use Cloudflare for the captcha.
            3 
            4 /* what this file does:
            5    - rewrites User-Agent headers.
            6    - sets consent cookie for Google and Youtube. */
            7 
            8 function main(info) {
            9 
           10 // https://www.useragents.me/#most-common-desktop-useragents-json-csv
           11 
           12 /* get current running Firefox version, to always keep up-to-date */
           13 var ff_version = info.version || "136.0";
           14 
           15 /* User-Agent's */
           16 // Windows 7 (64-bit)
           17 //var ua_windows7 = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:" + ff_version + ") Gecko/20100101 Firefox/" + ff_version;
           18 // Windows 10 (64-bit)
           19 var ua_windows10 = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:" + ff_version + ") Gecko/20100101 Firefox/" + ff_version;
           20 // Windows 10 Chrome, NOTE: update version manually if needed.
           21 //var ua_windows10_chrome = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36";
           22 //var ua_windows10_chrome = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36";
           23 
           24 // OpenBSD amd64
           25 var ua_openbsd = "Mozilla/5.0 (X11; OpenBSD amd64; rv:" + ff_version + ") Gecko/20100101 Firefox/" + ff_version;
           26 
           27 var ua_none    = "";
           28 var ua_default = ua_windows10;
           29 
           30 var prefix = "^(http|https):\/\/([^\.]+\.)?"; // HTTP and HTTPS, optional subdomain.
           31 var patterns = [
           32         // Twitter: remove User-Agent, this prevents an annoying redirect to "legacy" Twitter each time.
           33         {
           34                 "re": new RegExp(prefix + "twitter\.com\/.*"),
           35                 "ua": ua_windows10
           36         }
           37 ];
           38 
           39 function rewriteua(e) {
           40         var headers = e.requestHeaders;
           41         for (var j = 0; j < headers.length; j++) {
           42                 var header = headers[j];
           43                 if (header.name.toLowerCase() !== "user-agent")
           44                         continue;
           45                 var ua = ua_default;
           46                 var url = e.documentUrl || e.url || "";
           47                 for (var p of patterns) {
           48                         var m = p.re.exec(url);
           49                         if (m === null)
           50                                 continue;
           51                         ua = p.ua;
           52                         break;
           53                 }
           54                 if (ua !== "")
           55                         header.value = ua;
           56                 else
           57                         delete headers[j];
           58         }
           59 
           60         return { requestHeaders: headers };
           61 }
           62 
           63 chrome.webRequest.onBeforeSendHeaders.addListener(
           64         rewriteua,
           65         { urls: [ "<all_urls>" ]},
           66         ["blocking", "requestHeaders"]
           67 );
           68 
           69 // "forced consent"...
           70 function google_forced_consent(e) {
           71         var r = parseInt(100+(Math.random() * 899));
           72 //        var r = 101;
           73         var value = "CONSENT=YES+cb.20210328-17-p0.en+FX+" + r.toString();
           74         var headers = e.requestHeaders;
           75         var cookieset = 0;
           76         for (var j = 0; j < headers.length; j++) {
           77                 var header = headers[j];
           78                 if (header.name.toLowerCase() !== "cookie")
           79                         continue;
           80 
           81                 header.value = value;
           82 
           83                 cookieset = 1;
           84         }
           85         if (!cookieset)
           86                 headers.set("Cookie", value);
           87         return { requestHeaders: headers };
           88 }
           89 
           90 /*
           91 chrome.webRequest.onBeforeSendHeaders.addListener(
           92         google_forced_consent,
           93         {
           94 //        urls: [ "<all_urls>" ]},
           95         urls: [
           96 //                "*://*/*",
           97                 "*://*.google.com/*",
           98                 "*://google.com/*",
           99                 "*://*.google.nl/*",
          100                 "*://google.nl/*",
          101                 "*://*.youtube.com/*",
          102                 "*://youtube.com/*",
          103                 "*://*.youtu.be/*",
          104                 "*://youtu.be/*",
          105                 "*://*.gstatic.com/*"
          106         ]
          107         },
          108         ["blocking", "requestHeaders"]
          109 );
          110 
          111 }
          112 
          113 browser.runtime.getBrowserInfo().then(main);
          114 */