-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from yorkxin/chrome-keyboard-copy-hack
Inject an iframe to perform copy on Chrome + Keyboard Shortcut
- Loading branch information
Showing
4 changed files
with
163 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Copy as Markdown (Content Script)</title> | ||
</head> | ||
<body> | ||
<textarea id="copy"></textarea> | ||
<script src="./iframe-copy.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
window.addEventListener('message', (event) => { | ||
switch (event.data.cmd) { | ||
case 'copy': { | ||
const { text } = event.data; | ||
if (text === '' || !text) { | ||
event.source.postMessage({ topic: 'iframe-copy-response', ok: false, reason: 'no text' }, event.origin); | ||
} | ||
|
||
const textBox = document.getElementById('copy'); | ||
textBox.innerHTML = text; | ||
textBox.select(); | ||
const result = document.execCommand('Copy'); | ||
if (result) { | ||
event.source.postMessage({ topic: 'iframe-copy-response', ok: true }, event.origin); | ||
} else { | ||
event.source.postMessage({ topic: 'iframe-copy-response', ok: false, reason: 'execCommand returned false' }, event.origin); | ||
} | ||
textBox.innerHTML = ''; | ||
break; | ||
} | ||
|
||
default: { | ||
event.source.postMessage({ topic: 'iframe-copy-response', ok: false, reason: `unknown command ${event.data.cmd}` }, event.origin); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters