Skip to content

Commit

Permalink
Add option to save from tabstrip
Browse files Browse the repository at this point in the history
  • Loading branch information
gyng committed Feb 3, 2018
1 parent 762b0fc commit 5969a36
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2.5.0

* Save tabs from tabstrip, with options to save to right, and tabs opened from another tab. Firefox only. (#57)

# 2.4.1

* Fix Freedesktop shortcuts not using the page title for Name and Title (#54)
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "__MSG_extensionName__",
"description": "__MSG_extensionDescription__",
"version": "2.4.1",
"version": "2.5.0",
"default_locale": "en",

"applications": {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "save-in",
"version": "2.4.1",
"version": "2.5.0",
"license": "MIT",
"scripts": {
"build": "env -u WEB_EXT_API_KEY -u WEB_EXT_API_SECRET web-ext build --overwrite-dest -i test docs yarn.lock",
Expand Down
3 changes: 2 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ const DOWNLOAD_TYPES = {
LINK: "LINK",
SELECTION: "SELECTION",
PAGE: "PAGE",
CLICK: "CLICK"
CLICK: "CLICK",
TAB: "TAB"
};

const CONFLICT_ACTION = {
Expand Down
8 changes: 8 additions & 0 deletions src/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ const Download = {
);
}

if (
typeof state.needRouteMatch !== "undefined" &&
state.needRouteMatch &&
!routeMatches
) {
return;
}

const download = _state => {
const finalFullPath = Download.finalizeFullPath(_state);

Expand Down
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ window.init = () => {
media = options.selection ? media.concat(["selection"]) : media;
media = options.page ? media.concat(["page"]) : media;

Menus.addTabMenus();

// CHROME ONLY, FF does not support yet
// https://bugzilla.mozilla.org/show_bug.cgi?id=1320462
const setAccesskey = (str, key) => {
Expand Down Expand Up @@ -178,6 +180,10 @@ window.init = () => {
};

browser.contextMenus.onClicked.addListener(info => {
if (Object.values(Menus.IDS.TABSTRIP).includes(info.menuItemId)) {
return;
}

const matchSave = info.menuItemId.match(/save-in-(\d|_)+-(.*?)-(.*)/);

if (matchSave && matchSave.length === 4) {
Expand Down Expand Up @@ -284,7 +290,7 @@ browser.contextMenus.onClicked.addListener(info => {
info: opts
};

requestedDownloadFlag = true; // Notifications.
requestedDownloadFlag = 1; // Notifications.
Download.renameAndDownload(state);
}

Expand Down
128 changes: 127 additions & 1 deletion src/menu.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
const Menus = {
IDS: {
TABSTRIP: {
SELECTED_TAB: "save-in-_-_-SI-selected-tab",
TO_RIGHT: "save-in-_-_-SI-to-right",
TO_RIGHT_MATCH: "save-in-_-_-SI-to-right-match",
OPENED_FROM_TAB: "save-in-_-_-SI-opened-from-tab"
}
},

makeSeparator: (() => {
let separatorCounter = 0;

Expand All @@ -13,7 +22,124 @@ const Menus = {
};

return makeSeparatorInner;
})()
})(),

addTabMenus: () => {
if (!options.tabEnabled) {
return;
}

browser.contextMenus.create({
id: Menus.IDS.TABSTRIP.SELECTED_TAB,
title: "Save selected tab",
contexts: ["tab"]
});

browser.contextMenus.create({
id: Menus.IDS.TABSTRIP.OPENED_FROM_TAB,
title: "Save other tabs opened from this tab",
contexts: ["tab"]
});

browser.contextMenus.create({
id: Menus.IDS.TABSTRIP.TO_RIGHT,
title: "Save all tabs to right including this",
contexts: ["tab"]
});

browser.contextMenus.create({
id: Menus.IDS.TABSTRIP.TO_RIGHT_MATCH,
title: "Save all tabs to right including this (must match a route)",
contexts: ["tab"]
});

const ids = Object.values(Menus.IDS.TABSTRIP);

browser.contextMenus.onClicked.addListener((info, fromTab) => {
if (!ids.includes(info.menuItemId)) {
return;
}

let filter = () => false;
let query = {
pinned: false,
windowId: fromTab.windowId,
windowType: "normal"
};

switch (info.menuItemId) {
case Menus.IDS.TABSTRIP.SELECTED_TAB:
filter = t => t.id === fromTab.id;
break;
case Menus.IDS.TABSTRIP.TO_RIGHT:
case Menus.IDS.TABSTRIP.TO_RIGHT_MATCH:
filter = t => t.index >= fromTab.index;
break;
case Menus.IDS.TABSTRIP.OPENED_FROM_TAB:
filter = () => true;
query = Object.assign(query, { openerTabId: fromTab.id });
break;
default:
break;
}

browser.tabs
.query(query)
.then(tabs => tabs.filter(t => !t.url.match(/^(about|chrome):/)))
.then(tabs => tabs.filter(filter))
.then(tabs => {
tabs.forEach(t => {
requestedDownloadFlag += 1; // Notifications.

let url = t.url;
let suggestedFilename = null;

if (options.shortcutTab) {
url = Shortcut.makeShortcut(
options.shortcutType,
url,
t.title || t.url
);

suggestedFilename = Shortcut.suggestShortcutFilename(
options.shortcutType,
DOWNLOAD_TYPES.TAB,
info,
t.title,
options.truncateLength
);
}

const opts = {
currentTab: t, // Global
linkText: t.title,
now: new Date(),
pageUrl: t.url,
selectionText: info.selectionText,
sourceUrl: t.url,
url, // Changes based off context
suggestedFilename, // wip: rename
context: DOWNLOAD_TYPES.TAB,
menuIndex: null,
comment: null,
modifiers: info.modifiers,
legacyDownloadInfo: info // wip, remove
};

// keeps track of state of the final path
const state = {
path: new Path.Path("."),
scratch: {},
info: opts,
needRouteMatch:
info.menuItemId === Menus.IDS.TABSTRIP.TO_RIGHT_MATCH
};

Download.renameAndDownload(state);
});
});
});
}
};

// Export for testing
Expand Down
8 changes: 5 additions & 3 deletions src/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const ICON_URL = "icons/ic_archive_black_128px.png";
const ERROR_ICON_URL = "icons/ic_error_outline_red_96px.png";

const downloadsList = {}; // global
let requestedDownloadFlag = false;
let requestedDownloadFlag = 0;

const Notification = {
currentDownloadChangeListener: null,
Expand Down Expand Up @@ -54,9 +54,11 @@ const Notification = {
}

const onDownloadCreatedListener = item => {
if (requestedDownloadFlag) {
if (requestedDownloadFlag > 0) {
downloadsList[item.id] = item;
requestedDownloadFlag = false;
requestedDownloadFlag -= 1;
} else {
requestedDownloadFlag = 0;
}
};

Expand Down
4 changes: 3 additions & 1 deletion src/option.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ const OptionsManagement = {
{ name: "shortcutLink", type: T.BOOL, default: false },
{ name: "shortcutMedia", type: T.BOOL, default: false },
{ name: "shortcutPage", type: T.BOOL, default: false },
{ name: "shortcutTab", type: T.BOOL, default: false },
{
name: "shortcutType",
type: T.VALUE,
default: SHORTCUT_TYPES.HTML_REDIRECT
},
{ name: "truncateLength", type: T.VALUE, default: 240 },
{ name: "fetchViaContent", type: T.BOOL, default: false }
{ name: "fetchViaContent", type: T.BOOL, default: false },
{ name: "tabEnabled", type: T.BOOL, default: false }
],

getKeys: () =>
Expand Down
3 changes: 2 additions & 1 deletion src/options/clauselist.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ <h3>Matchers</h3>
<code>link</code>,
<code>selection</code>,
<code>page</code>,
<code>click</code>}</td>
<code>click</code>,
<code>tab</code> (Firefox)}</td>
</tr>
<tr>
<td>
Expand Down
12 changes: 12 additions & 0 deletions src/options/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ <h3>Behavior</h3>
<div class="caption caption-line">Does not save external resources</div>
</label>

<label>
<input type="checkbox" id="tabEnabled" class="chrome-disabled" /> Enable saving from tabstrip
<span class="badge">Firefox</span>
<span class="warning badge">Experimental</span>
<div class="caption caption-line">Saves a tab, tab group, or tabs to right</div>
</label>

<label>
<input type="checkbox" id="prompt" /> Open a save file dialog when saving
</label>
Expand Down Expand Up @@ -354,6 +361,11 @@ <h2 id="section-save-as-shortcuts">Save As Shortcuts</h2>
<input type="checkbox" id="shortcutPage" /> Save pages as shortcuts instead
</label>

<label>
<input type="checkbox" id="shortcutTab" class="chrome-disabled" /> Save tabs as shortcuts instead (from right-click tab)
<span class="badge">Firefox</span>
</label>

<hr>

<h2 id="section-save-as-shortcuts">Keyboard Shortcuts</h2>
Expand Down
6 changes: 2 additions & 4 deletions src/shortcut.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ const Shortcut = {
}
},

makeShortcut: (type, url) =>
Download.makeObjectUrl(
Shortcut.makeShortcutContent(type, url, currentTab.title)
),
makeShortcut: (type, url, title = currentTab && currentTab.title) =>
Download.makeObjectUrl(Shortcut.makeShortcutContent(type, url, title)),

suggestShortcutFilename: (
shortcutType,
Expand Down

0 comments on commit 5969a36

Please sign in to comment.