Skip to content

Commit

Permalink
feat: export selected bookmark or folder, fix #206
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkoperillo committed Nov 25, 2024
1 parent c5dcabb commit e07ae9e
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ <h4>
<ul class="dropdown-menu">
<li>
<a href="#" data-bind="click: defaultContextDialog">
<span data-bind="text: defaultCtx.name"></span>
<span data-bind="text: defaultCtxName"></span>
</a>
</li>
<li role="separator" class="divider"></li>
Expand Down
36 changes: 20 additions & 16 deletions src/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ requirejs(
function AppVm() {
const contexts = ko.observableArray()
const selectedCtx = new ContextVm()
let defaultCtx = new ContextVm()
const defaultCtxName = 'default';
let defaultCtxIdx = -1;

const bookmarkSelected = new BookmarkSelectedVm() // bookmark loaded
let tabCounter = 0
Expand Down Expand Up @@ -163,15 +164,15 @@ requirejs(
}

const defaultContextDialog = () => {
contextDialog(defaultCtx)
contextDialog(_getDefaultCtx())
}

const contextDialogByName = () => {
let ctxToLoad = contexts().find(
(ctx) => ctx.name() === request.context()
ctx => ctx.name() === request.context()
)
if (ctxToLoad === undefined) {
ctxToLoad = defaultCtx
ctxToLoad = _getDefaultCtx()
}
contextDialog(ctxToLoad)
}
Expand Down Expand Up @@ -533,9 +534,9 @@ requirejs(
// Note that elements order is important
const _mapContext = () =>
[
defaultCtx,
_getDefaultCtx(),
request.context() !== 'default' &&
contexts().find((ctx) => ctx.name() === request.context()),
contexts().find(ctx => ctx.name() === request.context()),
]
.filter((ctx) => !!ctx)
.map((ctx) => _extractCtxVars(ctx.variables()))
Expand Down Expand Up @@ -661,7 +662,7 @@ requirejs(
name: selectedCtx.name(),
variables: _extractModelFromVM(selectedCtx.variables()),
})
const contextToEditIdx = contexts().find(
const contextToEditIdx = contexts().findIndex(
(ctx) => ctx.name === selectedCtx.name()
)
if (contextToEditIdx > -1) {
Expand Down Expand Up @@ -690,25 +691,27 @@ requirejs(
// load contexts
const loadedCtxs = []
storage.loadContexts(
(ctx) => {
ctx => {
loadedCtxs.push(new ContextVm(ctx.name, ctx.variables))
},
() => {
const defaultCtxIdx = loadedCtxs.findIndex(
(ctx) => ctx.name() === 'default'
defaultCtxIdx = loadedCtxs.findIndex(
ctx => ctx.name() === 'default'
)
if (defaultCtxIdx < 0) {
// default context
defaultCtx = new ContextVm()
} else {
defaultCtx = loadedCtxs[defaultCtxIdx]
defaultCtxIdx = 0
contexts.push(new ContextVm())
}
loadedCtxs.forEach((ctx) => contexts.push(ctx))
loadedCtxs.forEach(ctx => contexts.push(ctx))
contexts.sort(sortCriteriaCtx)
}
)
}

const _getDefaultCtx = () => {
return defaultCtxIdx >= 0 ? contexts()[defaultCtxIdx] : new ContextVm()
}

const dismissConfirmDialog = () => {
showConfirmDialog(false)
}
Expand Down Expand Up @@ -884,7 +887,8 @@ requirejs(
return {
contexts,
selectedCtx,
defaultCtx,
defaultCtxName,
defaultCtxIdx,

bookmarkSelected,
tabCounter,
Expand Down
23 changes: 13 additions & 10 deletions src/js/app/bookmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ define(function () {

const addBookmarks = (folder, bookmarks = []) => {
const newFolder = Object.assign({}, folder)
newFolder.bookmarks = folder.bookmarks.concat(bookmarks)
newFolder.bookmarks = folder.bookmarks ? folder.bookmarks.concat(bookmarks) : [].concat(bookmarks)
return newFolder
}

Expand All @@ -73,13 +73,13 @@ define(function () {
b.id === id

const sortBookmarks = (folder, criteria) => {
const bookmarks = folder.bookmarks
const bookmarks = folder.bookmarks ? folder.bookmarks : []
bookmarks.sort(criteria)
return Object.assign({}, folder, { bookmarks })
}

const replaceBookmark = (folder, bookmark) => {
const bookmarks = folder.bookmarks.slice()
const bookmarks = folder.bookmarks ? folder.bookmarks.slice() : []
const indexToReplace = bookmarks.findIndex(bookmarkById(bookmark))
if (indexToReplace !== -1) {
bookmarks.splice(indexToReplace, 1, bookmark)
Expand All @@ -91,7 +91,7 @@ define(function () {
}

const removeBookmarks = (folder, bookmarksToRemove = []) => {
let bookmarks = folder.bookmarks.slice()
let bookmarks = folder.bookmarks ? folder.bookmarks.slice() : []

const bookmarksToRemoveIds = (
Array.isArray(bookmarksToRemove) ? bookmarksToRemove : [bookmarksToRemove]
Expand Down Expand Up @@ -187,22 +187,23 @@ define(function () {
}

const exportObj = (bookmarks = [], contexts = []) => {
const exportSelectedBookmark = bookmarks.length === 1 && !bookmarks[0].isFolder
let harExport = {}
harExport.log = {}
harExport.log.version = '1.1'
harExport.log.creator = {}
harExport.log.creator.name = 'Resting WebExtension'
harExport.log.creator.version = '1.0'
harExport.log.entries = _bookmarkToHar(bookmarks)
harExport.log.entries = _bookmarkToHar(bookmarks, exportSelectedBookmark)
harExport.log._contexts = _contextsToHar(contexts)
return harExport
}

const _contextsToHar = (contexts = []) => {
return contexts.map((c) => {
return contexts.map(ctx => {
let contextHarField = {}
contextHarField.name = c.name
contextHarField.variables = c.variables.map((v) => ({
contextHarField.name = ctx.name
contextHarField.variables = ctx.variables.map(v => ({
name: v.name,
value: v.value,
enabled: v.enabled,
Expand All @@ -211,15 +212,17 @@ define(function () {
})
}

const _bookmarkToHar = (sources = []) => {
const _bookmarkToHar = (sources = [], exportSelectedBookmark = false) => {
let exported = []
if (sources.length > 0) {
let bookmarkExport = {}
bookmarkExport._name = sources[0].name
bookmarkExport._isFolder = sources[0].isFolder
bookmarkExport._id = sources[0].id
bookmarkExport._created = sources[0].created
bookmarkExport._folder = sources[0].folder
if (!exportSelectedBookmark) {
bookmarkExport._folder = sources[0].folder
}
bookmarkExport.startedDateTime = '1970-01-01T00:00:00Z' // not supported
bookmarkExport.request = {
headersSize: -1,
Expand Down
89 changes: 59 additions & 30 deletions src/js/app/components/bookmarks/bookmarksVm.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ define([
const exportSrc = ko.observable('har')

// contextual menu
const showContextMenu = ko.observable(false)
const showBookmarkContextMenu = ko.observable(false)
const showFolderContextMenu = ko.observable(false)
const contextMenuPosX = ko.observable('0px')
const contextMenuPosY = ko.observable('0px')

Expand Down Expand Up @@ -181,7 +182,8 @@ define([
showBookmarkDeleteDialog(false)
showImportDialog(false)
showExportDialog(false)
showContextMenu(false)
showBookmarkContextMenu(false)
showFolderContextMenu(false)
}
}
const loadBookmarkObj = (bookmarkObj) => {
Expand All @@ -192,7 +194,11 @@ define([
const posX = event.clientY
const posY = event.clientX
bookmarkOfContextMenu = bookmark
showContextMenu(true)
if (bookmark.isFolder) {
showFolderContextMenu(true)
} else {
showBookmarkContextMenu(true)
}
contextMenuPosX(`${posX}px`)
contextMenuPosY(`${posY}px`)
}
Expand All @@ -218,6 +224,33 @@ define([
}
}

const exportSelectedBookmarks = () => {
const contextsModels = _extractContextFromVM(contexts())
let contextsToExport = []
if(bookmarkOfContextMenu.isFolder) {
if(bookmarkOfContextMenu.bookmarks) {
contextsToExport = [...new Set(bookmarkOfContextMenu.bookmarks.map(b => b.request.context))]
}
} else {
contextsToExport.push(bookmarkOfContextMenu.request.context)
}
const selectedContexts = contextsModels.filter(cm => contextsToExport.includes(cm.name))
const exportContent = JSON.stringify(
bookmarkProvider.exportObj([bookmarkOfContextMenu], selectedContexts)
)
const exportFile = new File([exportContent], 'export.resting.json', {
type: 'application/json',
})

const url = URL.createObjectURL(exportFile)

chrome.downloads.download({
filename: exportFile.name,
url: url,
saveAs: true,
})
}

const _handleImport = () => {
const f = document.getElementById('import-file').files
const fr = new FileReader()
Expand All @@ -240,7 +273,7 @@ define([
}
})

importedBookmarks.contexts.forEach((c) => _saveContext(c))
importedBookmarks.contexts.forEach(c => _saveContext(c))
}
if (f[0]) {
fr.readAsText(f[0])
Expand All @@ -250,16 +283,26 @@ define([

// FIXME: duplication of appVm function
const _saveContext = (context = {}) => {
storage.saveContext({ name: context.name, variables: context.variables })
const contextToEdit = contexts().find(
(ctx) => ctx.name() === context.name
const contextToEditIdx = contexts().findIndex(
ctx => ctx.name() === context.name
)
const contextVm = new ContextVm(context.name, context.variables)
if (contextToEdit) {
contexts.replace(contextToEdit, contextVm)
} else if (context.name != 'default') {
let contextToSave;
if (contextToEditIdx >= 0) {
// append variables to existent context
contextToSave = { name: context.name, variables: _extractItemFromVM(contexts()[contextToEditIdx].variables()).concat(context.variables) }
} else {
contextToSave = context
}
storage.saveContext(contextToSave)
const contextVm = new ContextVm(contextToSave.name, contextToSave.variables)
if (contextToEditIdx >= 0) {
contexts.replace(contexts()[contextToEditIdx], contextVm)
} else {
contexts.push(contextVm)
}
if (context.name === 'default') {
appVm.defaultCtxIdx = contextToEditIdx
}
}

const _handleExport = () => {
Expand Down Expand Up @@ -327,7 +370,8 @@ define([
$(() => {
// hide context menu on every click on page
$('.row').on('click', function () {
showContextMenu(false)
showBookmarkContextMenu(false)
showFolderContextMenu(false)
})
_loadBookmarksNewFormat()

Expand All @@ -340,23 +384,6 @@ define([
return h('bookmarks-menu')
},
})
/*new Vue({
el: '#v-bookmarks-buttons',
components: {
ImportButton, ExportButton, SortButton, AddFolderButton
},
render: function(h) {
return h(
'div',
{},
[
h('add-folder-button'),
h('import-button'),
h('export-button'),
h('sort-button')
])
}
})*/
})

/*
Expand Down Expand Up @@ -392,9 +419,11 @@ define([
expandFolder,
importBookmarks,
exportBookmarks,
exportSelectedBookmarks,
// context menu
contextMenu,
showContextMenu,
showBookmarkContextMenu,
showFolderContextMenu,
contextMenuPosX,
contextMenuPosY,
// context menu actions
Expand Down
Loading

0 comments on commit e07ae9e

Please sign in to comment.