diff --git a/package.json b/package.json index 42b8b75..6a1a483 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "copy-as-markdown", - "version": "0.0.1", + "version": "0.0.2", "description": "A chrome extension to copy HTML as Markdown", "main": "index.js", "scripts": { diff --git a/public/manifest.json b/public/manifest.json index 07ce506..c9c5bb9 100644 --- a/public/manifest.json +++ b/public/manifest.json @@ -1,5 +1,5 @@ { - "version": "0.0.1", + "version": "0.0.2", "name": "Copy as Markdown", "manifest_version": 2, "description": "Copy the selected HTML as Markdown by right click or Ctrl+Shift+Y", @@ -13,20 +13,12 @@ }, "background": { "scripts": [ - "/js/background_script.bundle.js" + "js/background_script.bundle.js" ], "persistent": false }, - "content_scripts": [ - { - "matches": [ - "" - ], - "all_frames": true, - "js": [ - "/js/content_script.bundle.js" - ] - } + "web_accessible_resources": [ + "js/content_script.bundle.js" ], "commands": { "copy-as-markdown": { @@ -41,5 +33,5 @@ "contextMenus", "activeTab" ], - "content_security_policy": "default-src 'self'; script-src 'self'; connect-src 'self'; style-src * 'unsafe-inline'; img-src 'self' data:;" + "content_security_policy": "default-src 'self'; script-src 'self'; connect-src 'self'; style-src 'self'; img-src 'self';" } diff --git a/src/background_script.ts b/src/background_script.ts index 116fc9d..9e29c3e 100644 --- a/src/background_script.ts +++ b/src/background_script.ts @@ -3,16 +3,7 @@ import {turndownServie, copyToClipboard} from './types' const sendMessageToTab = () => { chrome.tabs.query({active: true}, (tabs) => { if (tabs[0]) { - chrome.tabs.sendMessage(tabs[0].id as number, {'copy-as-markdown': true}, (response: {selection?: string}) => { - if (response.selection) { - const markdown = turndownServie.turndown(response.selection) - copyToClipboard(markdown) - console.log('Copied') - console.log(markdown) - } else { - console.log('There is no selection.') - } - }) + chrome.tabs.executeScript(tabs[0].id as number, {file: 'js/content_script.bundle.js'}) } else { console.log('No active tab') @@ -31,6 +22,21 @@ const onCommandTriggered = (command: string) => { } } +const onMessageReceived = (message: {selection?: string}, + sender: chrome.runtime.MessageSender, + sendResponse: (response?: any) => void) => { + if (message && message.selection) { + const markdown = turndownServie.turndown(message.selection) + copyToClipboard(markdown) + console.log('Copied') + console.log(markdown) + sendResponse(true) + } else { + console.log('There is no selection.') + sendResponse(false) + } +} + const initBackgroundScript = () => { console.log('background running'); chrome.runtime.onInstalled.addListener(() => { @@ -42,6 +48,9 @@ const initBackgroundScript = () => { chrome.contextMenus.onClicked.addListener(onContextMenuClicked) chrome.commands.onCommand.addListener(onCommandTriggered); + + chrome.runtime.onMessage.addListener(onMessageReceived) + }); } diff --git a/src/content_script.ts b/src/content_script.ts index bec92a3..05a8b2e 100644 --- a/src/content_script.ts +++ b/src/content_script.ts @@ -1,15 +1,5 @@ -import {getHtmlSelection, turndownServie, copyToClipboard} from './types' +import {getHtmlSelection} from './types' -const onExtensionMessage = (request: any, sender: chrome.runtime.MessageSender, sendResponse: (response: any) => void) => { - console.log('receive message:') - console.log(request) - if (request['copy-as-markdown'] != undefined) { - sendResponse({selection: getHtmlSelection()}); - } -} -const initContentScript: () => void = async () => { - console.log('loading script') - chrome.runtime.onMessage.addListener(onExtensionMessage); -} +const selectedHTML: string | undefined = getHtmlSelection(); -initContentScript(); +chrome.runtime.sendMessage({selection: selectedHTML})