From 2f282d3a4b077b423cfa023765714faa6c7bbc83 Mon Sep 17 00:00:00 2001 From: Nils Maier Date: Sat, 24 Aug 2019 07:29:39 +0200 Subject: [PATCH] Implement All Tabs actions Closes #3 --- _locales/en/messages.json | 12 +++- lib/background.ts | 122 +++++++++++++++++++++++++------------- windows/popup.html | 13 +++- 3 files changed, 104 insertions(+), 43 deletions(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 4c02620..fd7502e 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -183,6 +183,14 @@ "description": "Download (verb/action)", "message": "Download" }, + "dta-regular-all": { + "description": "", + "message": "DownThemAll! - All Tabs" + }, + "dta-turbo-all": { + "description": "", + "message": "OneClick! - All Tabs" + }, "dta.regular": { "description": "Regular dta action", "message": "DownThemAll!" @@ -204,8 +212,8 @@ "message": "Save Selection with DownThemAll!" }, "dta.turbo": { - "description": "Turbo dta action (please keep the case of dTa!)", - "message": "dTa! One Click" + "description": "", + "message": "OneClick!" }, "dta.turbo.image": { "description": "", diff --git a/lib/background.ts b/lib/background.ts index 1e4b0ec..f02df87 100644 --- a/lib/background.ts +++ b/lib/background.ts @@ -17,8 +17,11 @@ import { } from "./browser"; import { Bus } from "./bus"; + const menus = typeof (_menus) !== "undefined" && _menus || _cmenus; +const GATHER = "/bundles/content-gather.js"; + async function runContentJob(tab: any, file: string, msg: any) { try { @@ -49,6 +52,14 @@ async function runContentJob(tab: any, file: string, msg: any) { } } +type SelectionOptions = { + selectionOnly: boolean; + allTabs: boolean; + turbo: boolean; + tab: any; +}; + + class Handler { async processResults(turbo = false, results: any[]) { const links = this.makeUnique(results, "links"); @@ -65,6 +76,34 @@ class Handler { filter((i: any) => i); })); } + + async performSelection(options: SelectionOptions) { + try { + const selectedTabs = options.allTabs ? + await tabs.query({ + currentWindow: true, + discarded: false, + hidden: false}) as any[] : + [options.tab]; + + const textLinks = await Prefs.get("text-links", true); + const goptions = { + type: "DTA:gather", + selectionOnly: options.selectionOnly, + textLinks, + schemes: Array.from(ALLOWED_SCHEMES.values()), + transferable: TRANSFERABLE_PROPERTIES, + }; + + const results = await Promise.all(selectedTabs. + map((tab: any) => runContentJob(tab, GATHER, goptions))); + + await this.processResults(options.turbo, results.flat()); + } + catch (ex) { + console.error(ex.toString(), ex.stack, ex); + } + } } new class Action extends Handler { @@ -279,57 +318,58 @@ const menuHandler = new class Menus extends Handler { }, tab[0]); } - async onClickedDTARegularInternal( - selectionOnly: boolean, info: any, tab: any) { - try { - await this.processResults( - false, - await runContentJob( - tab, "/bundles/content-gather.js", { - type: "DTA:gather", - selectionOnly, - textLinks: await Prefs.get("text-links", true), - schemes: Array.from(ALLOWED_SCHEMES.values()), - transferable: TRANSFERABLE_PROPERTIES, - })); - } - catch (ex) { - console.error(ex); - } + async onClickedDTARegular(info: any, tab: any) { + return await this.performSelection({ + selectionOnly: false, + allTabs: false, + turbo: false, + tab, + }); } - async onClickedDTARegular(info: any, tab: any) { - return await this.onClickedDTARegularInternal(false, info, tab); + async onClickedDTARegularAll(info: any, tab: any) { + return await this.performSelection({ + selectionOnly: false, + allTabs: true, + turbo: false, + tab, + }); } async onClickedDTARegularSelection(info: any, tab: any) { - return await this.onClickedDTARegularInternal(true, info, tab); - } - - async onClickedDTATurboInternal(selectionOnly: boolean, info: any, tab: any) { - try { - await this.processResults( - true, - await runContentJob( - tab, "/bundles/content-gather.js", { - type: "DTA:gather", - selectionOnly, - textLinks: await Prefs.get("text-links", true), - schemes: Array.from(ALLOWED_SCHEMES.values()), - transferable: TRANSFERABLE_PROPERTIES, - })); - } - catch (ex) { - console.error(ex); - } + return await this.performSelection({ + selectionOnly: true, + allTabs: false, + turbo: false, + tab, + }); } async onClickedDTATurbo(info: any, tab: any) { - return await this.onClickedDTATurboInternal(false, info, tab); + return await this.performSelection({ + selectionOnly: false, + allTabs: false, + turbo: true, + tab, + }); + } + + async onClickedDTATurboAll(info: any, tab: any) { + return await this.performSelection({ + selectionOnly: false, + allTabs: true, + turbo: true, + tab, + }); } async onClickedDTATurboSelection(info: any, tab: any) { - return await this.onClickedDTATurboInternal(false, info, tab); + return await this.performSelection({ + selectionOnly: true, + allTabs: false, + turbo: true, + tab, + }); } async onClickedDTARegularLink(info: any, tab: any) { @@ -366,7 +406,9 @@ const menuHandler = new class Menus extends Handler { }(); Bus.on("do-regular", () => menuHandler.enumulate("DTARegular")); +Bus.on("do-regular-all", () => menuHandler.enumulate("DTARegularAll")); Bus.on("do-turbo", () => menuHandler.enumulate("DTATurbo")); +Bus.on("do-turbo-all", () => menuHandler.enumulate("DTATurboAll")); Bus.on("do-single", () => API.singleRegular(null)); Bus.on("open-manager", () => openManager(true)); Bus.on("open-prefs", () => openPrefs()); diff --git a/windows/popup.html b/windows/popup.html index 9fa69c2..e756de3 100644 --- a/windows/popup.html +++ b/windows/popup.html @@ -17,7 +17,18 @@
  • - OneClick! + +
  • +
  • +
    +
  • +
  • + + +
  • +
  • + +