Skip to content

Commit

Permalink
Merge pull request #5 from satoryu/context-menu
Browse files Browse the repository at this point in the history
Adds a context menu for creating a link for the current tab.
  • Loading branch information
satoryu authored Nov 12, 2022
2 parents 0721182 + 8cfceb8 commit 398e50e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
19 changes: 19 additions & 0 deletions background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { writeTextToClipboard } from './src/clipboard.js'
import { createLinkForTab } from './src/link.js'

chrome.runtime.onInstalled.addListener(function () {
chrome.contextMenus.create({
id: 'copy-for-scrapbox',
title: 'Copy [URL PageTitle]'
})
})

chrome.contextMenus.onClicked.addListener(function (_, tab) {
const text = createLinkForTab(tab)

chrome.scripting.executeScript({
target: { tabId: tab.id },
func: writeTextToClipboard,
args: [text]
})
})
9 changes: 8 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@
"128": "images/icon128.png"
}
},
"background": {
"service_worker": "background.js",
"type": "module"
},
"icons": {
"16": "images/icon16.png",
"24": "images/icon24.png",
"32": "images/icon32.png",
"128": "images/icon128.png"
},
"permissions": ["tabs"],
"permissions": ["tabs", "contextMenus", "scripting"],
"host_permissions": [
"https://*/*"
],
"web_accessible_resources": [
{
"resources": ["/src/*.js"],
Expand Down
14 changes: 8 additions & 6 deletions src/link.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
function createLinksForTabs(tabs) {
return tabs.map(tab => {
let title = tab.title.replaceAll(/[\[\]]/g, '').replaceAll(/`(.*)`/g, '$1')
function createLinkForTab(tab) {
const title = tab.title.replaceAll(/[\[\]]/g, '').replaceAll(/`(.*)`/g, '$1')

return ` [${tab.url} ${title}]`
}

return ` [${tab.url} ${title}]`
}).join("\n")
function createLinksForTabs(tabs) {
return tabs.map(createLinkForTab).join("\n")
}

export { createLinksForTabs }
export { createLinksForTabs, createLinkForTab }

0 comments on commit 398e50e

Please sign in to comment.