Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import webExtension from 'webextension-polyfill'
// workaround as following functions cannot pass redux-saga
// and throw error: `TypeError: Function.prototype.toString requires that 'this' be a Function`
const wrap = , U>(fn: (...args: T) => U) => {
return (...args: T): U => fn(...args)
}
export const clearStorage = wrap(webExtension.storage.sync.clear)
export const createBookmark = wrap(webExtension.bookmarks.create)
export const createTab = wrap(webExtension.tabs.create)
export const createWindow = wrap(webExtension.windows.create)
export const executeScript = wrap(webExtension.tabs.executeScript)
export const getBookmarkChildNodes = wrap(webExtension.bookmarks.getChildren)
export const getBookmarkNodes = wrap(webExtension.bookmarks.get)
export const getI18n = wrap(webExtension.i18n.getMessage)
export const getLocalStorage = wrap(webExtension.storage.local.get)
export const getSyncStorage = wrap(webExtension.storage.sync.get)
export const moveBookmark = wrap(webExtension.bookmarks.move)
export const queryTabs = wrap(webExtension.tabs.query)
export const removeBookmark = wrap(webExtension.bookmarks.remove)
export const removeBookmarkTree = wrap(webExtension.bookmarks.removeTree)
export const searchBookmarkNodes = wrap(webExtension.bookmarks.search)
export const setLocalStorage = wrap(webExtension.storage.local.set)
export const setSyncStorage = wrap(webExtension.storage.sync.set)
export const updateBookmark = wrap(webExtension.bookmarks.update)
import webExtension from 'webextension-polyfill'
// workaround as following functions cannot pass redux-saga
// and throw error: `TypeError: Function.prototype.toString requires that 'this' be a Function`
const wrap = , U>(fn: (...args: T) => U) => {
return (...args: T): U => fn(...args)
}
export const clearStorage = wrap(webExtension.storage.sync.clear)
export const createBookmark = wrap(webExtension.bookmarks.create)
export const createTab = wrap(webExtension.tabs.create)
export const createWindow = wrap(webExtension.windows.create)
export const executeScript = wrap(webExtension.tabs.executeScript)
export const getBookmarkChildNodes = wrap(webExtension.bookmarks.getChildren)
export const getBookmarkNodes = wrap(webExtension.bookmarks.get)
export const getI18n = wrap(webExtension.i18n.getMessage)
export const getLocalStorage = wrap(webExtension.storage.local.get)
export const getSyncStorage = wrap(webExtension.storage.sync.get)
export const moveBookmark = wrap(webExtension.bookmarks.move)
export const queryTabs = wrap(webExtension.tabs.query)
export const removeBookmark = wrap(webExtension.bookmarks.remove)
export const removeBookmarkTree = wrap(webExtension.bookmarks.removeTree)
export const searchBookmarkNodes = wrap(webExtension.bookmarks.search)
export const setLocalStorage = wrap(webExtension.storage.local.set)
export const setSyncStorage = wrap(webExtension.storage.sync.set)
export const updateBookmark = wrap(webExtension.bookmarks.update)
export const updateTab = wrap(webExtension.tabs.update)
return acc
}, [])
// Sort and concatenate all lists into single list
const sortedChildrenInfo: Object[] = R.compose(R.flatten, R.map(R.map(sortByTitle)))(
classifiedItemsList
)
// Moving bookmarks to sorted index
for (const [index: number, itemInfo: Object] of sortedChildrenInfo.entries()) {
const oldItemInfo = await getBookmark(itemInfo.id)
const oldIndex: number = oldItemInfo.index
if (oldIndex !== index) {
await webExtension.bookmarks.move(itemInfo.id, {
// if new index is after old index, need to add 1,
// because index means the position in current array,
// which also count the current position
index: index + (index > oldIndex ? 1 : 0)
})
}
}
}
async function toggleBookmark() {
const allTabs = await browser.tabs.query({'active': true, currentWindow: true})
const activeTab = allTabs[0]
const matchedBookmarks = await browser.bookmarks.search({url: activeTab.url})
if (matchedBookmarks.length === 0) {
await browser.bookmarks.create({title: activeTab.title, url: activeTab.url})
}
else {
// apparently, you can bookmark the same page in a different tab.
for (const bookmark of matchedBookmarks) {
await browser.bookmarks.remove(bookmark.id)
}
}
window.close()
}
export const removeBookmark = async (target: Object): Promise => {
if (isFolder(target)) {
await webExtension.bookmarks.removeTree(target.id)
} else {
await webExtension.bookmarks.remove(target.id)
}
}
React.useEffect(() => {
const refreshTrees = debounce(refreshBookmarkTrees, 100)
webExtension.bookmarks.onChanged.addListener(refreshTrees)
webExtension.bookmarks.onCreated.addListener(refreshTrees)
webExtension.bookmarks.onMoved.addListener(refreshTrees)
webExtension.bookmarks.onRemoved.addListener(refreshTrees)
return () => {
webExtension.bookmarks.onChanged.removeListener(refreshTrees)
webExtension.bookmarks.onCreated.removeListener(refreshTrees)
webExtension.bookmarks.onMoved.removeListener(refreshTrees)
webExtension.bookmarks.onRemoved.removeListener(refreshTrees)
}
}, [refreshBookmarkTrees])
}
export const searchBookmarkNodes = (...args: Array) => webExtension.bookmarks.search(...args)
export const setStorage = (...args: Array) => webExtension.storage.sync.set(...args)
export const getBookmark = async (id: string): Promise => {
const [itemInfo] = await webExtension.bookmarks.get(id)
return itemInfo
}
React.useEffect(() => {
const refreshTrees = debounce(refreshBookmarkTrees, 100)
webExtension.bookmarks.onChanged.addListener(refreshTrees)
webExtension.bookmarks.onCreated.addListener(refreshTrees)
webExtension.bookmarks.onMoved.addListener(refreshTrees)
webExtension.bookmarks.onRemoved.addListener(refreshTrees)
return () => {
webExtension.bookmarks.onChanged.removeListener(refreshTrees)
webExtension.bookmarks.onCreated.removeListener(refreshTrees)
webExtension.bookmarks.onMoved.removeListener(refreshTrees)
webExtension.bookmarks.onRemoved.removeListener(refreshTrees)
}
}, [refreshBookmarkTrees])
}
handleConfirm = async (evt) => {
evt.persist()
evt.preventDefault()
const {closeEditor, editorTarget, isCreatingNewFolder} = this.props
const {title, url} = this.state
if (isCreatingNewFolder) {
await webExtension.bookmarks.create({
parentId: editorTarget.parentId,
title: title.trim(),
index: editorTarget.index + 1
})
} else {
await webExtension.bookmarks.update(editorTarget.id, {
title,
url: url.trim()
})
}
closeEditor()
}