Skip to content

Commit

Permalink
update files
Browse files Browse the repository at this point in the history
  • Loading branch information
piyush1104 committed Mar 20, 2022
1 parent cfdd5db commit 05f465b
Show file tree
Hide file tree
Showing 13 changed files with 240 additions and 117 deletions.
5 changes: 5 additions & 0 deletions config/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


module.exports = {
backgroundFiles: ['background.js', 'service-worker.js', 'manifest.json']
}
9 changes: 7 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"jest-watch-typeahead": "0.4.2",
"lodash": "^4.17.19",
"mini-css-extract-plugin": "0.9.0",
"node-watch": "^0.7.3",
"optimize-css-assets-webpack-plugin": "5.0.3",
"pnp-webpack-plugin": "1.6.4",
"postcss-flexbugs-fixes": "4.1.0",
Expand Down Expand Up @@ -70,11 +71,12 @@
"scripts": {
"start": "node scripts/start.js",
"watch": "node scripts/watch.js",
"watch:background": "node scripts/watchBackground.js",
"watch:options": "node scripts/watchOptions.js",
"build:popup": "node scripts/build.js",
"build:options": "node scripts/buildOptions.js",
"build": "rm -rf build/ && npm run build:popup && npm run build:options",
"build:bg": "cp src/background/index.js build/background.js",
"build:background": "node scripts/buildBackground.js",
"build": "rm -rf build/ && npm run build:popup && npm run build:options && npm run build:background",
"copy": "node scripts/copy.js",
"test": "node scripts/test.js"
},
Expand Down
56 changes: 0 additions & 56 deletions public/background.js

This file was deleted.

49 changes: 49 additions & 0 deletions public/firefox-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"manifest_version": 2,
"name": "Window Saver",
"description": "Save Windows to visit them later.",
"version": "1.8",
"icons":{
"48": "icons/dark.png",
"96": "icons/dark.png"
},
"browser_action":{
"default_title": "Window Saver",
"default_popup": "index.html",
"default_icon": {
"16": "icons/dark.png",
"32": "icons/dark.png",
"64": "icons/dark.png",
"128": "icons/dark.png"
},
"theme_icons": [
{
"size": 16,
"light": "icons/light.png",
"dark": "icons/dark.png"
}, {
"size": 32,
"light": "icons/light.png",
"dark": "icons/dark.png"
}, {
"size": 64,
"light": "icons/light.png",
"dark": "icons/dark.png"
}, {
"size": 128,
"light": "icons/light.png",
"dark": "icons/dark.png"
}
]
},
"options_ui": {
"page": "options.html"
},
"permissions": [
"tabs",
"storage"
],
"background": {
"scripts": ["browser-polyfill.js","background.js"]
}
}
15 changes: 15 additions & 0 deletions scripts/buildBackground.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const fs = require('fs-extra')
const paths = require('../config/paths')
const utilConfig = require('../config/util')



function copyBackgroundScripts() {
console.log("<==== copying background scripts ====>")
utilConfig.backgroundFiles.forEach((filename) => {
console.log(" copying %s ...", filename)
fs.copyFile(paths.appSrc + '/background/' + filename, paths.appBuild + '/' + filename)
})
}

copyBackgroundScripts()
17 changes: 17 additions & 0 deletions scripts/watchBackground.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const fs = require('fs-extra')
const path = require('path')
const watch = require('node-watch');

const paths = require('../config/paths')
const utilConfig = require('../config/util')

watch(paths.appSrc + '/background/', function(evt, name) {
if(name) {
const basename = path.basename(name)
console.log("<==== changes registered in file %s ====>", basename)
if (utilConfig.backgroundFiles.includes(basename)) {
console.log(" updating build folder ... \n")
fs.copyFile(name, paths.appBuild + '/' + basename)
}
}
});
121 changes: 121 additions & 0 deletions src/background/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
browserMod.runtime.onInstalled.addListener(init)
browserMod.runtime.onMessage.addListener(handleMessage)

const defaultSettings = {
confirmSavePrivate: true,
confirmDelete: true,
confirmOverride: true,
confirmReplaceNonPrivate: false,
confirmReplacePrivate: false,
showHints: true,
updateStorage: true,
}

async function init() {
try {
console.log('init called')
// let response = await browserMod.storage.sync.get('settings')
// let syncSettings = response?.settings
// console.log(syncSettings)
// if (!syncSettings) {
// // if not sync, check the local and convert to sync
// response = await browserMod.storage.local.get('settings')
// let localSettings = response?.settings
// console.log(localSettings)
// if (localSettings) {
// response = await browserMod.storage.local.get('list')
// let localList = response?.list
// console.log(localList)
// response = await browserMod.storage.sync.set({
// list: localList,
// settings: localSettings,
// })
// console.log(response)
// syncSettings = localSettings
// }
// }

// if (syncSettings) {
// if (!(updateStorage in syncSettings) || syncSettings['updateStorage']) {
// await updateStorage()
// await browserMod.storage.sync.set({
// settings: { ...syncSettings, updateStorage: false },
// })
// }
// return
// }
// await browserMod.storage.sync.set({ settings: defaultSettings })

let response = await browserMod.storage.local.get('settings')
let localSettings = response?.settings

if (localSettings) {
if (!(updateStorage in localSettings) || localSettings['updateStorage']) {
await updateStorage()
await browserMod.storage.local.set({
settings: { ...localSettings, updateStorage: false },
})
}
return
}
await browserMod.storage.local.set({ settings: defaultSettings })
} catch (e) {
console.log(e)
}
}

async function updateStorage() {
const response = await browserMod.storage.local.get('list')
const list = response['list']
if (Array.isArray(list) && list.length) {
const newList = list.map(item => {
if (!item?.id) {
return { ...item, id: crypto.randomUUID() }
}
return item
})
await browserMod.storage.local.set({ list: newList })
}
}

async function handleMessage(request) {
if (request.task === 'open_separate' || request.task === 'open_same') {
let currentWindow = await browserMod.windows.getCurrent()
let tabList = request.data
let windowInfo = await browserMod.windows.create()
let errors = []
let length = tabList.length
for (var i = 0; i < length; i++) {
let tab = tabList[i]
try {
if (
typeof browser !== 'undefined' &&
Object.getPrototypeOf(browser) === Object.prototype
) {
await browserMod.tabs.create({
active: false,
discarded: true,
title: tab.title,
url: tab.url,
windowId: windowInfo.id,
})
} else {
await browserMod.tabs.create({
active: false,
url: tab.url,
windowId: windowInfo.id,
})
}
} catch (err) {
errors.push(tab)
}
}
let response = await browserMod.tabs.query({ windowId: windowInfo.id })
await browserMod.tabs.move(response[0]['id'], { index: -1 })
if (errors.length > 0) {
console.error('<==== Following tabs could not be opened ====>')
console.error(errors)
}
request.task === 'open_same' && browserMod.windows.remove(currentWindow.id)
}
}
45 changes: 0 additions & 45 deletions src/background/index.js

This file was deleted.

10 changes: 5 additions & 5 deletions public/manifest.json → src/background/manifest.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"manifest_version": 2,
"manifest_version": 3,
"name": "Window Saver",
"description": "Save Windows to visit them later.",
"version": "1.8",
"version": "1.9",
"icons":{
"48": "icons/dark.png",
"96": "icons/dark.png"
},
"browser_action":{
"action":{
"default_title": "Window Saver",
"default_popup": "index.html",
"default_icon": {
Expand Down Expand Up @@ -36,15 +36,15 @@
}
]
},
"options_page": "options.html",
"options_ui": {
"page": "options.html"
},
"permissions": [
"bookmarks",
"tabs",
"storage"
],
"background": {
"scripts": ["browser-polyfill.js","background.js"]
"service_worker": "service-worker.js"
}
}
5 changes: 5 additions & 0 deletions src/background/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
try {
importScripts('browser-polyfill.js', 'background.js');
} catch (e) {
console.log(e);
}
Loading

0 comments on commit 05f465b

Please sign in to comment.