2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-12-23 02:28:55 +00:00

Merge pull request #149 from garymoon/development

Make app data folder consistent per URL and allow clearing of app data from menu
This commit is contained in:
Jia Hao 2016-02-25 09:54:10 +08:00
commit 875149bcf7
3 changed files with 36 additions and 14 deletions

View File

@ -55,7 +55,7 @@ function createMainWindow(options, onAppQuit, setDockBadge) {
mainWindow.webContents.send('change-zoom', currentZoom);
};
createMenu(options.nativefierVersion, onAppQuit, mainWindow.webContents.goBack, mainWindow.webContents.goForward, onZoomIn, onZoomOut, mainWindow.webContents.getURL);
createMenu(options.nativefierVersion, onAppQuit, onZoomIn, onZoomOut, mainWindow, options);
initContextMenu(mainWindow);
if (options.userAgent) {

View File

@ -1,16 +1,15 @@
import {Menu, shell, clipboard} from 'electron';
import {Menu, shell, clipboard, dialog} from 'electron';
/**
*
* @param {string} nativefierVersion
* @param {function} onQuit should be from app.quit
* @param {function} onGoBack
* @param {electron} onGoForward
* @param {function} onZoomIn
* @param {function} onZoomOut
* @param {function} getUrl
* @param {{}}} mainWindow
* @param {{}}} options
*/
function createMenu(nativefierVersion, onQuit, onGoBack, onGoForward, onZoomIn, onZoomOut, getUrl) {
function createMenu(nativefierVersion, onQuit, onZoomIn, onZoomOut, mainWindow, options) {
if (Menu.getApplicationMenu()) {
return;
}
@ -46,7 +45,7 @@ function createMenu(nativefierVersion, onQuit, onGoBack, onGoForward, onZoomIn,
label: 'Copy Current URL',
accelerator: 'CmdOrCtrl+C',
click: () => {
const currentURL = getUrl();
const currentURL = mainWindow.webContents.getURL();
clipboard.writeText(currentURL);
}
},
@ -69,14 +68,14 @@ function createMenu(nativefierVersion, onQuit, onGoBack, onGoForward, onZoomIn,
label: 'Back',
accelerator: 'CmdOrCtrl+[',
click: () => {
onGoBack();
mainWindow.webContents.goBack();
}
},
{
label: 'Forward',
accelerator: 'CmdOrCtrl+]',
click: () => {
onGoForward();
mainWindow.webContents.goForward();
}
},
{
@ -129,6 +128,27 @@ function createMenu(nativefierVersion, onQuit, onGoBack, onGoForward, onZoomIn,
onZoomOut();
}
},
{
label: 'Clear App Data',
click: () => {
dialog.showMessageBox(mainWindow, {
type: 'warning',
buttons: ['Yes', 'Cancel'],
defaultId: 1,
title: 'Clear cache confirmation',
message: 'This will clear all data (cookies, local storage etc) from this app. Are you sure you wish to proceed?'
}, response => {
if (response === 0) {
mainWindow.webContents.session.clearStorageData({},
() => {
mainWindow.webContents.session.clearCache(() => {
mainWindow.loadURL(options.targetUrl);
});
});
}
});
}
},
{
label: 'Toggle Window Developer Tools',
accelerator: (() => {

View File

@ -24,15 +24,15 @@ function buildApp(src, dest, options, callback) {
}
fs.writeFileSync(path.join(dest, '/nativefier.json'), JSON.stringify(appArgs));
changeAppPackageJsonName(dest, appArgs.name);
changeAppPackageJsonName(dest, appArgs.name, appArgs.targetUrl);
callback();
});
}
function changeAppPackageJsonName(appPath, name) {
function changeAppPackageJsonName(appPath, name, url) {
const packageJsonPath = path.join(appPath, '/package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath));
packageJson.name = normalizeAppName(name);
packageJson.name = normalizeAppName(name, url);
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson));
}
@ -57,9 +57,11 @@ function selectAppArgs(options) {
};
}
function normalizeAppName(appName) {
function normalizeAppName(appName, url) {
// use a simple 3 byte random string to prevent collision
const postFixHash = crypto.randomBytes(3).toString('hex');
let hash = crypto.createHash('md5');
hash.update(url);
const postFixHash = hash.digest('hex').substring(0, 6);
const normalized = _.kebabCase(appName.toLowerCase());
return `${normalized}-nativefier-${postFixHash}`;
}