├── LICENSE ├── README.md ├── background.js ├── icons ├── hammer-16-Zzzz.png ├── hammer-16-star.png ├── hammer-32-Zzzz.png ├── hammer-32-star.png ├── hammer-64-Zzzz-lightbg.png ├── hammer-64-Zzzz.png ├── hammer-64-star-lightbg.png └── hammer-64-star.png ├── img ├── arrow-left.svg ├── off-status.png └── on-status.png ├── logaddnew.css ├── logaddnew.html ├── logaddnew.js ├── manifest.json ├── popup.css ├── popup.html └── popup.js /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Content-Type-Fixer-extension 2 | Firefox extension to override dumb Content-Type headers with correct values. Built-in support for downloads with these extensions: .acsm, .doc/docx, .epub, .pdf, .ppt/pptx, .psd, .rar, .rtf, .xls/xlsx, .zip, and .7z. You can define your own Content-Type associations for other file extensions, either filling in the real one or a fictitious one. 3 | 4 | https://addons.mozilla.org/firefox/addon/content-type-fixer/ 5 | 6 | By default, this extension is not listening. Use the toolbar button to turn listening on and off. 7 | 8 | When you turn it on, the extension checks every request to see whether it has a file extension that should be assigned a specific Content-Type header. 9 | 10 | The extension keeps a log of its activity, and offers Add and Edit buttons so you can add a custom Content-Type as needed. The log is not saved to disk and is flushed when you snooze the extension. 11 | 12 | **Permissions Note:** While it is listening, the extension reads the Content-Type and Content-Disposition headers that servers send with files. It may modify those headers. The extension does not read web pages. 13 | 14 | **Test Page:** Without the extension running, you get bad results for the zip files on this page, and the extension fixes it: https://www.jeffersonscher.com/res/badct/ 15 | 16 | **Hat tip:** The code draws from the Display inline extension from samlh - https://github.com/samlh/display-inline 17 | -------------------------------------------------------------------------------- /background.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2022. Jefferson "jscher2000" Scher. License: MPL-2.0. 3 | v0.2 - initial design; uses some code from https://github.com/samlh/display-inline (MIT) 4 | v0.3 - fix unquoted filename's (old ASP on IIS issue) 5 | v1.0 - log script actions while listening (not stored); adding/editing associations 6 | v1.1 - content-disposition override (initial support), autostart option 7 | v1.5 - ability to enable/disable individual Content-Type overrides 8 | v1.6 - fix bug with saving enabled/disabled status of built-in content types, 9 | don't apply content-disposition: attachment to text/html unless user overrides, 10 | buttons with light background for dark themes 11 | v1.6.1, v1.6.2 - bug fix for the bug fix 12 | v1.7 - don't apply content-type changes to text/html unless user overrides 13 | v1.7.1 - Google Drive / Gmail attachment exception re Content-Disposition for Firefox 85 14 | v1.7.2 - Google Docs download exception re Content-Disposition 15 | v1.7.3 - Avoid Content-Disposition modifications with response status codes != 200 16 | v1.7.4 - Some additional default content-type entries 17 | */ 18 | 19 | let nowlistening = false; 20 | let fixCTlog = {}; 21 | 22 | /**** Create and populate data structure ****/ 23 | // Default starting values 24 | let trueCT = [ 25 | { ext: "acsm", ct: "application/vnd.adobe.adept+xml", builtin: true, enabled: true }, 26 | { ext: "doc", ct: "application/msword", builtin: true, enabled: true }, 27 | { ext: "docx", ct: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", builtin: true, enabled: true }, 28 | { ext: "epub", ct: "application/epub+zip", builtin: true, enabled: true }, 29 | { ext: "pdf", ct: "application/pdf", builtin: true, enabled: true }, 30 | { ext: "ppt", ct: "application/vnd.ms-powerpoint", builtin: true, enabled: true }, 31 | { ext: "pptx", ct: "application/vnd.openxmlformats-officedocument.presentationml.presentation", builtin: true, enabled: true }, 32 | { ext: "psd", ct: "application/vnd.adobe.photoshop", builtin: true, enabled: true }, 33 | { ext: "rar", ct: "application/vnd.rar", builtin: true, enabled: true }, 34 | { ext: "rtf", ct: "application/rtf", builtin: true, enabled: true }, 35 | { ext: "xls", ct: "application/vnd.ms-excel", builtin: true, enabled: true }, 36 | { ext: "xlsx", ct: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", builtin: true, enabled: true }, 37 | { ext: "zip", ct: "application/zip", builtin: true, enabled: true }, 38 | { ext: "7z", ct: "application/x-7z-compressed", builtin: true, enabled: true }, 39 | { ext: "css", ct: "text/css", builtin: true, enabled: true }, 40 | { ext: "csv", ct: "text/csv", builtin: true, enabled: true }, 41 | { ext: "jpg", ct: "image/jpeg", builtin: true, enabled: false }, 42 | { ext: "png", ct: "image/png", builtin: true, enabled: false }, 43 | { ext: "flv", ct: "video/x-flv", builtin: true, enabled: false }, 44 | { ext: "mp4", ct: "video/mp4", builtin: true, enabled: false }, 45 | { ext: "swf", ct: "application/x-shockwave-flash", builtin: true, enabled: false } 46 | ]; 47 | 48 | // Update trueCT from storage 49 | let customCT = []; 50 | function updateCT(){ 51 | browser.storage.local.get("userCT").then((results) => { 52 | customCT = results.userCT; 53 | if (customCT != undefined){ 54 | for (var j=0; j objCT.ext === customCT[j].ext ); 56 | if (defaultCT !== undefined){ 57 | defaultCT.builtin = defaultCT.ct; // is this legal? 58 | defaultCT.ct = customCT[j].ct; 59 | if (customCT[j].enabled === false) defaultCT.enabled = false; 60 | else defaultCT.enabled = true; 61 | } else { 62 | if (!customCT[j].hasOwnProperty("enabled")) customCT[j].enabled = true; 63 | trueCT.push(customCT[j]); 64 | } 65 | } 66 | } 67 | }).catch((err) => {console.log('Error retrieving storage: '+err.message);}); 68 | } 69 | updateCT(); 70 | 71 | // Preferences 72 | let oPrefs = { 73 | intLogExpiration: 300, // Seconds after which log entries expire (default to 5 minutes) 74 | dispoAction: 'browser', // Content-disposition: 'browser' (do not override), 'inline', 'attachment' 75 | autostart: false, // Whether to start listening when the extension initializes 76 | excepthtml: true, // Don't override content-disposition on text/html 77 | ctexcepthtml: true // Don't override content-type on text/html 78 | }; 79 | // Update oPrefs from storage 80 | function updatePref(){ 81 | browser.storage.local.get("userprefs").then((results) => { 82 | if (results.userprefs != undefined){ 83 | if (JSON.stringify(results.userprefs) != '{}'){ 84 | var arrSavedPrefs = Object.keys(results.userprefs) 85 | for (var j=0; j {console.log('Error retrieving storage: '+err.message);}); 93 | } 94 | updatePref(); 95 | 96 | // Theme 97 | let iconsuffix = ''; 98 | browser.theme.getCurrent().then((theme) => { 99 | if (theme.colors && theme.colors.toolbar_field){ 100 | var rgbarray = theme.colors.toolbar_field.replace(/rgb\(/, '').replace(/\)/, '').split(', '); 101 | var luminance = (Math.max(parseInt(rgbarray[0]), parseInt(rgbarray[1]), parseInt(rgbarray[2])) + Math.min(parseInt(rgbarray[0]), parseInt(rgbarray[1]), parseInt(rgbarray[2]))) / 2; 102 | if (luminance < 120) iconsuffix = '-lightbg'; 103 | } 104 | }); 105 | 106 | /**** Fix Headers of Intercepted Responses ****/ 107 | function fixCT(details) { 108 | let serverCT = ''; 109 | // extract the Content-Type and Content-Disposition headers if present 110 | let contentTypeHeader, contentDispositionHeader; 111 | for (let header of details.responseHeaders) { 112 | switch (header.name.toLowerCase()) { 113 | case "content-type": 114 | contentTypeHeader = header; 115 | serverCT = contentTypeHeader.value; 116 | break; 117 | case "content-disposition": 118 | contentDispositionHeader = header; 119 | break; 120 | } 121 | } 122 | let filename = ''; 123 | // check requested file name (may be overridden by content disposition later) 124 | if (details.url !== undefined){ 125 | filename = details.url; 126 | filename = filename.substr(filename.lastIndexOf('/') + 1); 127 | if (filename.indexOf('?') > -1) filename = filename.substr(0, filename.indexOf('?')); 128 | if (filename.indexOf('#') > -1) filename = filename.substr(0, filename.indexOf('#')); 129 | } 130 | // check for filename in content disposition header (e.g., 'attachment; filename="blahblah.doc"') 131 | let cdaction = ''; 132 | if (contentDispositionHeader) { 133 | let sections = contentDispositionHeader.value.split(";"); 134 | for (var i=0; i -1) { 140 | // quote the filename (fix for bad IIS ASP code) 141 | cdaction += '\nC-D: Filename quoted due to spaces'; 142 | if (filename.startsWith('"')) parts[1] = parts[1].replace(filename, filename + '"'); 143 | else parts[1] = parts[1].replace(filename, '"' + filename + '"'); 144 | sections[i] = parts.join('='); 145 | } 146 | // console.log('filename from content-disposition => ' + filename); 147 | } else { 148 | if (oPrefs.dispoAction == 'inline' || (oPrefs.dispoAction == 'attachment' && (serverCT.indexOf('text/html') == -1 || oPrefs.excepthtml == false))){ 149 | if (sections[i].trim().toLowerCase() != oPrefs.dispoAction){ 150 | // Check for Google Drive/Gmail Exception v1.7.1 / Google Docs Exception v1.7.2 151 | if (oPrefs.dispoAction == 'inline' && details.type == 'sub_frame' && details.url.indexOf('googleusercontent.com') > -1 && 152 | ((details.documentUrl.indexOf('mail.google.com') > -1) || (details.documentUrl.indexOf('drive.google.com') > -1) || (details.documentUrl.indexOf('docs.google.com') > -1))){ 153 | cdaction += '\nC-D: Not changed from ' + sections[i] + ' due to Google Drive/Gmail/Docs attachment exception (retrieving from googleusercontent.com triggered from ' + details.documentUrl + ')'; 154 | } else { 155 | if (details.statusCode == 200){ // updated 1.7.3 156 | cdaction += '\nC-D: Changed from ' + sections[i] + ' to ' + oPrefs.dispoAction; 157 | sections[i] = sections[i].replace(/inline|attachment/i, oPrefs.dispoAction); 158 | } else { 159 | cdaction += '\nC-D: Not changed from ' + sections[i] + ' due to statusCode ' + details.statusCode; 160 | } 161 | } 162 | } 163 | } 164 | } 165 | } 166 | contentDispositionHeader.value = sections.join(';'); 167 | } else if (oPrefs.dispoAction == 'attachment' && (serverCT.indexOf('text/html') == -1 || oPrefs.excepthtml == false)){ 168 | // TODO Should we limit this by content type?? 169 | if (details.statusCode == 200){ // updated 1.7.3 170 | details.responseHeaders.push({ name: 'Content-Disposition', value: 'attachment' }); 171 | cdaction = '\nC-D: Forced to attachment'; 172 | } else { 173 | cdaction += '\nC-D: Not forced to attachment due to statusCode ' + details.statusCode; 174 | } 175 | } 176 | // if there's no discernible file name or file extension, exit now 177 | if (filename === '' || filename.lastIndexOf('.') < 0){ 178 | fixCTlog.enqueue({ 179 | time: Date.now(), 180 | url: details.url + ' (' + details.type + ')', 181 | extension: '', 182 | action: 'C-T: N/A - file extension unavailable' + cdaction 183 | }); 184 | return { responseHeaders: details.responseHeaders }; 185 | } 186 | 187 | let fileext = filename.substr(filename.lastIndexOf('.')+1); 188 | 189 | if (details.statusCode == 200){ 190 | // check file extension for known content-type 191 | let newCT = trueCT.find( objCT => objCT.ext === fileext ); 192 | 193 | // if we don't have anything to do, we're done 194 | if (newCT === undefined){ 195 | if (contentTypeHeader){ 196 | fixCTlog.enqueue({ 197 | time: Date.now(), 198 | url: details.url + ' (' + details.type + ')', 199 | extension: fileext, 200 | action: 'C-T: No Action - no association for extension; current CT is "' + contentTypeHeader.value + '"' + cdaction 201 | }); 202 | } else { 203 | fixCTlog.enqueue({ 204 | time: Date.now(), 205 | url: details.url + ' (' + details.type + ')', 206 | extension: fileext, 207 | action: 'C-T: No Action - no association for extension; CT absent' + cdaction 208 | }); 209 | } 210 | return { responseHeaders: details.responseHeaders }; 211 | } 212 | 213 | // fix the header 214 | if (contentTypeHeader){ 215 | if (contentTypeHeader.value !== newCT.ct){ 216 | if (serverCT.indexOf('text/html') == -1 || oPrefs.ctexcepthtml == false){ 217 | if (newCT.enabled === true){ 218 | fixCTlog.enqueue({ 219 | time: Date.now(), 220 | url: details.url + ' (' + details.type + ')', 221 | extension: fileext, 222 | action: 'C-T: Updated CT header from "' + contentTypeHeader.value + '" to "' + newCT.ct + '"' + cdaction 223 | }); 224 | contentTypeHeader.value = newCT.ct; 225 | } else { 226 | fixCTlog.enqueue({ 227 | time: Date.now(), 228 | url: details.url + ' (' + details.type + ')', 229 | extension: fileext, 230 | action: 'C-T: No Action - rule to update from "' + contentTypeHeader.value + '" to "' + newCT.ct + '" is disabled' + cdaction 231 | }); 232 | } 233 | } else { // text/html, presumably 234 | fixCTlog.enqueue({ 235 | time: Date.now(), 236 | url: details.url + ' (' + details.type + ')', 237 | extension: fileext, 238 | action: 'C-T: No Action - not overriding "' + contentTypeHeader.value + '" to "' + newCT.ct + '" (because HTML)' + cdaction 239 | }); 240 | } 241 | } else { 242 | fixCTlog.enqueue({ 243 | time: Date.now(), 244 | url: details.url + ' (' + details.type + ')', 245 | extension: fileext, 246 | action: 'C-T: No Action - CT already set to "' + contentTypeHeader.value + '"' + cdaction 247 | }); 248 | } 249 | } else { 250 | details.responseHeaders.push({ name: "Content-Type", value: newCT.ct }); 251 | fixCTlog.enqueue({ 252 | time: Date.now(), 253 | url: details.url + ' (' + details.type + ')', 254 | extension: fileext, 255 | action: 'C-T: Created CT header with value "' + newCT.ct + '"' + cdaction 256 | }); 257 | } 258 | } else { 259 | fixCTlog.enqueue({ 260 | time: Date.now(), 261 | url: details.url + ' (' + details.type + ')', 262 | extension: fileext, 263 | action: 'C-T: N/A - "' + details.statusLine + '"' + cdaction 264 | }); 265 | } 266 | 267 | // dispatch headers, we're done 268 | return { responseHeaders: details.responseHeaders }; 269 | } 270 | 271 | /**** CODE RELATED TO TOOLBAR BUTTON ****/ 272 | 273 | // Listen for button click and turn listener on/off accordingly 274 | browser.browserAction.onClicked.addListener((currTab) => { 275 | if (nowlistening) { // Show popup (this was previously set up) 276 | browser.browserAction.openPopup(); 277 | } else { // Enable listening/logging/popup 278 | // Create listener 279 | startListening(); 280 | } 281 | }); 282 | 283 | function startListening(){ 284 | browser.webRequest.onHeadersReceived.addListener( 285 | fixCT, 286 | { 287 | urls: [""], 288 | types: ["main_frame", "sub_frame", "other"] 289 | }, 290 | ["blocking", "responseHeaders"] 291 | ); 292 | nowlistening = true; 293 | // Update toolbar button 294 | setButton(); 295 | // Create a new log 296 | fixCTlog = new Queue(); 297 | // Change button behavior to show popup 298 | browser.browserAction.setPopup({popup: browser.extension.getURL('popup.html')}); 299 | } 300 | 301 | // when a window is focused, make sure it has the correct button 302 | browser.windows.onFocusChanged.addListener((wid) => { 303 | setButton(); 304 | }); 305 | 306 | // Update icon image and tooltip 307 | // https://www.emojione.com/emoji/1f528 ; https://www.emojione.com/licenses 308 | function setButton(){ 309 | if (nowlistening){ 310 | browser.browserAction.setIcon({ 311 | path: { 312 | 64: "icons/hammer-64-star" + iconsuffix + ".png" 313 | } 314 | }); 315 | browser.browserAction.setTitle({title: 'Turn Content-Type Fixer OFF or Add/Edit Content Types'}); 316 | } else { 317 | browser.browserAction.setIcon({ 318 | path: { 319 | 64: "icons/hammer-64-Zzzz" + iconsuffix + ".png" 320 | } 321 | }); 322 | browser.browserAction.setTitle({title: 'Turn Content-Type Fixer ON'}); 323 | } 324 | } 325 | 326 | /**** CODE RELATED TO MESSAGES FROM OTHER PAGES ****/ 327 | 328 | // Handle Popup menu and form actions 329 | function handleMessage(request, sender, sendResponse) { 330 | if ('popupaction' in request) { 331 | // do the user's bidding 332 | if (request.popupaction == "turnoff") { // Disable listening/logging/popup 333 | // Remove listener 334 | browser.webRequest.onHeadersReceived.removeListener(fixCT); 335 | nowlistening = false; 336 | // Update toolbar button 337 | setButton(); 338 | // Clear log 339 | fixCTlog = {}; 340 | // Remove popup from button 341 | browser.browserAction.setPopup({popup: ''}); 342 | return true; 343 | } else if (request.popupaction == 'addnew') { // Show log page 344 | var creating = browser.tabs.create({ url: '/logaddnew.html' }); 345 | return true; 346 | } 347 | } else if ('want' in request) { 348 | if (request.want == "log") { // Return log to log page 349 | if (Object.keys(fixCTlog).length > 0) { 350 | // Expire old entries 351 | expireQ(fixCTlog, 'time', oPrefs.intLogExpiration); 352 | sendResponse({ 353 | logarray: fixCTlog.lifo() 354 | }); 355 | } else { 356 | sendResponse({ 357 | error: 'No log available (not listening?)' 358 | }); 359 | } 360 | return true; 361 | } 362 | if (request.want == "CTarray") { // Return trueCT to log page 363 | sendResponse({ 364 | CTarray: trueCT 365 | }); 366 | return true; 367 | } 368 | if (request.want == "prefs") { // Return oPrefs to popup 369 | sendResponse({ 370 | prefs: oPrefs 371 | }); 372 | return true; 373 | } 374 | } else if ('prefupdate' in request){ 375 | // Take what the popup sends us, assume it's valid 376 | oPrefs = request.prefupdate; 377 | browser.storage.local.set({userprefs: oPrefs}) 378 | .catch((err) => {console.log('Error on browser.storage.local.set(): '+err.message);}); 379 | } else if ('update' in request) { 380 | // receive CT update, store to customCT and commit to storage, update trueCT 381 | var oChange = request['update']; 382 | if (customCT === undefined) customCT = []; 383 | //console.log('oChange => ' + JSON.stringify(oChange)); 384 | //console.log('customCT before => ' + JSON.stringify(customCT)); 385 | //console.log('trueCT before => ' + JSON.stringify(trueCT)); 386 | switch (oChange.action){ 387 | case 'add': 388 | // push to customCT 389 | customCT.push({ 390 | ext: oChange.ext, 391 | ct: oChange.ctype, 392 | builtin: false, 393 | enabled: true 394 | }); 395 | // push to trueCT 396 | trueCT.push({ 397 | ext: oChange.ext, 398 | ct: oChange.ctype, 399 | builtin: false, 400 | enabled: true 401 | }); 402 | break; 403 | case 'change': 404 | // get a reference to the existing element in customCT 405 | var oCustCT = customCT.find( objCT => objCT.ext === oChange.ext ); 406 | var oTrueCT = trueCT.find( objCT => objCT.ext === oChange.ext ); 407 | if (oCustCT){ 408 | if (oChange.revert){ 409 | // delete from customCT 410 | customCT.splice(customCT.findIndex( objCT => objCT.ext === oChange.ext ), 1); 411 | // revert in trueCT 412 | if (oTrueCT.builtin !== true && oTrueCT.builtin !== false){ 413 | oTrueCT.ct = oTrueCT.builtin; 414 | oTrueCT.builtin = true; 415 | } else { 416 | console.log('WTF: received revert but old ct not in trueCT.builtin!'); 417 | } 418 | } else { 419 | // modify ct in customCT 420 | oCustCT.ct = oChange.ctype; 421 | // modify ct in trueCT 422 | if (oTrueCT.builtin == true){ 423 | oTrueCT.builtin = oTrueCT.ct; 424 | oTrueCT.builtin = false; 425 | } 426 | oTrueCT.ct = oChange.ctype; 427 | } 428 | } else { 429 | // add as custom 430 | customCT.push({ 431 | ext: oChange.ext, 432 | ct: oChange.ctype, 433 | builtin: false, 434 | enabled: true 435 | }); 436 | if (oTrueCT){ 437 | // modify ct in trueCT 438 | if (oTrueCT.builtin == true){ 439 | oTrueCT.builtin = oTrueCT.ct; 440 | oTrueCT.builtin = false; 441 | } 442 | oTrueCT.ct = oChange.ctype; 443 | } else { 444 | // add to trueCT (this shouldn't happen?) 445 | trueCT.push({ 446 | ext: oChange.ext, 447 | ct: oChange.ctype, 448 | builtin: false, 449 | enabled: true 450 | }); 451 | } 452 | } 453 | break; 454 | case 'delete': 455 | // delete the existing element in customCT 456 | customCT.splice(customCT.findIndex( objCT => objCT.ext === oChange.ext ), 1); 457 | // cleanup trueCT 458 | var oTrueCT = trueCT.find( objCT => objCT.ext === oChange.ext ); 459 | if (oTrueCT.builtin === false){ 460 | // delete the custom element from trueCT 461 | trueCT.splice(trueCT.findIndex( objCT => objCT.ext === oChange.ext ), 1); 462 | } else if (oTrueCT.builtin !== true) { 463 | // restore default ct 464 | oTrueCT.ct = oTrueCT.builtin; 465 | oTrueCT.builtin = true; 466 | } 467 | break; 468 | } 469 | //console.log('customCT after => ' + JSON.stringify(customCT)); 470 | //console.log('trueCT after => ' + JSON.stringify(trueCT)); 471 | browser.storage.local.set({userCT: customCT}) 472 | .catch((err) => {console.log('Error on browser.storage.local.set(): '+err.message);}); 473 | } else if ('statuschg' in request) { 474 | // receive CT status change, store to customCT and commit to storage, update trueCT 475 | var oChange = request['statuschg']; 476 | if (customCT === undefined) customCT = []; 477 | //console.log('oChange => ' + JSON.stringify(oChange)); 478 | // modify ct in trueCT 479 | var oTrueCT = trueCT.find( objCT => objCT.ext === oChange.ext ); 480 | oTrueCT.enabled = oChange.newstatus; 481 | // modify ct in customCT 482 | var oCustCT = customCT.find( objCT => objCT.ext === oChange.ext ); 483 | if (oCustCT){ // update record 484 | oCustCT.enabled = oChange.newstatus; 485 | } else { // add record (version 1.6) 486 | customCT.push({ 487 | ext: oTrueCT.ext, 488 | ct: oTrueCT.ct, 489 | builtin: oTrueCT.builtin, 490 | enabled: oChange.newstatus 491 | }); 492 | } 493 | // Update storage 494 | browser.storage.local.set({userCT: customCT}) 495 | .catch((err) => {console.log('Error on browser.storage.local.set(): '+err.message);}); 496 | } 497 | } 498 | browser.runtime.onMessage.addListener(handleMessage); 499 | 500 | /**** CODE RELATED TO RESPONSE LOGGING / PURGING ****/ 501 | 502 | // Next function from Queue.js - Created by Kate Morley - http://code.iamkate.com/javascript/queues/ - CC0 License 503 | 504 | function Queue(){ 505 | var queue = [], offset = 0; 506 | this.getLength = function(){ return (queue.length - offset); } 507 | this.isEmpty = function(){ return (queue.length == 0); } 508 | this.enqueue = function(item){ queue.push(item); } 509 | this.dequeue = function(){ 510 | if (queue.length == 0) return undefined; 511 | var item = queue[offset]; 512 | // increment the offset and remove the free space if necessary 513 | if (++ offset * 2 >= queue.length){ 514 | queue = queue.slice(offset); 515 | offset = 0; 516 | } 517 | return item; 518 | } 519 | this.peek = function(){ return (queue.length > 0 ? queue[offset] : undefined); } 520 | this.lifo = function(){ 521 | var copy = queue.slice(offset); 522 | return copy.reverse(); 523 | } 524 | } 525 | 526 | // Clear expired items; default to 5 minutes 527 | 528 | function expireQ(oQueue, strTimeProp, intSecs){ 529 | // Check the parameters 530 | if (typeof oQueue !== 'object' || typeof strTimeProp === 'undefined'){ 531 | console.log('expireQ terminated due to a problem with the queue object name or a missing time property name'); 532 | return; 533 | } 534 | if (typeof intSecs === 'undefined' || isNaN(intSecs) || intSecs < 0) intSecs = (5 * 60); 535 | // Plow through the queue until no old items remain 536 | var oItem, dItem; 537 | while (!oQueue.isEmpty()){ 538 | oItem = oQueue.peek(); 539 | if (strTimeProp in oItem){ 540 | dItem = new Date(oItem[strTimeProp]); 541 | if (!isNaN(dItem.getTime())){ 542 | if ((Date.now() - dItem.getTime()) > (intSecs * 1000)){ 543 | oQueue.dequeue(); // remove item from queue 544 | } else break; // No more old items 545 | } else { 546 | console.log('expireQ terminated because this is not a valid date-time: ' + oItem[strTimeProp]); 547 | break; 548 | } 549 | } else { 550 | console.log('expireQ terminated because the ' + strTimeProp + ' property was not found in the queue item: ' + JSON.stringify(oItem)); 551 | break; 552 | } 553 | } 554 | } 555 | -------------------------------------------------------------------------------- /icons/hammer-16-Zzzz.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jscher2000/Content-Type-Fixer-extension/1a13a6007224949df6c05e421fe659e4efe43d3e/icons/hammer-16-Zzzz.png -------------------------------------------------------------------------------- /icons/hammer-16-star.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jscher2000/Content-Type-Fixer-extension/1a13a6007224949df6c05e421fe659e4efe43d3e/icons/hammer-16-star.png -------------------------------------------------------------------------------- /icons/hammer-32-Zzzz.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jscher2000/Content-Type-Fixer-extension/1a13a6007224949df6c05e421fe659e4efe43d3e/icons/hammer-32-Zzzz.png -------------------------------------------------------------------------------- /icons/hammer-32-star.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jscher2000/Content-Type-Fixer-extension/1a13a6007224949df6c05e421fe659e4efe43d3e/icons/hammer-32-star.png -------------------------------------------------------------------------------- /icons/hammer-64-Zzzz-lightbg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jscher2000/Content-Type-Fixer-extension/1a13a6007224949df6c05e421fe659e4efe43d3e/icons/hammer-64-Zzzz-lightbg.png -------------------------------------------------------------------------------- /icons/hammer-64-Zzzz.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jscher2000/Content-Type-Fixer-extension/1a13a6007224949df6c05e421fe659e4efe43d3e/icons/hammer-64-Zzzz.png -------------------------------------------------------------------------------- /icons/hammer-64-star-lightbg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jscher2000/Content-Type-Fixer-extension/1a13a6007224949df6c05e421fe659e4efe43d3e/icons/hammer-64-star-lightbg.png -------------------------------------------------------------------------------- /icons/hammer-64-star.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jscher2000/Content-Type-Fixer-extension/1a13a6007224949df6c05e421fe659e4efe43d3e/icons/hammer-64-star.png -------------------------------------------------------------------------------- /img/arrow-left.svg: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /img/off-status.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jscher2000/Content-Type-Fixer-extension/1a13a6007224949df6c05e421fe659e4efe43d3e/img/off-status.png -------------------------------------------------------------------------------- /img/on-status.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jscher2000/Content-Type-Fixer-extension/1a13a6007224949df6c05e421fe659e4efe43d3e/img/on-status.png -------------------------------------------------------------------------------- /logaddnew.css: -------------------------------------------------------------------------------- 1 | body { 2 | background-color: #f4f4f4; 3 | font-family: sans-serif; 4 | } 5 | p, td { 6 | font-size: 1.1em; 7 | } 8 | /* Log Table */ 9 | #CTLog { 10 | border-collapse: collapse; 11 | table-layout: fixed; 12 | } 13 | #CTLog td { 14 | border: 1px solid #000; 15 | vertical-align: top; 16 | padding: 4px; 17 | } 18 | #CTLog thead td { 19 | font-weight: bold; 20 | } 21 | .grayed { 22 | color: #888; 23 | } 24 | #btnRefresh { 25 | font-size: 0.75em; 26 | float: right; 27 | } 28 | h2 { 29 | font-size: 1.25em; 30 | } 31 | .addedit { 32 | margin: 6px; 33 | border: 1px solid #cef; 34 | border-radius: 16px; 35 | background-color: #def; 36 | padding: 0px 20px; 37 | } 38 | .addedit p, .addedit input { 39 | font-size: 0.9em; 40 | } 41 | .warning { 42 | color: #eee; 43 | background: #f88; 44 | padding: 2px 8px; 45 | } 46 | /* v1.7.1 (honor \n) */ 47 | td:nth-of-type(4) > span:nth-of-type(1) { 48 | white-space: pre-wrap; 49 | } -------------------------------------------------------------------------------- /logaddnew.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Header Log - Content-Type Fixer 7 | 8 | 9 |

Log of Recent Requests

10 |

Use the Add and Edit buttons to define or redefine a file extension - Content-Type association.

11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 |
Date/Time URLExt.Action Taken
24 |
25 | 26 | 27 | 41 | 57 | 58 | -------------------------------------------------------------------------------- /logaddnew.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020. Jefferson "jscher2000" Scher. License: MPL-2.0. 3 | v1.0 - added log and add/edit features for customizing content types 4 | v1.1 - minor fixes; use template to reduce insertAdjacentHTML 5 | */ 6 | 7 | /**** Set up Page -- Get Data from background.js -- Build Table ****/ 8 | 9 | let arrCT = []; 10 | function refreshLog(blnClear){ 11 | browser.runtime.sendMessage({ 12 | want: "log" 13 | }).then((oLog) => { 14 | var dest = document.querySelector('#logbody'); 15 | if (blnClear !== false) dest.innerHTML = ''; 16 | var arrItems = oLog.logarray; 17 | if (arrItems && arrItems.length > 0){ 18 | var arrExtensions = []; // track file extensions so only one action button appears per extension 19 | var newTR = document.getElementById('newTR'), clone, row, cells; 20 | for (var j=0; j -1){ 37 | // We're all set with the Add button 38 | } else { 39 | if (arrItems[j].action.indexOf('Updated CT header') > -1 || arrItems[j].action.indexOf('Created CT header') > -1 || 40 | arrItems[j].action.indexOf('CT already set') > -1){ 41 | // Switch to an Edit button 42 | cells[3].querySelector('button').setAttribute('fexttype', 'edit'); 43 | cells[3].querySelector('button').setAttribute('title', 'Edit content-type association'); 44 | cells[3].querySelector('button').textContent = 'Edit'; 45 | } else { 46 | // Remove the Add button?? Not sure how we got here 47 | //console.log('WTF'); 48 | cells[3].querySelector('button').remove(); 49 | } 50 | } 51 | arrExtensions.push(arrItems[j].extension); 52 | } else { 53 | // Remove the Add button 54 | cells[3].querySelector('button').remove(); 55 | } 56 | dest.appendChild(clone); 57 | } 58 | } else { // No log data to show in table, display textual message 59 | if (arrItems && arrItems.length > 0){ 60 | addErrorRow(dest, 'No requests have been logged in the past 5 minutes', 4); 61 | } else { 62 | addErrorRow(dest, 'No log data (listening is disabled? no downloads yet?)', 4); 63 | } 64 | } 65 | }).catch((err) => { // No log retrieved, display error message from promise 66 | addErrorRow(document.querySelector('#logbody'), 'Problem getting log: ' + err.message, 4); 67 | }); 68 | } 69 | function addErrorRow(tgt, msg, intSpan){ 70 | var cell = document.createElement('td'); 71 | cell.setAttribute('colspan', intSpan); 72 | cell.textContent = msg; 73 | var row = document.createElement('tr'); 74 | row.appendChild(cell); 75 | tgt.appendChild(row); 76 | } 77 | function getCTarray(){ 78 | browser.runtime.sendMessage({ 79 | want: "CTarray" 80 | }).then((oCTarray) => { 81 | arrCT = oCTarray.CTarray; 82 | }).catch((err) => { 83 | console.log('Problem getting CTarray: ' + err.message); 84 | }); 85 | } 86 | getCTarray(); 87 | refreshLog(false); 88 | document.querySelector('#btnRefresh').addEventListener('click', refreshLog, false); 89 | document.querySelector('#logbody').addEventListener('click', doButton, false); 90 | 91 | /**** Handle Button events that occur in the log table body (Add/Edit/Reset/etc) ****/ 92 | 93 | function doButton(evt){ 94 | var tgt = evt.target; 95 | if (tgt.nodeName !== 'BUTTON') return; 96 | if (tgt.hasAttribute('fexttype')){ // Add or Edit 97 | // Insert form from template and populate 98 | switch (evt.target.getAttribute('fexttype')){ 99 | case 'add': 100 | var t = document.querySelector('#frmAdd'); 101 | break; 102 | case 'edit': 103 | var t = document.querySelector('#frmEdit'); 104 | break; 105 | } 106 | var clone = document.importNode(t.content, true); 107 | var fext = tgt.getAttribute('fext'); 108 | clone.querySelector('form > div').setAttribute('fext', fext); 109 | var els = clone.querySelectorAll('span.fext'); 110 | for (var i=0; i 114 | tgt.style.display = 'none'; 115 | // Populate text input with current association, if applicable 116 | var newCT = arrCT.find( objCT => objCT.ext === fext ); 117 | if (newCT) { 118 | console.log('newCT => ' + JSON.stringify(newCT)); 119 | tgt.parentNode.querySelector('[name="customCT"]').setAttribute('value', newCT.ct); 120 | tgt.parentNode.querySelector('[name="ctType"][value="cust"]').checked = true; 121 | // if this is a built-in association, store that, and disable the Delete button 122 | if (newCT.builtin == true){ 123 | tgt.parentNode.querySelector('button.delbtn').disabled = true; 124 | } else { 125 | tgt.parentNode.querySelector('button.delbtn').setAttribute('currCT', newCT.ct); 126 | if (newCT.builtin !== false) { 127 | // display built-in content-type as one of the options 128 | tgt.parentNode.querySelector('span.builtinspan').style.display = ''; 129 | tgt.parentNode.querySelector('span.builtinspan span.string').textContent = newCT.builtin; 130 | } 131 | } 132 | } 133 | // Attach focus event to customCT input 134 | tgt.parentNode.querySelector('[name="customCT"]').addEventListener('focus', function(){ 135 | this.previousElementSibling.checked = true; 136 | }, false); 137 | } else if (tgt.className == 'reset') { // Clear text input changes 138 | tgt.previousElementSibling.value = tgt.previousElementSibling.getAttribute('value'); 139 | } else { // Non-submit form actions 140 | // Process Add, Modify, Delete buttons 141 | var dCtrls = tgt.closest('div.addedit'); 142 | if (dCtrls){ 143 | // Extract form details 144 | var fext = dCtrls.getAttribute('fext'); 145 | var oldCT = dCtrls.querySelector('[name="customCT"]').getAttribute('value'); 146 | var rads = dCtrls.querySelectorAll('input[type="radio"][name="ctType"]'); 147 | var axnCT = ''; 148 | var builtin = false; 149 | for (var i=0; i -1){ 165 | alert('Please enter a content-type without any spaces and try again.') 166 | return; 167 | } 168 | } else if (rads[i].value == "built"){ 169 | axnCT = dCtrls.querySelector('input[value="built"] + span.string').textContent; 170 | builtin = true; 171 | } 172 | break; 173 | } 174 | } 175 | // Send message to background script 176 | var oChange; 177 | switch (tgt.className){ 178 | case 'addbtn': 179 | oChange = { 180 | action: 'add', 181 | ext: fext, 182 | ctype: axnCT, 183 | revert: builtin 184 | } 185 | break; 186 | case 'modbtn': 187 | // No need to re-add the existing content-type 188 | if (axnCT == oldCT){ 189 | alert('This content-type is already set up!'); 190 | return; 191 | } 192 | oChange = { 193 | action: 'change', 194 | ext: fext, 195 | ctype: axnCT, 196 | revert: builtin 197 | } 198 | break; 199 | case 'delbtn': 200 | // Delete ignores the selected content-type (axnCT) 201 | if (confirm('Delete current content-type '+ tgt.getAttribute('currCT') + '?')){ 202 | oChange = { 203 | action: 'delete', 204 | ext: fext, 205 | ctype: axnCT, 206 | revert: builtin 207 | } 208 | } else { 209 | return; 210 | } 211 | break; 212 | } 213 | browser.runtime.sendMessage({ 214 | update: oChange 215 | }).then(() => { 216 | dCtrls.querySelector('.warning').style.display = ''; 217 | getCTarray(); // ISSUE: not running correctly? 218 | }).catch((err) => { 219 | console.log('Problem sending update: ' + err.message); 220 | }); 221 | } else { 222 | alert('Problem processing the form for some unknown reason. Sorry.'); 223 | } 224 | } 225 | } 226 | -------------------------------------------------------------------------------- /manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Content-Type Fixer", 3 | "description": "Override dumb Content-Type headers to fix download problems, and downloading forced by Content-Disposition: attachment.", 4 | "version": "1.7.4.1", 5 | "manifest_version": 2, 6 | "applications":{ 7 | "gecko": { 8 | "id": "{ff75dc6a-251e-42e2-955d-b33dd660a54c}", 9 | "strict_min_version": "60.0" 10 | } 11 | }, 12 | "background": { 13 | "scripts": [ 14 | "background.js" 15 | ] 16 | }, 17 | "permissions": [ 18 | "", 19 | "webRequest", 20 | "webRequestBlocking", 21 | "storage" 22 | ], 23 | "browser_action": { 24 | "browser_style": true, 25 | "default_title": "Turn Content-Type Fixer ON", 26 | "theme_icons": [{ 27 | "light": "icons/hammer-64-Zzzz-lightbg.png", 28 | "dark": "icons/hammer-64-Zzzz.png", 29 | "size": 64 30 | }] 31 | } 32 | } -------------------------------------------------------------------------------- /popup.css: -------------------------------------------------------------------------------- 1 | /* v1.7.4.1 */ 2 | body { 3 | background-color: #f2f2f2; 4 | margin: 4px; 5 | font-size: 16px; 6 | } 7 | h1 { 8 | font-size: 1.25em; 9 | margin: 0; 10 | padding: 4px 8px; 11 | background: #a8e8f8; 12 | } 13 | h2 { 14 | font-size: 1.1em; 15 | margin: 0.5em 0; 16 | } 17 | ul { 18 | list-style-type: none; 19 | margin: 0; 20 | padding: 0; 21 | } 22 | ul li { 23 | width: auto; 24 | padding: 4px 8px; 25 | cursor: pointer; 26 | background-color: #f8f8f8; 27 | margin: 2px 0; 28 | } 29 | ul li:hover { 30 | background-color: #b8d8f8; 31 | } 32 | ul li ul li { 33 | padding: 2px 4px; 34 | margin-left: 10px; 35 | } 36 | #dispohtml { 37 | padding-left: 20px; 38 | } 39 | #typehtml { 40 | padding-left: 15px; 41 | } 42 | ul li ul li:hover { 43 | background-color: #f8f8b8; 44 | } 45 | .hassubview { 46 | position: relative; 47 | } 48 | .hassubview::after { 49 | content: url(arrow-left.svg); /* chrome://browser/skin/arrow-left.svg */ 50 | position: absolute; 51 | right: 0; 52 | top: 3px; 53 | transform: scale(-1); 54 | opacity: 0.5; 55 | } 56 | .hassubview:hover::after { 57 | opacity: 1.0; 58 | } 59 | .subview { 60 | display: none; 61 | padding: 0 4px; 62 | margin-bottom: 4px; 63 | min-height: 350px; 64 | } 65 | .backtomain { 66 | position: relative; 67 | margin: 1px -4px; 68 | padding: 3px 4px; 69 | background-color: #b8d8f8; 70 | text-align: right; 71 | cursor: pointer; 72 | } 73 | .backtomain img { 74 | opacity: 0.5; 75 | vertical-align: sub; 76 | } 77 | .backtomain:hover img { 78 | opacity: 1.0; 79 | } 80 | 81 | #subview1 table { 82 | border: 1px solid #000; 83 | width: 100%; 84 | } 85 | #subview1 thead tr { 86 | color: white; 87 | background-color: darkblue; 88 | } 89 | #subview1 tbody tr:nth-of-type(odd) { 90 | background-color: #fff; 91 | } 92 | #subview1 tbody tr:nth-of-type(even) { 93 | background-color: #a8e8f8; 94 | } 95 | #subview1 th, #subview1 td { 96 | padding: 2px 6px; 97 | text-align: left; 98 | } 99 | #subview1 td[status="on"] { 100 | cursor: pointer; 101 | background: url(img/on-status.png) center no-repeat; 102 | } 103 | #subview1 td[status="off"] { 104 | cursor: pointer; 105 | background: url(img/off-status.png) center no-repeat; 106 | } 107 | .customct { 108 | color: #00f; 109 | } 110 | .credit { 111 | font-size: 0.6em; 112 | margin-bottom: 0; 113 | } -------------------------------------------------------------------------------- /popup.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Menu Popup – Content-Type Fixer 1.7 7 | 8 | 9 |

Content-Type Fixer

10 |
11 |
    12 |
  • Stop Listening & Clear Log
  • 13 |
  • Add/Edit Content Types (View Log)
  • 14 |
  • Enable/Disable Content Types
     Except text/html
  • 15 |
  •  Auto-start with Firefox
  • 16 |
  • Content-Disposition Override
      17 |
    •  As set by the server
    • 18 |
    •  Follow my settings (inline)
    • 19 |
    •  Force downloading (attachment)
       Except text/html
    • 20 |
  • 21 |
22 |
23 |
24 |

Back to Main Menu

25 |

Enable/Disable Content-Type Overrides

26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 |
On/OffExtensionContent-Type
37 |

On and off switches derived from On Off Switch Toggle Settings Preferences Free Icon licensed under CC BY 3.0.

38 |
39 | 40 | 41 | 46 | -------------------------------------------------------------------------------- /popup.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020. Jefferson "jscher2000" Scher. License: MPL-2.0. 3 | v1.0 - added drop-down menu for the toolbar button 4 | v1.1 - added content-disposition and autostart options 5 | v1.5 - support enable/disable of individual Content-Type overrides 6 | v1.6 - don't apply content-disposition: attachment to text/html unless user overrides 7 | v1.7 - don't apply content-type changes to text/html unless user overrides 8 | */ 9 | 10 | /**** Form setup ****/ 11 | let oPrefs = {}; 12 | function getPrefs(){ 13 | browser.runtime.sendMessage({ 14 | want: "prefs" 15 | }).then((oBGprefs) => { 16 | oPrefs = oBGprefs.prefs; 17 | document.querySelector('#cd' + oPrefs.dispoAction + ' > span').textContent = '☑'; 18 | if (oPrefs.autostart === true) document.querySelector('#autostart > span').textContent = '☑'; 19 | if (oPrefs.excepthtml === true) document.querySelector('#dispohtml > span').textContent = '☑'; 20 | if (oPrefs.ctexcepthtml === true) document.querySelector('#typehtml > span').textContent = '☑'; 21 | }).catch((err) => { 22 | console.log('Problem getting oPrefs: ' + err.message); 23 | }); 24 | } 25 | getPrefs(); 26 | 27 | let arrCT = []; 28 | function getCTarray(){ 29 | browser.runtime.sendMessage({ 30 | want: "CTarray" 31 | }).then((oCTarray) => { 32 | arrCT = oCTarray.CTarray; 33 | refreshCTtable(); 34 | }).catch((err) => { 35 | console.log('Problem getting CTarray: ' + err.message); 36 | }); 37 | } 38 | getCTarray(); 39 | 40 | function refreshCTtable(){ 41 | if (!arrCT || arrCT.length === 0) return; 42 | var dest = document.querySelector('#subview1 tbody'); 43 | // TODO option to clear previous tbody contents 44 | // Build string array for easier sorting 45 | var arrCTsortable = [], strTemp = ''; 46 | for (var j=0; j { 152 | console.log('Problem sending update: ' + err.message); 153 | }); 154 | } 155 | 156 | /*** Subviews ***/ 157 | function backToMain(el){ 158 | var subviewdiv = document.querySelector('#' + el); 159 | if (subviewdiv){ 160 | subviewdiv.style.display = ''; 161 | document.getElementById('mainview').style.display = ''; 162 | } 163 | } 164 | function subv1Click(evt){ 165 | var tgt = evt.target; 166 | if (tgt.classList.contains('backtomain')){ 167 | // We want to close this subview 168 | backToMain(tgt.getAttribute('closes')); 169 | return; 170 | } else if (tgt.hasAttribute('status')) { 171 | // Do Content-Type Override status change 172 | var oChange; 173 | if (tgt.getAttribute('status') === 'on'){ 174 | // Update cell attribute to OFF 175 | tgt.setAttribute('status', 'off'); 176 | // Prepare change object 177 | oChange = { 178 | ext: tgt.nextElementSibling.textContent, 179 | newstatus: false 180 | } 181 | } else { 182 | // Update cell attribute to ON 183 | tgt.setAttribute('status', 'on'); 184 | // Prepare change object 185 | oChange = { 186 | ext: tgt.nextElementSibling.textContent, 187 | newstatus: true 188 | } 189 | } 190 | // Send message to background 191 | browser.runtime.sendMessage({ 192 | statuschg: oChange 193 | }).catch((err) => { 194 | console.log('Problem sending update: ' + err.message); 195 | }); 196 | } else { 197 | // What is this? 198 | console.log(evt); 199 | } 200 | } 201 | document.getElementById('subview1').addEventListener('click', subv1Click, false); --------------------------------------------------------------------------------