-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
34 lines (26 loc) · 887 Bytes
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'use strict';
// Set up context menu tree at install time.
chrome.runtime.onInstalled.addListener(function() {
chrome.contextMenus.create({
title: "Copy Link to Here",
id: 'test',
contexts: ['selection']
});
});
// Menu click handler
chrome.contextMenus.onClicked.addListener(function(info, tab){
// Copy the selection to the clipboard as a link.
// We use a text input and execCommand because this is the
// only solution that works on http websites.
chrome.tabs.executeScript(tab.id, {
code: `
var copyFrom = document.createElement("input");
copyFrom.value = window.location.href.replace(/#.*/, '') + '#:~:text=' +
encodeURIComponent(String(getSelection()).trim());
document.body.appendChild(copyFrom);
copyFrom.select();
document.execCommand('copy');
document.body.removeChild(copyFrom);
`
});
});