Skip to content

Commit

Permalink
Merge pull request #1 from sjmyuan/remove-host-permission
Browse files Browse the repository at this point in the history
Remove host permission
  • Loading branch information
sjmyuan authored Feb 29, 2020
2 parents 0f44c8d + 0e517da commit e1f9864
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 37 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
18 changes: 5 additions & 13 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -13,20 +13,12 @@
},
"background": {
"scripts": [
"/js/background_script.bundle.js"
"js/background_script.bundle.js"
],
"persistent": false
},
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"all_frames": true,
"js": [
"/js/content_script.bundle.js"
]
}
"web_accessible_resources": [
"js/content_script.bundle.js"
],
"commands": {
"copy-as-markdown": {
Expand All @@ -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';"
}
29 changes: 19 additions & 10 deletions src/background_script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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(() => {
Expand All @@ -42,6 +48,9 @@ const initBackgroundScript = () => {
chrome.contextMenus.onClicked.addListener(onContextMenuClicked)

chrome.commands.onCommand.addListener(onCommandTriggered);

chrome.runtime.onMessage.addListener(onMessageReceived)

});
}

Expand Down
16 changes: 3 additions & 13 deletions src/content_script.ts
Original file line number Diff line number Diff line change
@@ -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})

0 comments on commit e1f9864

Please sign in to comment.