Skip to content

Commit

Permalink
change menu title dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
nettee committed Oct 11, 2019
1 parent 5125430 commit 863cc5f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
38 changes: 24 additions & 14 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,26 @@ function notifyCopied(content) {
});
}

chrome.contextMenus.create({
var menuItems = {};

menuItems.root = chrome.contextMenus.create({
title: 'Copy it as markdown',
id: 'root',
contexts: ['all'],
});

chrome.contextMenus.create({
menuItems.selectedTextToMarkdown = chrome.contextMenus.create({
parentId: 'root',
title: 'Selected text',
type: 'normal',
contexts: ['selection'],
onclick: function (info, tab) {
// Inject the content script into the current page
chrome.tabs.executeScript(null, {file: 'get-selected-html.js'});

// Perform the callback when a message is received from the content script
chrome.runtime.onMessage.addListener(function (message) {
const html = message.selectedHtml;
console.log('html: ', html);
const markdown = htmlToMarkdown(html);
console.log('markdown: ', markdown);
copyToClipboard(markdown);
notifyCopied(markdown);
});
}
});

chrome.contextMenus.create({
menuItems.pageLinkTitle = chrome.contextMenus.create({
parentId: 'root',
title: 'Current page: [title](url)',
type: 'normal',
Expand Down Expand Up @@ -87,7 +79,7 @@ chrome.contextMenus.create({
// }
// });

chrome.contextMenus.create({
menuItems.pageLinkSelectedText = chrome.contextMenus.create({
parentId: 'root',
title: 'Current page: [selected text](url)',
type: 'normal',
Expand All @@ -100,3 +92,21 @@ chrome.contextMenus.create({
notifyCopied(markdown);
}
});

chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
if (message.request === 'selectedHtml') {
const html = message.selectedHtml;
console.log('html: ', html);
const markdown = htmlToMarkdown(html);
console.log('markdown: ', markdown);
copyToClipboard(markdown);
notifyCopied(markdown);

} else if (message.request === 'selectedText') {
const text = message.selectedText;
console.log('text: ', text);
chrome.contextMenus.update(menuItems.pageLinkSelectedText, {
title: `Current page: [${text}](url)`,
});
}
});
8 changes: 8 additions & 0 deletions content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
document.addEventListener('selectionchange', function () {
let selection = document.getSelection();
let selectionText = selection.toString().trim();
chrome.runtime.sendMessage({
request: 'selectedText',
selectedText: selectionText,
});
});
7 changes: 4 additions & 3 deletions get-selected-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ if (range) {
}

chrome.runtime.sendMessage({
'title': document.title,
'url': window.location.href,
'selectedHtml': selectedHtml,
request: 'selectedHtml',
title: document.title,
url: window.location.href,
selectedHtml: selectedHtml,
});
6 changes: 6 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
"icons": {
"128": "img/icon100.png"
},
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["content.js"]
}
],
"background": {
"scripts": ["background.js", "showdown.min.js"],
"persistent": true
Expand Down

0 comments on commit 863cc5f

Please sign in to comment.