Skip to content

Commit

Permalink
New feature: copy link as markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
nettee committed Oct 11, 2019
1 parent 221fd0a commit 9120854
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
55 changes: 30 additions & 25 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function notifyCopied(content) {
type: 'basic',
title: 'Content copied to clipboard',
message: content,
iconUrl: 'img/icon128.png',
iconUrl: 'img/icon100.png',
});
}

Expand All @@ -43,6 +43,8 @@ function compress(s, len) {
var pageTitle = 'title';
var pageUrl = 'url';
var pageSelectionText = 'selected text';
var linkText = 'text';
var linkUrl = 'url';

var menuItems = {};

Expand All @@ -63,41 +65,37 @@ menuItems.selectionToMarkdown = chrome.contextMenus.create({
}
});

menuItems.pageLinkTitle = chrome.contextMenus.create({
menuItems.pageLinkSelectedText = chrome.contextMenus.create({
parentId: 'root',
title: 'Current page: [title](url)',
title: 'Current page: [selected text](url)',
type: 'normal',
contexts: ['all'],
contexts: ['selection'],
onclick: function (info, tab) {
const text = tab.title;
const text = info.selectionText;
const url = info.pageUrl;
const markdown = `[${text}](${url})`;
copyToClipboard(markdown);
notifyCopied(markdown);
}
});

//// It's not easy to get hyperlink text
//// See https://stackoverflow.com/questions/7427357/getting-hyperlink-text-on-chrome-right-click
// chrome.contextMenus.create({
// title: 'Current link: [text](url)',
// type: 'normal',
// contexts: ['link'],
// onclick: function (info, tab) {
// const text = 'text';
// const url = info.linkUrl;
// const markdown = `[${text}](${url})`;
// alert(markdown);
// }
// });
menuItems.hyperLink = chrome.contextMenus.create({
parentId: 'root',
title: 'This link: [text](link)',
type: 'normal',
contexts: ['link'],
onclick: function (info, tab) {
chrome.tabs.executeScript(null, {file: 'get-hyperlink-info.js'});
}
});

menuItems.pageLinkSelectedText = chrome.contextMenus.create({
menuItems.pageLinkTitle = chrome.contextMenus.create({
parentId: 'root',
title: 'Current page: [selected text](url)',
title: 'Current page: [title](url)',
type: 'normal',
contexts: ['selection'],
contexts: ['all'],
onclick: function (info, tab) {
const text = info.selectionText;
const text = tab.title;
const url = info.pageUrl;
const markdown = `[${text}](${url})`;
copyToClipboard(markdown);
Expand All @@ -106,12 +104,12 @@ menuItems.pageLinkSelectedText = chrome.contextMenus.create({
});

function updateMenuTitle() {
chrome.contextMenus.update(menuItems.pageLinkTitle, {
title: `Current page: [${pageTitle}](${pageUrl})`,
});
chrome.contextMenus.update(menuItems.pageLinkSelectedText, {
title: `Current page: [${pageSelectionText}](${pageUrl})`,
});
chrome.contextMenus.update(menuItems.hyperLink, {
title: `This link: [${linkText}](${linkUrl})`,
});
}

chrome.tabs.onActivated.addListener(function(activeInfo) {
Expand All @@ -135,5 +133,12 @@ chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
} else if (message.request === 'selectedText') {
pageSelectionText = message.selectedText;
updateMenuTitle();

} else if (message.request === 'hyperlinkInfo') {
linkText = message.text;
linkUrl = message.href;
const markdown = `[${linkText}](${linkUrl})`;
copyToClipboard(markdown);
notifyCopied(markdown);
}
});
9 changes: 9 additions & 0 deletions get-hyperlink-info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let element = document.activeElement;
if (element.tagName.toLowerCase() === 'a') {
chrome.runtime.sendMessage({
request: 'hyperlinkInfo',
text: element.innerText,
href: element.href,
});
}

0 comments on commit 9120854

Please sign in to comment.