Implement All Tabs actions

Closes #3
This commit is contained in:
Nils Maier 2019-08-24 07:29:39 +02:00
parent a9f83071dc
commit 2f282d3a4b
3 changed files with 104 additions and 43 deletions

View File

@ -183,6 +183,14 @@
"description": "Download (verb/action)", "description": "Download (verb/action)",
"message": "Download" "message": "Download"
}, },
"dta-regular-all": {
"description": "",
"message": "DownThemAll! - All Tabs"
},
"dta-turbo-all": {
"description": "",
"message": "OneClick! - All Tabs"
},
"dta.regular": { "dta.regular": {
"description": "Regular dta action", "description": "Regular dta action",
"message": "DownThemAll!" "message": "DownThemAll!"
@ -204,8 +212,8 @@
"message": "Save Selection with DownThemAll!" "message": "Save Selection with DownThemAll!"
}, },
"dta.turbo": { "dta.turbo": {
"description": "Turbo dta action (please keep the case of dTa!)", "description": "",
"message": "dTa! One Click" "message": "OneClick!"
}, },
"dta.turbo.image": { "dta.turbo.image": {
"description": "", "description": "",

View File

@ -17,8 +17,11 @@ import {
} from "./browser"; } from "./browser";
import { Bus } from "./bus"; import { Bus } from "./bus";
const menus = typeof (_menus) !== "undefined" && _menus || _cmenus; const menus = typeof (_menus) !== "undefined" && _menus || _cmenus;
const GATHER = "/bundles/content-gather.js";
async function runContentJob(tab: any, file: string, msg: any) { async function runContentJob(tab: any, file: string, msg: any) {
try { 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 { class Handler {
async processResults(turbo = false, results: any[]) { async processResults(turbo = false, results: any[]) {
const links = this.makeUnique(results, "links"); const links = this.makeUnique(results, "links");
@ -65,6 +76,34 @@ class Handler {
filter((i: any) => i); 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 { new class Action extends Handler {
@ -279,57 +318,58 @@ const menuHandler = new class Menus extends Handler {
}, tab[0]); }, tab[0]);
} }
async onClickedDTARegularInternal( async onClickedDTARegular(info: any, tab: any) {
selectionOnly: boolean, info: any, tab: any) { return await this.performSelection({
try { selectionOnly: false,
await this.processResults( allTabs: false,
false, turbo: false,
await runContentJob( tab,
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) { async onClickedDTARegularAll(info: any, tab: any) {
return await this.onClickedDTARegularInternal(false, info, tab); return await this.performSelection({
selectionOnly: false,
allTabs: true,
turbo: false,
tab,
});
} }
async onClickedDTARegularSelection(info: any, tab: any) { async onClickedDTARegularSelection(info: any, tab: any) {
return await this.onClickedDTARegularInternal(true, info, tab); return await this.performSelection({
} selectionOnly: true,
allTabs: false,
async onClickedDTATurboInternal(selectionOnly: boolean, info: any, tab: any) { turbo: false,
try { tab,
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);
}
} }
async onClickedDTATurbo(info: any, tab: any) { 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) { 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) { 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", () => menuHandler.enumulate("DTARegular"));
Bus.on("do-regular-all", () => menuHandler.enumulate("DTARegularAll"));
Bus.on("do-turbo", () => menuHandler.enumulate("DTATurbo")); Bus.on("do-turbo", () => menuHandler.enumulate("DTATurbo"));
Bus.on("do-turbo-all", () => menuHandler.enumulate("DTATurboAll"));
Bus.on("do-single", () => API.singleRegular(null)); Bus.on("do-single", () => API.singleRegular(null));
Bus.on("open-manager", () => openManager(true)); Bus.on("open-manager", () => openManager(true));
Bus.on("open-prefs", () => openPrefs()); Bus.on("open-prefs", () => openPrefs());

View File

@ -17,7 +17,18 @@
</li> </li>
<li id="turbo" data-action="do-turbo"> <li id="turbo" data-action="do-turbo">
<img srcset="/style/button-turbo.png, /style/button-turbo@2x.png 2x"> <img srcset="/style/button-turbo.png, /style/button-turbo@2x.png 2x">
<span data-i18n="dta.turbo">OneClick!</span> <span data-i18n="dta.turbo"></span>
</li>
<li class="sep">
<hr>
</li>
<li id="regular-all" data-action="do-regular-all">
<img srcset="/style/button-regular.png, /style/button-regular@2x.png 2x">
<span data-i18n="dta-regular-all"></span>
</li>
<li id="turbo" data-action="do-turbo">
<img srcset="/style/button-turbo.png, /style/button-turbo@2x.png 2x">
<span data-i18n="dta-turbo-all"></span>
</li> </li>
<li class="sep"> <li class="sep">
<hr> <hr>