Skip to content

Commit

Permalink
Implement startup session
Browse files Browse the repository at this point in the history
  • Loading branch information
sienori committed Jan 1, 2020
1 parent 746275c commit 5a1a604
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 28 deletions.
32 changes: 25 additions & 7 deletions src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@
"makeCopySessionLabel": {
"message": "Make a copy of the session"
},
"registerStartupLabel": {
"message": "Register for startup"
},
"removeStartupLabel": {
"message": "Remove from startup"
},
"errorLabel": {
"message": "Error"
},
Expand Down Expand Up @@ -230,9 +236,6 @@
"autoSaveLabel": {
"message": "Auto save"
},
"startUpLabel": {
"message": "Startup"
},
"styleLabel": {
"message": "Style"
},
Expand Down Expand Up @@ -263,11 +266,26 @@
"isRestoreWindowPositionCaptionLabel": {
"message": "Restore windows position and size when opening session."
},
"ifOpenLastSessionWhenStartUpLabel": {
"message": "Restore previous session at startup"
"startupLabel": {
"message": "Startup"
},
"startupBehaviorLabel": {
"message": "Behavior when starting the browser"
},
"openPreviousSessionLabel": {
"message": "Open previous session"
},
"openPreviousSessionCaptionLabel": {
"message": "Open the session that was closed the last time."
},
"openStartupSessionLabel": {
"message": "Open startup session"
},
"openStartupSessionCaptionLabel": {
"message": "Open the sessions registered for startup. You can register for startup from the menu in the session list."
},
"ifOpenLastSessionWhenStartUpCaptionLabel": {
"message": "When browser starts up, restore the session that was closed the last time."
"DoNothingLabel": {
"message": "Do nothing"
},
"ifAutoSaveLabel": {
"message": "Save the session regularly"
Expand Down
1 change: 0 additions & 1 deletion src/background/autoSave.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ export const autoSaveWhenExitBrowser = async () => {
};

export const openLastSession = async () => {
if (!getSettings("ifOpenLastSessionWhenStartUp")) return;
log.info(logDir, "openLastSession()");

const currentWindows = await browser.windows.getAll();
Expand Down
23 changes: 14 additions & 9 deletions src/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ import {
import getSessions from "./getSessions";
import { openSession } from "./open";
import { addTag, removeTag } from "./tag";
import { initSettings, handleSettingsChange } from "src/settings/settings";
import { initSettings, handleSettingsChange, getSettings } from "src/settings/settings";
import exportSessions from "./export";
import onInstalledListener, { isUpdated } from "./onInstalledListener";
import onInstalledListener from "./onInstalledListener";
import { onCommandListener } from "./keyboardShortcuts";
import { openStartupSessions } from "./startup";
import { updateLogLevel, overWriteLogLevel } from "../common/log";

const logDir = "background/background";
Expand Down Expand Up @@ -65,17 +66,19 @@ const init = async () => {
IsInit = true;
await updateOldSessions();

if (!isUpdated) {
autoSaveWhenExitBrowser().then(() => {
openLastSession();
});
}

setAutoSave();
backupSessions();
addListeners();
};
init();

const onStartupListener = async () => {
await init();
autoSaveWhenExitBrowser().then(() => {
const startupBehavior = getSettings("startupBehavior");
if (startupBehavior === "previousSession") openLastSession();
else if (startupBehavior === "startupSession") openStartupSessions();
});
};

const onMessageListener = async (request, sender, sendResponse) => {
log.info(logDir, "onMessageListener()", request);
Expand Down Expand Up @@ -123,6 +126,8 @@ const onMessageListener = async (request, sender, sendResponse) => {
}
};

browser.runtime.onStartup.addListener(onStartupListener);
browser.runtime.onInstalled.addListener(init);
browser.runtime.onInstalled.addListener(onInstalledListener);
browser.runtime.onMessage.addListener(onMessageListener);
browser.commands.onCommand.addListener(onCommandListener);
3 changes: 0 additions & 3 deletions src/background/onInstalledListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ const openOptionsPage = active => {
});
};

export let isUpdated = false;

export default async details => {
if (details.reason != "install" && details.reason != "update") return;
log.info(logDir, "onInstalledListener()", details);
isUpdated = true;
await initSettings();
initShortcuts();
const isShowOptionsPage = getSettings("isShowOptionsPageWhenUpdated");
Expand Down
15 changes: 15 additions & 0 deletions src/background/startup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import log from "loglevel";
import { openSession } from "./open.js";
import { getSessionsByTag } from "./tag.js";

const logDir = "background/startup";

export const openStartupSessions = async () => {
log.info(logDir, "openStartupSessions()");
const startupSessions = await getSessionsByTag("_startup");
if (startupSessions.length == 0) return;

for (const i in startupSessions) {
await openSession(startupSessions[i], i == 0 ? "openInCurrentWindow" : "openInNewWindow");
}
};
3 changes: 2 additions & 1 deletion src/background/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export async function addTag(id, tag) {
"_auto",
browser.i18n.getMessage("regularSaveSessionName"),
browser.i18n.getMessage("winCloseSessionName"),
browser.i18n.getMessage("browserExitSessionName")
browser.i18n.getMessage("browserExitSessionName"),
browser.i18n.getMessage("startupLabel")
];
const currentTags = session.tag;
if (!reservedTag.every(isNotEqual)) return;
Expand Down
2 changes: 2 additions & 0 deletions src/popup/actions/generateTagLabel.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export default tag => {
return browser.i18n.getMessage("winCloseSessionName");
case "browserExit":
return browser.i18n.getMessage("browserExitSessionName");
case "_startup":
return browser.i18n.getMessage("startupLabel");
default:
return tag;
}
Expand Down
3 changes: 2 additions & 1 deletion src/popup/components/OptionsArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { Component } from "react";
import browser from "webextension-polyfill";
import "../styles/OptionsArea.scss";
import SearchBar from "./SearchBar";
import generateTagLabel from "../actions/generateTagLabel";

const alphabeticallySort = (a, b) => {
if (a.name.toLowerCase() > b.name.toLowerCase()) return 1;
Expand Down Expand Up @@ -140,7 +141,7 @@ export default class OptionsArea extends Component {
)}
{tagsCount.tags.map((tag, index) => (
<option value={tag.name} key={index}>
{tag.name} [{tag.count}]
{generateTagLabel(tag.name)} [{tag.count}]
</option>
))}
</select>
Expand Down
15 changes: 15 additions & 0 deletions src/popup/components/SessionMenuItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import React from "react";
import browser from "webextension-polyfill";
import {
sendOpenMessage,
sendTagAddMessage,
sendTagRemoveMessage,
replaceCurrentSession,
addCurrentWindow,
makeCopySession,
Expand Down Expand Up @@ -30,13 +32,21 @@ export default props => {
const handleMakeCopySession = () => {
makeCopySession(props.session.id);
};
const handleRegisterStartup = () => {
sendTagAddMessage(props.session.id, "_startup");
};
const handleRemoveStartup = () => {
sendTagRemoveMessage(props.session.id, "_startup");
};
const handleExportSession = () => {
sendExportSessionMessage(props.session.id);
};
const handleClickSection = e => {
e.stopPropagation();
};

const isStartup = () => props.session.tag.includes("_startup");

return (
<ul>
<li className="section" onClick={handleClickSection}>
Expand Down Expand Up @@ -82,6 +92,11 @@ export default props => {
</button>
</li>
<hr />
<li>
<button onClick={isStartup() ? handleRemoveStartup : handleRegisterStartup}>
{browser.i18n.getMessage(isStartup() ? "removeStartupLabel" : "registerStartupLabel")}
</button>
</li>
<li>
<button onClick={handleExportSession}>
{browser.i18n.getMessage("exportButtonLabel")}
Expand Down
36 changes: 30 additions & 6 deletions src/settings/defaultSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,38 @@ export default [
]
},
{
category: "startUpLabel",
category: "startupLabel",
elements: [
{
id: "ifOpenLastSessionWhenStartUp",
title: "ifOpenLastSessionWhenStartUpLabel",
captions: ["ifOpenLastSessionWhenStartUpCaptionLabel"],
type: "checkbox",
default: false
id: "startupBehavior",
title: "startupBehaviorLabel",
captions: [],
type: "none",
default: "none",
new: true,
childElements: [
{
id: "startupBehavior",
title: "openPreviousSessionLabel",
captions: ["openPreviousSessionCaptionLabel"],
type: "radio",
value: "previousSession"
},
{
id: "startupBehavior",
title: "openStartupSessionLabel",
captions: ["openStartupSessionCaptionLabel"],
type: "radio",
value: "startupSession"
},
{
id: "startupBehavior",
title: "DoNothingLabel",
captions: [""],
type: "radio",
value: "none"
}
]
}
]
},
Expand Down

0 comments on commit 5a1a604

Please sign in to comment.