├── .gitignore ├── LICENSE ├── README.md ├── background ├── index.js ├── tabs.js └── tst.js ├── build-config.js ├── common └── options.js ├── dim-unloaded-tabs.css ├── icon.png ├── images ├── get-ff-ext.png └── tab-context-menu.png ├── many.png ├── package.json ├── update └── extension │ └── installed.js └── views └── installed.html /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | package-lock.json 4 | /build/ 5 | files.json 6 | manifest.json 7 | UnloadTabs 8 | UnloadTabs.html 9 | view.html 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License, version 2.0 2 | 3 | 1. Definitions 4 | 5 | 1.1. "Contributor" 6 | 7 | means each individual or legal entity that creates, contributes to the 8 | creation of, or owns Covered Software. 9 | 10 | 1.2. "Contributor Version" 11 | 12 | means the combination of the Contributions of others (if any) used by a 13 | Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | 17 | means Covered Software of a particular Contributor. 18 | 19 | 1.4. "Covered Software" 20 | 21 | means Source Code Form to which the initial Contributor has attached the 22 | notice in Exhibit A, the Executable Form of such Source Code Form, and 23 | Modifications of such Source Code Form, in each case including portions 24 | thereof. 25 | 26 | 1.5. "Incompatible With Secondary Licenses" 27 | means 28 | 29 | a. that the initial Contributor has attached the notice described in 30 | Exhibit B to the Covered Software; or 31 | 32 | b. that the Covered Software was made available under the terms of 33 | version 1.1 or earlier of the License, but not also under the terms of 34 | a Secondary License. 35 | 36 | 1.6. "Executable Form" 37 | 38 | means any form of the work other than Source Code Form. 39 | 40 | 1.7. "Larger Work" 41 | 42 | means a work that combines Covered Software with other material, in a 43 | separate file or files, that is not Covered Software. 44 | 45 | 1.8. "License" 46 | 47 | means this document. 48 | 49 | 1.9. "Licensable" 50 | 51 | means having the right to grant, to the maximum extent possible, whether 52 | at the time of the initial grant or subsequently, any and all of the 53 | rights conveyed by this License. 54 | 55 | 1.10. "Modifications" 56 | 57 | means any of the following: 58 | 59 | a. any file in Source Code Form that results from an addition to, 60 | deletion from, or modification of the contents of Covered Software; or 61 | 62 | b. any new file in Source Code Form that contains any Covered Software. 63 | 64 | 1.11. "Patent Claims" of a Contributor 65 | 66 | means any patent claim(s), including without limitation, method, 67 | process, and apparatus claims, in any patent Licensable by such 68 | Contributor that would be infringed, but for the grant of the License, 69 | by the making, using, selling, offering for sale, having made, import, 70 | or transfer of either its Contributions or its Contributor Version. 71 | 72 | 1.12. "Secondary License" 73 | 74 | means either the GNU General Public License, Version 2.0, the GNU Lesser 75 | General Public License, Version 2.1, the GNU Affero General Public 76 | License, Version 3.0, or any later versions of those licenses. 77 | 78 | 1.13. "Source Code Form" 79 | 80 | means the form of the work preferred for making modifications. 81 | 82 | 1.14. "You" (or "Your") 83 | 84 | means an individual or a legal entity exercising rights under this 85 | License. For legal entities, "You" includes any entity that controls, is 86 | controlled by, or is under common control with You. For purposes of this 87 | definition, "control" means (a) the power, direct or indirect, to cause 88 | the direction or management of such entity, whether by contract or 89 | otherwise, or (b) ownership of more than fifty percent (50%) of the 90 | outstanding shares or beneficial ownership of such entity. 91 | 92 | 93 | 2. License Grants and Conditions 94 | 95 | 2.1. Grants 96 | 97 | Each Contributor hereby grants You a world-wide, royalty-free, 98 | non-exclusive license: 99 | 100 | a. under intellectual property rights (other than patent or trademark) 101 | Licensable by such Contributor to use, reproduce, make available, 102 | modify, display, perform, distribute, and otherwise exploit its 103 | Contributions, either on an unmodified basis, with Modifications, or 104 | as part of a Larger Work; and 105 | 106 | b. under Patent Claims of such Contributor to make, use, sell, offer for 107 | sale, have made, import, and otherwise transfer either its 108 | Contributions or its Contributor Version. 109 | 110 | 2.2. Effective Date 111 | 112 | The licenses granted in Section 2.1 with respect to any Contribution 113 | become effective for each Contribution on the date the Contributor first 114 | distributes such Contribution. 115 | 116 | 2.3. Limitations on Grant Scope 117 | 118 | The licenses granted in this Section 2 are the only rights granted under 119 | this License. No additional rights or licenses will be implied from the 120 | distribution or licensing of Covered Software under this License. 121 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 122 | Contributor: 123 | 124 | a. for any code that a Contributor has removed from Covered Software; or 125 | 126 | b. for infringements caused by: (i) Your and any other third party's 127 | modifications of Covered Software, or (ii) the combination of its 128 | Contributions with other software (except as part of its Contributor 129 | Version); or 130 | 131 | c. under Patent Claims infringed by Covered Software in the absence of 132 | its Contributions. 133 | 134 | This License does not grant any rights in the trademarks, service marks, 135 | or logos of any Contributor (except as may be necessary to comply with 136 | the notice requirements in Section 3.4). 137 | 138 | 2.4. Subsequent Licenses 139 | 140 | No Contributor makes additional grants as a result of Your choice to 141 | distribute the Covered Software under a subsequent version of this 142 | License (see Section 10.2) or under the terms of a Secondary License (if 143 | permitted under the terms of Section 3.3). 144 | 145 | 2.5. Representation 146 | 147 | Each Contributor represents that the Contributor believes its 148 | Contributions are its original creation(s) or it has sufficient rights to 149 | grant the rights to its Contributions conveyed by this License. 150 | 151 | 2.6. Fair Use 152 | 153 | This License is not intended to limit any rights You have under 154 | applicable copyright doctrines of fair use, fair dealing, or other 155 | equivalents. 156 | 157 | 2.7. Conditions 158 | 159 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in 160 | Section 2.1. 161 | 162 | 163 | 3. Responsibilities 164 | 165 | 3.1. Distribution of Source Form 166 | 167 | All distribution of Covered Software in Source Code Form, including any 168 | Modifications that You create or to which You contribute, must be under 169 | the terms of this License. You must inform recipients that the Source 170 | Code Form of the Covered Software is governed by the terms of this 171 | License, and how they can obtain a copy of this License. You may not 172 | attempt to alter or restrict the recipients' rights in the Source Code 173 | Form. 174 | 175 | 3.2. Distribution of Executable Form 176 | 177 | If You distribute Covered Software in Executable Form then: 178 | 179 | a. such Covered Software must also be made available in Source Code Form, 180 | as described in Section 3.1, and You must inform recipients of the 181 | Executable Form how they can obtain a copy of such Source Code Form by 182 | reasonable means in a timely manner, at a charge no more than the cost 183 | of distribution to the recipient; and 184 | 185 | b. You may distribute such Executable Form under the terms of this 186 | License, or sublicense it under different terms, provided that the 187 | license for the Executable Form does not attempt to limit or alter the 188 | recipients' rights in the Source Code Form under this License. 189 | 190 | 3.3. Distribution of a Larger Work 191 | 192 | You may create and distribute a Larger Work under terms of Your choice, 193 | provided that You also comply with the requirements of this License for 194 | the Covered Software. If the Larger Work is a combination of Covered 195 | Software with a work governed by one or more Secondary Licenses, and the 196 | Covered Software is not Incompatible With Secondary Licenses, this 197 | License permits You to additionally distribute such Covered Software 198 | under the terms of such Secondary License(s), so that the recipient of 199 | the Larger Work may, at their option, further distribute the Covered 200 | Software under the terms of either this License or such Secondary 201 | License(s). 202 | 203 | 3.4. Notices 204 | 205 | You may not remove or alter the substance of any license notices 206 | (including copyright notices, patent notices, disclaimers of warranty, or 207 | limitations of liability) contained within the Source Code Form of the 208 | Covered Software, except that You may alter any license notices to the 209 | extent required to remedy known factual inaccuracies. 210 | 211 | 3.5. Application of Additional Terms 212 | 213 | You may choose to offer, and to charge a fee for, warranty, support, 214 | indemnity or liability obligations to one or more recipients of Covered 215 | Software. However, You may do so only on Your own behalf, and not on 216 | behalf of any Contributor. You must make it absolutely clear that any 217 | such warranty, support, indemnity, or liability obligation is offered by 218 | You alone, and You hereby agree to indemnify every Contributor for any 219 | liability incurred by such Contributor as a result of warranty, support, 220 | indemnity or liability terms You offer. You may include additional 221 | disclaimers of warranty and limitations of liability specific to any 222 | jurisdiction. 223 | 224 | 4. Inability to Comply Due to Statute or Regulation 225 | 226 | If it is impossible for You to comply with any of the terms of this License 227 | with respect to some or all of the Covered Software due to statute, 228 | judicial order, or regulation then You must: (a) comply with the terms of 229 | this License to the maximum extent possible; and (b) describe the 230 | limitations and the code they affect. Such description must be placed in a 231 | text file included with all distributions of the Covered Software under 232 | this License. Except to the extent prohibited by statute or regulation, 233 | such description must be sufficiently detailed for a recipient of ordinary 234 | skill to be able to understand it. 235 | 236 | 5. Termination 237 | 238 | 5.1. The rights granted under this License will terminate automatically if You 239 | fail to comply with any of its terms. However, if You become compliant, 240 | then the rights granted under this License from a particular Contributor 241 | are reinstated (a) provisionally, unless and until such Contributor 242 | explicitly and finally terminates Your grants, and (b) on an ongoing 243 | basis, if such Contributor fails to notify You of the non-compliance by 244 | some reasonable means prior to 60 days after You have come back into 245 | compliance. Moreover, Your grants from a particular Contributor are 246 | reinstated on an ongoing basis if such Contributor notifies You of the 247 | non-compliance by some reasonable means, this is the first time You have 248 | received notice of non-compliance with this License from such 249 | Contributor, and You become compliant prior to 30 days after Your receipt 250 | of the notice. 251 | 252 | 5.2. If You initiate litigation against any entity by asserting a patent 253 | infringement claim (excluding declaratory judgment actions, 254 | counter-claims, and cross-claims) alleging that a Contributor Version 255 | directly or indirectly infringes any patent, then the rights granted to 256 | You by any and all Contributors for the Covered Software under Section 257 | 2.1 of this License shall terminate. 258 | 259 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user 260 | license agreements (excluding distributors and resellers) which have been 261 | validly granted by You or Your distributors under this License prior to 262 | termination shall survive termination. 263 | 264 | 6. Disclaimer of Warranty 265 | 266 | Covered Software is provided under this License on an "as is" basis, 267 | without warranty of any kind, either expressed, implied, or statutory, 268 | including, without limitation, warranties that the Covered Software is free 269 | of defects, merchantable, fit for a particular purpose or non-infringing. 270 | The entire risk as to the quality and performance of the Covered Software 271 | is with You. Should any Covered Software prove defective in any respect, 272 | You (not any Contributor) assume the cost of any necessary servicing, 273 | repair, or correction. This disclaimer of warranty constitutes an essential 274 | part of this License. No use of any Covered Software is authorized under 275 | this License except under this disclaimer. 276 | 277 | 7. Limitation of Liability 278 | 279 | Under no circumstances and under no legal theory, whether tort (including 280 | negligence), contract, or otherwise, shall any Contributor, or anyone who 281 | distributes Covered Software as permitted above, be liable to You for any 282 | direct, indirect, special, incidental, or consequential damages of any 283 | character including, without limitation, damages for lost profits, loss of 284 | goodwill, work stoppage, computer failure or malfunction, or any and all 285 | other commercial damages or losses, even if such party shall have been 286 | informed of the possibility of such damages. This limitation of liability 287 | shall not apply to liability for death or personal injury resulting from 288 | such party's negligence to the extent applicable law prohibits such 289 | limitation. Some jurisdictions do not allow the exclusion or limitation of 290 | incidental or consequential damages, so this exclusion and limitation may 291 | not apply to You. 292 | 293 | 8. Litigation 294 | 295 | Any litigation relating to this License may be brought only in the courts 296 | of a jurisdiction where the defendant maintains its principal place of 297 | business and such litigation shall be governed by laws of that 298 | jurisdiction, without reference to its conflict-of-law provisions. Nothing 299 | in this Section shall prevent a party's ability to bring cross-claims or 300 | counter-claims. 301 | 302 | 9. Miscellaneous 303 | 304 | This License represents the complete agreement concerning the subject 305 | matter hereof. If any provision of this License is held to be 306 | unenforceable, such provision shall be reformed only to the extent 307 | necessary to make it enforceable. Any law or regulation which provides that 308 | the language of a contract shall be construed against the drafter shall not 309 | be used to construe this License against a Contributor. 310 | 311 | 312 | 10. Versions of the License 313 | 314 | 10.1. New Versions 315 | 316 | Mozilla Foundation is the license steward. Except as provided in Section 317 | 10.3, no one other than the license steward has the right to modify or 318 | publish new versions of this License. Each version will be given a 319 | distinguishing version number. 320 | 321 | 10.2. Effect of New Versions 322 | 323 | You may distribute the Covered Software under the terms of the version 324 | of the License under which You originally received the Covered Software, 325 | or under the terms of any subsequent version published by the license 326 | steward. 327 | 328 | 10.3. Modified Versions 329 | 330 | If you create software not governed by this License, and you want to 331 | create a new license for such software, you may create and use a 332 | modified version of this License if you rename the license and remove 333 | any references to the name of the license steward (except to note that 334 | such modified license differs from this License). 335 | 336 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 337 | Licenses If You choose to distribute Source Code Form that is 338 | Incompatible With Secondary Licenses under the terms of this version of 339 | the License, the notice described in Exhibit B of this License must be 340 | attached. 341 | 342 | Exhibit A - Source Code Form License Notice 343 | 344 | This Source Code Form is subject to the 345 | terms of the Mozilla Public License, v. 346 | 2.0. If a copy of the MPL was not 347 | distributed with this file, You can 348 | obtain one at 349 | http://mozilla.org/MPL/2.0/. 350 | 351 | If it is not possible or desirable to put the notice in a particular file, 352 | then You may include the notice in a location (such as a LICENSE file in a 353 | relevant directory) where a recipient would be likely to look for such a 354 | notice. 355 | 356 | You may add additional accurate notices of copyright ownership. 357 | 358 | Exhibit B - "Incompatible With Secondary Licenses" Notice 359 | 360 | This Source Code Form is "Incompatible 361 | With Secondary Licenses", as defined by 362 | the Mozilla Public License, v. 2.0. 363 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Unload Tabs 3 | 4 | ## Description 5 | 6 | 7 | Features: 8 | 27 | 28 | BUGS: 29 | Firefox currently suffers from a number of bugs related to unloading tabs. Most notably, tabs may lose their icons or display as loading. Fixing that is Mozilla's job. 30 | If you encounter any other problems, please report them as an issue or comment on an existing issue matching your problem. Please don't complain in a rating on the Add-ons download page. I can't respond to those. 31 | 32 | Permissions used: 36 | 37 | 38 | ## Development builds -- ![](https://ci.appveyor.com/api/projects/status/github/NiklasGollenstede/unload-tabs?svg=true) 39 | 40 | Development builds are automatically created on every commit with [appveyor](https://ci.appveyor.com/project/NiklasGollenstede/unload-tabs/history) and [released](https://github.com/NiklasGollenstede/epub-creator/releases) on GitHub.\ 41 | These builds use a different id (`-dev` suffix), so they are installed as an additional extension and do not replace the release version. This means that: 42 | * you probably want to disable the release version while the development version is active 43 | * any options set are managed individually (so pre-release versions can't mess with your settings) 44 | * they never update to release versions, but 45 | * they update themselves to the latest development version 46 | * every release version has a corresponding development version (the one with the same prefix and highest build number) 47 | -------------------------------------------------------------------------------- /background/index.js: -------------------------------------------------------------------------------- 1 | (function(global) { 'use strict'; define(async ({ // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 2 | 'node_modules/web-ext-utils/browser/': { Menus, Commands, Windows, Tabs: _Tabs, }, 3 | 'node_modules/web-ext-utils/utils/notify': notify, 4 | 'node_modules/web-ext-utils/update/': updated, 5 | 'common/options': options, 6 | Tabs, tst, 7 | require, module, 8 | }) => { /* global setTimeout, */ 9 | let debug, debug2; options.debug.whenChange(([ value, ]) => { debug = value; debug2 = value >= 2; }); 10 | Object.assign(global, { Browser: require('node_modules/web-ext-utils/browser/'), options, Tabs, tst, }); 11 | debug && console.info('Ran updates', updated); 12 | 13 | 14 | /** 15 | * Firefox bugs affecting this extension (FF60): 16 | * * [BUG] tabs can be put in a state { discarded: true, state: 'loading', } which they don't leave automatically (might only happening when discarding a tab that is about to load after sessionrestore) 17 | * * reported as #1450371 18 | * * [BAD] loading discarded/pending tabs removes the favicon, which will be missing when the tab is discarded again before the favicon is restored 19 | * * reported as #1450382 20 | * * sometimes (very rarely), the tab is even displayed and reported as a blank tab { title: 'New Tab', url: 'about:newtab/blank', } 21 | * * [BUG] tabs.onUpdated doesn't always report favIconUrl (when restoring discarded tabs) 22 | * * reported as #1450384 23 | * * [API] favIconUrl can't be set 24 | * * requested in #1450386 25 | * * [BUG?] never loaded tabs are not discarded (but internally pending) 26 | * * this is supposed to be fixed, but it does (sometimes?) happen in FF60 27 | * * as a consequence, onUpdated(, { discarded: false, }) won't fire 28 | * Also: 29 | * * [BUG?] calling tabs.executeScript() for discarded tabs loads them 30 | * * [BUG] calling tabs.executeScript() for never-loaded tabs only resolves after the tab is manually loaded (should reject or behave as if the tab was discarded) 31 | */ 32 | 33 | /** 34 | * Interesting issues (https://bugzilla.mozilla.org/show_bug.cgi?id=): 35 | * * [1420681]: let `Tabs.discard( , { forceDiscard:true, })`` discard tabs with 'beforeunload' handlers 36 | * * [1303384]: UI for re-assigning an extension's command shortcut 37 | * * [1320332]: Support overriding existing keybinding through WebExtensions (e.g. allow "Ctrl+Page(Up|Down)" or "Ctrl(+Shift)+Tab") 38 | */ 39 | 40 | 41 | // only keep track of Tabs while options.onClose.value is true 42 | let onClose = false; options.onClose.whenChange(([ value, ]) => { 43 | onClose = value; Tabs.setEnabled(onClose); 44 | }); 45 | 46 | 47 | // add menus 48 | const menus = { 49 | unloadTab: { 50 | title: 'Unload Tab', 51 | icons: { 64: 'icon.png', }, 52 | contexts: [ 'tab', 'tools_menu', ], 53 | }, 54 | unloadOtherTabs: { 55 | title: 'Unload Other Tabs', 56 | icons: { 32: 'many.png', }, 57 | contexts: options.menus.children.unloadOtherTabs.value.split(' '), 58 | }, 59 | unloadAllTabs: { 60 | title: 'Unload in All Windows', 61 | contexts: options.menus.children.unloadAllTabs.value.split(' '), 62 | }, 63 | }; Object.keys(menus).forEach(id => (menus[id].id = id)); 64 | Object.values(menus).forEach(menu => Menus.create(menu)); 65 | options.menus.children.unloadOtherTabs.onChange(updateMenu); 66 | options.menus.children.unloadAllTabs.onChange(updateMenu); 67 | function updateMenu([ value, ], _, { name, }) { 68 | menus[name].contexts = value.split(' '); 69 | Menus.update(name, { contexts: value.split(' '), }); 70 | } 71 | // could use .onShown and .update(, { enabled, }) .refresh() 72 | 73 | 74 | // Tree Style Tab integration 75 | options['intregrate.tst'].value && tst.enable(); 76 | options['intregrate.tst'].onChange(([ value, ]) => value ? tst.enable() : tst.disable()); 77 | 78 | 79 | // respond to menu click 80 | addWrappedListener(Menus, onClicked); 81 | async function onClicked({ menuItemId, }, { id, active, windowId, pinned, highlighted, }) { 82 | const ids = highlighted ? (await Tabs.queryAsync({ windowId, highlighted, })).map(_=>_.id) : [ id, ]; 83 | 84 | // Bug. Not sure when, why and whether still this happens 85 | const unload = tabs => { Tabs.discard(tabs.map(_=>_.id)).catch(error => { 86 | const match = (/^Invalid tab ID: (\d+)$/).exec(error && error.message); 87 | if (!match || !Tabs.delete(+match[1])) { throw error; } 88 | debug && console.wran(`[BUG] .onRemoved for tab ${match[1]} was never fired`); 89 | onClicked.apply(null, arguments); // `unload` must not have its own `arguments` 90 | }); }; 91 | 92 | switch (menuItemId) { 93 | case 'unloadTab': { 94 | if (active || ids.length > 1) { 95 | const tabs = (await Tabs.queryAsync({ windowId, })).filter(tab => tab.active || !ids.includes(tab.id)); 96 | const i = tabs.findIndex(_=>_.active); 97 | const alt = findNext(tabs[i], tabs) || !onClose && (tabs[i + 1] || tabs[i - 1]); 98 | if (alt) { (await Tabs.update(alt.id, { active: true, })); } 99 | else { notify.info('Not unloading', 'No Tab to switch to'); return; } 100 | } 101 | discarding = ids; setTimeout(() => discarding === ids && (discarding = null), 500); 102 | (await Tabs.discard(ids)); 103 | (await sleep(1000)); 104 | (await Promise.all(ids.map(id=>Tabs.getAsync(id)))).some(_=>!_.discarded) && notify.warn( 105 | 'Failed to unload tab', 106 | `Some browser UI tabs and tabs with prompts on close can't be unloaded.`, 107 | ); 108 | } break; 109 | case 'unloadOtherTabs': { 110 | unload((await Tabs.queryAsync({ 111 | discarded: false, windowId, pinned: pinned ? undefined : false, 112 | })).filter(tab => tab.id !== id && !ids.includes(tab.id))); 113 | } break; 114 | case 'unloadAllTabs': { 115 | unload((await Tabs.queryAsync({ 116 | discarded: false, 117 | }))); 118 | } break; 119 | case 'unloadTree': { 120 | unload( 121 | (await Promise.all(ids.map(id => tst.getChildren(id)))) 122 | .flat().filter(tab => !ids.includes(tab.id)) // for consistency, unload none of the selected roots, user can just follow up with `unloadTab` on the same selection 123 | ); 124 | } break; 125 | } 126 | } 127 | // BUG[FF60]: tab will report as loading and non-discarded directly after discarding, 128 | // but that doesn't reflect in the UI. Discarding it again fixes the tab state 129 | let discarding = null; addWrappedListener(_Tabs, function onUpdated(id, change) { 130 | if (!discarding || !discarding.includes(id) || change.discarded !== false) { return; } 131 | debug && console.warn('[BUG] just-discarded tab updating as non-discarded', id); 132 | Tabs.discard(id); 133 | }); 134 | 135 | 136 | // respond to (keyboard) commands 137 | Commands && addWrappedListener(Commands, onCommand); 138 | async function onCommand(command) { { 139 | debug2 && console.log('command', command); 140 | } switch (command.replace(/_\d$/, '')) { 141 | case 'unloadSelectedTab': (await onClicked({ menuItemId: 'unloadTab', }, (await Tabs.queryAsync({ 142 | active: true, windowId: (await Windows.getLastFocused({ windowTypes: [ 'normal', ], })).id, 143 | }))[0])); break; 144 | case 'prevLoadedTab': (await seekNext(-1)); break; 145 | case 'nextLoadedTab': (await seekNext(+1)); break; 146 | } } 147 | async function seekNext(direction) { 148 | const window = (await Windows.getLastFocused({ windowTypes: [ 'normal', ], populate: !onClose, })); 149 | const tabs = (window.tabs || Tabs.query({ windowId: window.id, discarded: false, hidden: false, })).sort((a, b) => a.index - b.index); 150 | const start = tabs.findIndex(_=>_.active); if (start < 0) { return; } 151 | 152 | function find(tab) { return tab && !tab.discarded && !tab.hidden && (alt = tab) || debug2 && void console.log('skipping tab', clone(tab)); } 153 | function increment(index) { return (index + direction + tabs.length) % tabs.length; } 154 | let alt; for ( // search in one direction, wrap around and return the original tab if no other is found 155 | let i = increment(start); 156 | i !== start && !find(tabs[i]); 157 | i = increment(i) 158 | ) { void 0; } 159 | 160 | alt && (await Tabs.update(alt.id, { active: true, })); 161 | } 162 | options.commands.onAnyChange(async (values, _, { name, model: { maxLength, }, }) => { 163 | const commands = (await Commands.getAll()); 164 | for (let i = 0; i < maxLength; ++i) { 165 | const id = name + (i ? '_'+ i : ''), command = commands.find(_=>_.name === id); 166 | command.shortcut = values[i] || null; 167 | if (command.shortcut) { try { 168 | (await Commands.update(command)); 169 | } catch (error) { 170 | Commands.reset(id); throw error; 171 | } } else { 172 | Commands.reset(id); // can't remove, so must only allow not to set if default is unset 173 | } 174 | } 175 | }); 176 | 177 | 178 | // respond to tab close 179 | let activating = null, restoring = null; 180 | Tabs.onRemoved(async (tab, { isWindowClosing, }) => { 181 | if (isWindowClosing || !tab.active && !restoring) { return; } 182 | debug2 && console.log('active tab closing', tab.id, tab); 183 | 184 | const alt = findNext(tab); if (!alt) { return; } 185 | debug && console.info('activating', alt.id); 186 | 187 | activating = alt.id; setTimeout(() => activating === alt.id && (activating = null), 500); 188 | options.onClose.children.preemptive.value && Tabs.update(alt.id, { active: true, }); 189 | if (restoring) { forceActivate(alt.id); forceDiscard(restoring); } 190 | }); 191 | Tabs.onUpdated(async (tab, change) => { 192 | if (change.active === true && activating && activating !== tab.id) { 193 | debug && console.warn('wrong tab focusing', tab.id, clone(tab)); 194 | 195 | (await forceActivate(activating)); 196 | } 197 | if (change.discarded === false && !tab.active) { 198 | debug && console.warn('inactive tab restoring', tab.id, clone(tab)); 199 | 200 | tab.restoring = true; setTimeout(() => (tab.restoring = false), 500); // TODO: wait for status === 'complete'? 201 | if (!activating) { 202 | restoring = tab.id; setTimeout(() => restoring === tab.id && (restoring = null), 500); 203 | } else { (await forceDiscard(tab.id)); } 204 | } 205 | }); 206 | async function forceActivate(id) { 207 | debug && console.info('start force activate', id); 208 | Tabs.update(id, { active: true, }); 209 | for (const time of [ 6, 12, 25, 45, /*70, 120,*/ ]) { (await sleep(time)); 210 | Tabs.update(id, { active: true, }); debug && console.info('force activate', id); 211 | } 212 | } 213 | async function forceDiscard(id) { 214 | debug && console.info('start force discard', id); 215 | Tabs.discard(id); 216 | for (const time of [ 6, 12, 25, 45, /*70, 120,*/ ]) { (await sleep(time)); 217 | Tabs.discard(id); debug && console.info('force discard', id); 218 | } 219 | } 220 | // restoring tabs doesn't do any webRequests and webNavigation can't be canceled 221 | 222 | 223 | // get next loaded tab (on close or unload) 224 | function findNext(tab, tabs) { const { windowId, } = tab; 225 | debug2 && console.log('findNext', ...arguments); 226 | let found = null; function find(tab) { if ( 227 | tab && !tab.discarded && !tab.hidden && !tab.restoring 228 | ) { found = tab; return true; } return false; } 229 | 230 | if (options.onClose.children.previous.value) { 231 | if (find(Tabs.previous(windowId))) { return found; } 232 | } 233 | 234 | tabs = (tabs || Tabs.query({ 235 | windowId: tab.windowId, discarded: false, hidden: false, restoring: false, 236 | })).sort((a, b) => a.index - b.index); 237 | let start = tabs.indexOf(tab); if (start < 0) { 238 | while (++start < tabs.length && tabs[start].index < tab.index) { void 0; } 239 | tabs.splice(start, 0, null); 240 | } 241 | const direction = options.onClose.children.direction.value; 242 | // debug2 && console.log(clone(tabs), tab, start); 243 | 244 | for ( // search up and down at the same time. No need to wrap around 245 | let j = start + direction, i = start - direction, length = tabs.length; 246 | (j >= 0 && j < length || i >= 0 && i < length) && !(find(tabs[j]) || find(tabs[i])); 247 | j += direction, i -= direction 248 | ) { void 0; } 249 | return found; 250 | } 251 | 252 | 253 | // utils 254 | function sleep(time) { 255 | return new Promise(done => setTimeout(done, time)); 256 | } 257 | function clone(arg) { 258 | return JSON.parse(JSON.stringify(arg)); 259 | } 260 | 261 | function addWrappedListener(api, func) { 262 | api[func.name].addListener(func.wrapped || (func.wrapped = async function() { try { 263 | (await func.apply(this, arguments)); 264 | } catch (error) { notify.error(`Failed to handle ${func.name}`, error); } })); 265 | } 266 | /*function removeWrappedListener(api, func) { 267 | func.wrapped && api[func.name].removeListener(func.wrapped); 268 | }*/ 269 | 270 | 271 | module.exports = { 272 | menus, 273 | onClicked, onCommand, 274 | findNext, seekNext, 275 | }; 276 | 277 | }); })(this); 278 | -------------------------------------------------------------------------------- /background/tabs.js: -------------------------------------------------------------------------------- 1 | (function(global) { 'use strict'; define(async ({ // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 2 | 'node_modules/web-ext-utils/browser/': { Tabs, }, 3 | 'node_modules/web-ext-utils/utils/event': { setEvent, }, 4 | 'common/options': options, 5 | module, 6 | }) => { /* global setTimeout, */ 7 | let debug, debug2, debug3; options.debug.whenChange(([ value, ]) => { debug = value; debug2 = value >= 2; debug3 = value >= 3; }); 8 | 9 | /** 10 | * Synchronous cache for Tabs.get/.query() 11 | */ 12 | // browser.tabs properties 13 | const exports = module.exports = { 14 | update: Tabs.update, discard: Tabs.discard, query, 15 | // get/query falling back to native Tabs while the module is disabled 16 | getAsync() { return enabled ? tabs.get(...arguments) : Tabs.get(...arguments); }, 17 | queryAsync() { return enabled ? query(...arguments) : Tabs.query(...arguments); }, 18 | // get the active or previous active tab in a window 19 | active(windowId) { return tabs.get(active.get(windowId)) || null; }, 20 | previous(windowId) { return tabs.get(previous.get(windowId)) || null; }, 21 | // enables or disables the module, disabled by default 22 | setEnabled(bool) { bool ? enable() : disable(); }, 23 | // inherits Map methods (including synchronous get) 24 | __proto__: new Map, 25 | delete(id) { return tabs.delete(id); }, 26 | }; void find; 27 | const fireCreated = setEvent(exports, 'onCreated', { lazy: false, }); 28 | const fireUpdated = setEvent(exports, 'onUpdated', { lazy: false, }); 29 | const fireRemoved = setEvent(exports, 'onRemoved', { lazy: false, }); 30 | 31 | /// implementation 32 | 33 | 34 | // cache 35 | const tabs = Object.getPrototypeOf(exports); // new Map/**/; 36 | const active = new Map/**/, previous = new Map/**/; 37 | 38 | 39 | // initialize 40 | let enabled = false; const listeners = [ ]; 41 | function listen(event, listener) { listeners.push([ event, listener, ]); } 42 | function ensureEnabled() { if (!enabled) { throw new Error(`Tabs cache is not active`); } } 43 | async function enable() { 44 | if (enabled) { return; } enabled = true; 45 | listeners.forEach(([ event, listener, ]) => event.addListener(listener)); 46 | (await Tabs.query({ })).forEach(props => { 47 | // BUG[FF60]: FF *sometimes* reports never-loaded tabs as not discarded (this is supposed to be fixed, but it does still happen in FF60) 48 | // EDIT: does this only happen for pending pinned tabs with browser.sessionstore.restore_pinned_tabs_on_demand? 49 | if (!props.discarded && props.isArticle === undefined && props.status === 'complete') { 50 | debug && console.warn('[BUG] pending tab reported as non-discarded', props.id); 51 | props.discarded = true; 52 | } 53 | addTab(props); 54 | }); 55 | query({ active: true, }).forEach(setActive); 56 | } 57 | async function disable() { 58 | if (!enabled) { return; } enabled = false; 59 | listeners.forEach(([ event, listener, ]) => event.removeListener(listener)); 60 | tabs.clear(); 61 | } 62 | 63 | 64 | // add and basic update 65 | listen(Tabs.onCreated, function (props) { 66 | debug2 && console.log('onCreated', ...arguments); 67 | addOrUpdateTab(props.id, props, props); 68 | }); 69 | listen(Tabs.onUpdated, function (id, change, props) { 70 | debug2 && console.log('onUpdated', ...arguments); 71 | addOrUpdateTab(id, change, props); 72 | }); 73 | function addOrUpdateTab(id, change, props) { 74 | // BUG[FF60]: tabs sometimes get updated before they are created, so direct both events here 75 | // and decide based on the existence of the target tab what it actually is 76 | const tab = tabs.get(id); if (tab) { 77 | debug && change === props && console.warn('[BUG] receiving create update for existing tab', id); 78 | // if there was an early update, it's index may have been wrong, 79 | // so explicitly move the tab to fix the .index of shifted tabs 80 | if (change === props && tab.index !== change.index) { moveTab(tab.id, tab.index, change.index); } 81 | 82 | else if (tab.index !== props.index) { // TODO: the `.index` update logic doesn't work correctly 83 | debug && console.error(`unexpected .index change for tab ${id}: ${tab.index} ==> ${props.index}`); 84 | change.index = props.index; // temporary fix for that 85 | } 86 | 87 | updateTab(tab, change); // apply whatever else might have changed 88 | // TODO: which information would actually be correct, the early update or the late crate? 89 | } else { addTab(props); } 90 | } 91 | function updateTab(tab, change) { 92 | let changed = false; Object.entries(change).forEach(([ key, value, ]) => { 93 | if (!(key in tab) || tab[key] === value) { delete change[key]; } 94 | else { tab[key] = value; changed = true; } 95 | }); if (!changed) { return; } 96 | 97 | debug3 && console.log('fireUpdated', tab.id, change, clone(tab)); 98 | fireUpdated([ tab, change, ]); 99 | } 100 | function addTab({ id, discarded, active, hidden, status, windowId, index, pinned, highlighted, }) { 101 | const tab = { 102 | id: +id, discarded: discarded || false, active: active || false, hidden: hidden || false, 103 | status, windowId: +windowId, index: +index, pinned: pinned || false, highlighted: highlighted || false, __proto__: null, 104 | restoring: false, // custom 105 | }; tabs.set(id, tab); 106 | Object.defineProperty(tab, 'id', { configurable: false, writable: false, }); // `.id` must never change 107 | 108 | debug2 && console.log('fireCreated', id, clone(tab)); 109 | fireCreated([ tab, ]); 110 | 111 | query({ windowId, }).forEach(it => it.index >= index && it !== tab && updateTab(it, { index: it.index + 1, })); 112 | } 113 | 114 | 115 | // activate (focus) 116 | listen(Tabs.onActivated, function ({ tabId: id, windowId, }) { 117 | debug2 && console.log('onActivated', ...arguments); 118 | const tab = tabs.get(id), last = find({ windowId, active: true, }); 119 | 120 | // BUG[FF60]: If a not-restored tab it incorrectly not marked as discarded, onUpdated won't fire. 121 | // Normally, it fires before onActivated. 122 | tab.discarded && updateTab(tab, { discarded: false, }); 123 | 124 | setActive(tab); updateTab(tab, { active: true, }); last && updateTab(last, { active: false, }); 125 | }); 126 | function setActive(tab) { 127 | previous.set(tab.windowId, active.get(tab.windowId)); active.set(tab.windowId, tab.id); 128 | } 129 | 130 | 131 | // multiselection 132 | listen(Tabs.onHighlighted, function ({ tabIds: ids, windowId, }) { 133 | debug2 && console.log('onHighlighted', ...arguments); 134 | const newTabs = ids.map(id => tabs.get(id)), oldTabs = query({ windowId, highlighted: true, }); 135 | newTabs.forEach(tab => updateTab(tab, { highlighted: true, })); 136 | ids = new Set(ids); oldTabs.forEach(tab => !ids.has(tab.id) && updateTab(tab, { highlighted: false, })); 137 | }); 138 | 139 | 140 | // move within window 141 | listen(Tabs.onMoved, (id, { fromIndex, toIndex, }) => moveTab(id, fromIndex, toIndex)); 142 | function moveTab(id, from, to) { 143 | debug2 && console.log('moveTab', ...arguments); 144 | const tab = tabs.get(id); updateTab(tab, { index: to, }); 145 | const move = query({ windowId: tab.windowId, }); 146 | if (from < to) { 147 | move.forEach(tab => tab.index > from && tab.index < to && updateTab(tab, { index: tab.index - 1, })); 148 | } else { 149 | move.forEach(tab => tab.index < from && tab.index > to && updateTab(tab, { index: tab.index + 1, })); 150 | } 151 | } 152 | 153 | 154 | // moved to window (from other window) 155 | listen(Tabs.onAttached, function (id, { newWindowId, newPosition: newIndex, }) { 156 | debug2 && console.log('onAttached', ...arguments); 157 | const tab = tabs.get(id); const { windowId: oldWindowId, index: oldIndex, } = tab; 158 | query({ windowId: oldWindowId, }).forEach(tab => tab.index > oldIndex && updateTab(tab, { index: tab.index - 1, })); 159 | query({ windowId: newWindowId, }).forEach(tab => tab.index >= newIndex && updateTab(tab, { index: tab.index + 1, })); 160 | updateTab(tab, { windowId: newWindowId, index: newIndex, }); 161 | }); 162 | 163 | 164 | // closed 165 | listen(Tabs.onRemoved, function (id, { isWindowClosing, }) { setTimeout(() => { 166 | debug2 && console.log('onRemoved', ...arguments); 167 | const tab = tabs.get(id); tabs.delete(id); const { windowId, index, active, } = tab; 168 | 169 | debug2 && console.log('fireRemoved', id, clone(tab)); 170 | fireRemoved([ tab, { isWindowClosing, }, ]); 171 | 172 | !isWindowClosing && query({ windowId, }).forEach(tab => tab.index > index && updateTab(tab, { index: tab.index - 1, })); 173 | debug2 && !isWindowClosing && active && console.warn('removed active tab'); 174 | }); }); 175 | 176 | 177 | // get the first or all tabs that match the criteria 178 | function queryOrFind(one, query) { 179 | ensureEnabled(); 180 | const props = Object.entries(query).filter(_=>_[1] !== undefined); 181 | const res = [ ]; for (const { 1: tab, } of tabs) { 182 | if (props.every(({ 0: key, 1: value, }) => tab[key] === value)) { 183 | if (one) { return tab; } else { res.push(tab); } 184 | } 185 | } return one ? null : res; 186 | } 187 | function query(props) { return queryOrFind(false, props); } 188 | function find (props) { return queryOrFind(true, props); } 189 | 190 | function clone(arg) { 191 | return JSON.parse(JSON.stringify(arg)); 192 | } 193 | 194 | }); })(this); 195 | -------------------------------------------------------------------------------- /background/tst.js: -------------------------------------------------------------------------------- 1 | (function(global) { 'use strict'; define(async ({ // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 2 | 'node_modules/web-ext-utils/browser/': { manifest, Runtime, Menus, }, 3 | 'common/options': options, 4 | require, 5 | }) => { 6 | let debug; options.debug.whenChange(([ value, ]) => { debug = value >= 2; }); 7 | let onClicked, menus; require([ './', ], index => ({ onClicked, menus, } = index)); // cyclic 8 | 9 | const TST_ID = 'treestyletab@piro.sakura.ne.jp'; 10 | 11 | const unloadTreeMenu = { 12 | id: 'unloadTree', 13 | title: 'Unload Tree', 14 | contexts: [ 'tab', ], 15 | viewTypes: [ 'sidebar', ], 16 | }; 17 | 18 | const onError = console.error.bind(console, 'TST error'); 19 | 20 | async function register() { 21 | (await Runtime.sendMessage(TST_ID, { 22 | type: 'register-self', 23 | name: manifest.name, 24 | icons: manifest.icons, 25 | listeningTypes: [ ], 26 | style: options['intregrate.tst'].children.style.value, 27 | })); 28 | (await Promise.all(Object.values(menus).map(menu => Runtime.sendMessage(TST_ID, { 29 | type: 'fake-contextMenu-create', params: menu, 30 | })))); 31 | (await Runtime.sendMessage(TST_ID, { 32 | type: 'fake-contextMenu-create', params: unloadTreeMenu, 33 | })); 34 | } 35 | 36 | async function onMessageExternal(message, sender) { { 37 | debug && console.log('onMessageExternal', ...arguments); 38 | if (sender.id !== TST_ID) { return false; } 39 | } try { switch (message.type) { 40 | case 'ready': register().catch(onError); break; 41 | case 'fake-contextMenu-click': onClicked(message.info, message.tab); 42 | } } catch (error) { console.error('TST error', error); } { 43 | return true; // indicate to TST that the event was handled 44 | } } 45 | 46 | function updateMenu([ value, ], _, { name, }) { 47 | Runtime.sendMessage(TST_ID, { 48 | type: 'fake-contextMenu-update', params: [ name, { contexts: value.split(' '), }, ], 49 | }).catch(onError); 50 | } 51 | 52 | return { 53 | // the very first tst.enable() has to happen while TST is already running for the initial registration to work 54 | // also, this is somewhat racy: calling disable() and enable() before the other one was done can lead to unexpected states 55 | enable() { 56 | Runtime.onMessageExternal.addListener(onMessageExternal); 57 | options.menus.children.unloadOtherTabs.onChange.addListener(updateMenu); 58 | options.menus.children.unloadAllTabs.onChange.addListener(updateMenu); 59 | register().catch(() => null); // may very well not be ready yet 60 | Menus.create(unloadTreeMenu, () => Runtime.lastError && console.error('TST error (create native menu)', Runtime.lastError)); // (why doesn't this return a promise?!) 61 | }, 62 | disable() { 63 | Runtime.onMessageExternal.removeListener(onMessageExternal); 64 | options.menus.children.unloadOtherTabs.onChange.removeListener(updateMenu); 65 | options.menus.children.unloadAllTabs.onChange.removeListener(updateMenu); 66 | Runtime.sendMessage(TST_ID, { type: 'unregister-self', }).catch(onError); 67 | Menus.remove(unloadTreeMenu.id).catch(error => console.error('TST error (remove native menu)', error)); 68 | }, 69 | async getChildren(tabId) { 70 | const tree = (await Runtime.sendMessage(TST_ID, { type: 'get-tree', tab: tabId, })); 71 | const tabs = [ ]; (function flatten(tree) { 72 | tabs.push(tree); tree.children.forEach(flatten); 73 | })(tree); return tabs; 74 | }, 75 | }; 76 | 77 | }); })(this); 78 | -------------------------------------------------------------------------------- /build-config.js: -------------------------------------------------------------------------------- 1 | /*eslint strict: ['error', 'global'], no-implicit-globals: 'off'*/ 'use strict'; /* globals module, */ // license: MPL-2.0 2 | module.exports = function({ options, /*packageJson,*/ manifestJson, files, }) { 3 | 4 | manifestJson.applications.gecko.strict_min_version = '59.0'; 5 | 6 | manifestJson.permissions.push( 7 | 'menus', 8 | 'notifications', 9 | 'tabs', 10 | // 'webRequest', 'webRequestBlocking', 11 | ); 12 | 13 | !options.viewRoot && (options.viewRoot = options.chrome ? 'UnloadTabs.html' : 'UnloadTabs'); 14 | delete manifestJson.browser_action; 15 | delete manifestJson.background.persistent; 16 | 17 | manifestJson.commands = { 18 | unloadSelectedTab: { 19 | suggested_key: { }, 20 | description: 'Unload the current tab', 21 | }, unloadSelectedTab_1: { 22 | description: 'Unload the current tab (alternative)', 23 | }, 24 | prevLoadedTab: { 25 | suggested_key: { default: 'Alt+PageUp', }, 26 | description: 'Switch to the previous loaded Tab', 27 | }, prevLoadedTab_1: { 28 | description: 'Switch to the previous loaded Tab (alternative)', 29 | }, 30 | nextLoadedTab: { 31 | suggested_key: { default: 'Alt+PageDown', }, 32 | description: 'Switch to the next loaded Tab', 33 | }, nextLoadedTab_1: { 34 | description: 'Switch to the next loaded Tab (alternative)', 35 | }, 36 | }; 37 | 38 | files['.'].push( 39 | 'dim-unloaded-tabs.css', 40 | 'many.png', 41 | ); 42 | 43 | files.node_modules = [ 44 | 'pbq/require.js', 45 | 'web-ext-utils/browser/index.js', 46 | 'web-ext-utils/browser/storage.js', 47 | 'web-ext-utils/browser/version.js', 48 | 'web-ext-utils/loader/_background.html', 49 | 'web-ext-utils/loader/_background.js', 50 | 'web-ext-utils/loader/_view.html', 51 | 'web-ext-utils/loader/_view.js', 52 | 'web-ext-utils/loader/views.js', 53 | 'web-ext-utils/options/editor/about.css', 54 | 'web-ext-utils/options/editor/about.js', 55 | 'web-ext-utils/options/editor/index.css', 56 | 'web-ext-utils/options/editor/index.js', 57 | 'web-ext-utils/options/editor/inline.js', 58 | 'web-ext-utils/options/editor/inline.css', 59 | 'web-ext-utils/options/index.js', 60 | 'web-ext-utils/update/index.js', 61 | 'web-ext-utils/utils/icons/', 62 | 'web-ext-utils/utils/event.js', 63 | 'web-ext-utils/utils/files.js', 64 | 'web-ext-utils/utils/notify.js', 65 | 'web-ext-utils/utils/semver.js', 66 | ]; 67 | }; 68 | -------------------------------------------------------------------------------- /common/options.js: -------------------------------------------------------------------------------- 1 | (function(global) { 'use strict'; define(async ({ // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 2 | 'node_modules/web-ext-utils/browser/storage': { sync: storage, }, 3 | 'node_modules/web-ext-utils/browser/version': { version, }, 4 | 'node_modules/web-ext-utils/options/': Options, 5 | }) => { 6 | const isBeta = (/^\d+\.\d+.\d+(?!$)/).test((global.browser || global.chrome).runtime.getManifest().version); // version doesn't end after the 3rd number ==> bata channel 7 | 8 | const model = { 9 | onClose: { 10 | title: 'On Tab close', 11 | default: true, 12 | input: { type: 'boolean', suffix: `prevent Firefox from loading unloaded Tabs.`, }, 13 | children: { 14 | previous: { 15 | default: false, 16 | input: { type: 'boolean', prefix: `Instead select
`, suffix: `the previous focused Tab,`, }, 17 | }, 18 | direction: { 19 | default: +1, 20 | restrict: { type: 'number', match: (/^[-]?1$/), }, 21 | input: { type: 'menulist', options: [ 22 | { value: +1, label: `right`, }, 23 | { value: -1, label: `left`, }, 24 | ], prefix: `or the closest loaded Tab, prefering `, }, 25 | }, 26 | preemptive: { 27 | default: false, 28 | input: { type: 'menulist', options: [ 29 | { value: true, label: `always`, }, 30 | { value: false, label: `only if Firefox chooses a loaded Tab`, }, 31 | ], prefix: `Do that`, }, 32 | }, 33 | }, 34 | }, 35 | menus: { 36 | title: 'Menus', 37 | default: true, 38 | children: { 39 | unloadOtherTabs: { 40 | default: 'tab tools_menu', 41 | restrict: { type: 'string', }, 42 | input: { type: 'menulist', prefix: `Show Unload Other Tabs`, options: [ 43 | { value: 'tools_menu', label: `only in the Tools menu`, }, 44 | { value: 'tab tools_menu', label: `also in the Tab context menu`, }, 45 | ], }, 46 | }, 47 | unloadAllTabs: { 48 | default: 'tools_menu', 49 | restrict: { type: 'string', }, 50 | input: { type: 'menulist', prefix: `Show Unload in All Windows`, options: [ 51 | { value: 'tools_menu', label: `only in the Tools menu`, }, 52 | { value: 'tab tools_menu', label: `also in the Tab context menu`, }, 53 | ], }, 54 | }, 55 | }, 56 | }, 57 | commands: { 58 | title: 'Keyboards shortcuts', 59 | default: true, 60 | hidden: +(/\d+/).exec(version) < 60, 61 | children: { 62 | unloadSelectedTab: { 63 | description: `Unload the current Tab`, 64 | default: [ ], 65 | maxLength: 2, 66 | input: { type: 'command', default: 'Alt + W', }, 67 | }, 68 | prevLoadedTab: { 69 | description: `Switch to the previous loaded Tab`, 70 | default: 'Alt + PageUp', 71 | minLength: 1, maxLength: 2, 72 | input: { type: 'command', default: 'Alt + PageUp', }, 73 | }, 74 | nextLoadedTab: { 75 | description: `Switch to the next loaded Tab`, 76 | default: 'Alt + PageDown', 77 | minLength: 1, maxLength: 2, 78 | input: { type: 'command', default: 'Alt + PageDown', }, 79 | }, 80 | }, 81 | }, 82 | 'intregrate.tst': { 83 | title: 'Integrate with Tree Style Tabs', 84 | description: `Only effective if Tree Style Tabs is already installed and activated. 85 |
Please re-enable if the integration fails.`, 86 | default: false, 87 | input: { type: 'boolean', suffix: `Dim unloaded Tabs and add context menu options.`, }, 88 | children: { 89 | style: { hidden: true, default: `.tab.discarded { opacity: 0.6; }`, }, 90 | }, 91 | }, 92 | debug: { 93 | title: 'Debug log verbosity', 94 | expanded: false, 95 | default: +isBeta, 96 | // hidden: !isBeta, 97 | restrict: { type: 'number', from: 0, to: 3, match: { exp: /^\d$/, message: 'This value must be an integer', }, }, 98 | input: { type: 'number', suffix: `Set 0 to disable, 1 for some, 2 for a lot of diagnostic logging. 3 is just ridiculous.`, }, 99 | }, 100 | }; 101 | 102 | return (await new Options({ model, storage, prefix: 'options', })).children; 103 | 104 | }); })(this); 105 | -------------------------------------------------------------------------------- /dim-unloaded-tabs.css: -------------------------------------------------------------------------------- 1 | /** 2 | * @name Dim Unloaded Tabs 3 | * @author Niklas Gollenstede 4 | * @licence CC-BY-SA-4.0 or MIT or MPL 2.0 5 | * @description 6 | * This style dims not loaded tabs in Firefox. 7 | * It is complementary to the UnloadTabs WebExtension and can either: 8 | * * be manually added to the `chrome/user.chrome.css` file in Firefox's profile directory 9 | * * or installed with the reStyle Firefox extension (and NativeExt). 10 | * 11 | * If you have any issues with this style, please open a ticket at https://github.com/NiklasGollenstede/unload-tabs 12 | */ 13 | 14 | @-moz-document 15 | url(chrome://browser/content/browser.xhtml), 16 | url(chrome://browser/content/browser.xul) 17 | { 18 | tab[pending], #alltabs-popup menuitem[pending] 19 | { 20 | opacity: 0.6 !important; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NiklasGollenstede/unload-tabs/2a112ca381fcc769aa61ac77dae96937e8bf3616/icon.png -------------------------------------------------------------------------------- /images/get-ff-ext.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NiklasGollenstede/unload-tabs/2a112ca381fcc769aa61ac77dae96937e8bf3616/images/get-ff-ext.png -------------------------------------------------------------------------------- /images/tab-context-menu.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NiklasGollenstede/unload-tabs/2a112ca381fcc769aa61ac77dae96937e8bf3616/images/tab-context-menu.png -------------------------------------------------------------------------------- /many.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NiklasGollenstede/unload-tabs/2a112ca381fcc769aa61ac77dae96937e8bf3616/many.png -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "unload-tabs", 3 | "version": "0.8.6", 4 | "title": "UnloadTabs", 5 | "description": "Unload your tabs to free resources and prevents them from loading unintentionally", 6 | "author": "Niklas Gollenstede", 7 | "license": "MPL-2.0", 8 | "repository": { 9 | "type": "git", 10 | "url": "https://github.com/NiklasGollenstede/unload-tabs" 11 | }, 12 | "dependencies": { 13 | "multiport": "0.2.3", 14 | "pbq": "0.3.5", 15 | "web-ext-build": "0.0.10", 16 | "web-ext-utils": "0.1.8" 17 | }, 18 | "devDependencies": { 19 | "babel-eslint": "9.0.0", 20 | "eslint": "5.4.0", 21 | "eslintrc": "NiklasGollenstede/eslintrc#8e5b58d" 22 | }, 23 | "scripts": { 24 | "postinstall": "npm start", 25 | "start": "web-ext-build", 26 | "lint": "eslint --ignore-path .gitignore .", 27 | "sign": "web-ext-build {beta:$APPVEYOR_BUILD_NUMBER,sign:1}" 28 | }, 29 | "eslintConfig": { 30 | "extends": "./node_modules/eslintrc/web-ext.js", 31 | "rules": { "no-console": "off" } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /update/extension/installed.js: -------------------------------------------------------------------------------- 1 | (function(global) { 'use strict'; define(({ // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 2 | 'node_modules/web-ext-utils/loader/views': { openView, }, 3 | }) => { 4 | 5 | openView('installed'); 6 | 7 | }); })(this); 8 | -------------------------------------------------------------------------------- /views/installed.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Installed UnloadTabs 8 | 17 | 18 | 19 | 20 |

UnloadTabs WebExtension was installed

21 | 22 |

If you just installed UnloadTabs, you know why you see this and can simply close the tab.

23 |

UnloadTabs was disabled last November because Firefox Quantum (57+) disabled support for the kind of add-on it was, without adding the necessary functionality to re-write it. 24 | Since then, there have been some Firefox updates, which allow UnloadTabs with most of its previous features to be implemented again as a WebExtension.
25 | So I invested around 40 hours to re-write the entire extension from scratch. I believe that the result now works well enough to be of value to other users again, but there are still some things that don't work:

    26 |
  • Unloaded tabs can't be styled. There are no plans to ever allow this in Firefox again. See below for workarounds.
  • 27 |
  • Preventing tabs from loading doesn't always work completely. This is due to the way WebExtensions work. They report things (like the closing of a tab) asynchronously, that is, after it already happened. Sometimes (often randomly) that is too late. In these cases, UnloadTabs tries to revert the default actions, but that may not always work entirely:
  • 31 |
  • The shortcuts for circling had to be changed to Alt+PageUp/Down (because only one of Ctrl or Alt can be used and default shortcuts can't be overwritten). They can be changed from Firefox 60 onward.
  • 32 |
33 |

If you encounter any other problems, please report them as an issue or comment on an existing issue matching your problem. Please don't complain in a rating on the Add-ons download page. I can't respond to those.

34 | 35 | 36 |

Unloaded Tabs Style

37 | 38 |

WebExtensions can't change this part of the browsers appearance anymore. The only way to still do this is by changing (or adding) files in the profile directory on the computers disk, either manually or automatically with the reStyle extension.

39 | 40 |

reStyle

41 | 42 |

Install the reStyle extension and set it up to use NativeExt.
43 | Then use it to install e.g. the Dim Unloaded Tabs user style.

44 | 45 |

Manual

46 |

    47 |
  • visit about:config (in a new tab), accept
  • 48 |
  • search for and toggle toolkit.legacyUserProfileCustomizations.stylesheets to true
  • 49 |
  • visit about:support (in a new tab)
  • 50 |
  • click Open Folder next to Profile Folder in the first table
  • 51 |
  • create and/or open the chrome folder therein
  • 52 |
  • create and/or open the userChrome.css therein
  • 53 |
  • add/append the content of this style
  • 54 |
  • save and restart Firefox, e.g. via about:restartrequired
  • 55 |

56 | 57 | 58 | 59 | --------------------------------------------------------------------------------