Это будет работать в Chrome 39 и выше.
Сначала вам нужно поместить разрешение «clipboardRead» в раздел разрешений файла манифеста. Смотрите эти ссылки для получения более подробной информации о том, что: https://developer.chrome.com/apps/manifest и https://developer.chrome.com/apps/declare_permissions
Затем вы можете использовать эту функцию:
// getClipboardText - return any text that is currently on the clipboard
function getClipboardText() {
// create div element for pasting into
var pasteDiv = document.createElement("div");
// place div outside the visible area
pasteDiv.style.position = "absolute";
pasteDiv.style.left = "-10000px";
pasteDiv.style.top = "-10000px";
// set contentEditable mode
pasteDiv.contentEditable = true;
// find a good place to add the div to the document
var insertionElement = document.activeElement; // start with the currently active element
var nodeName = insertionElement.nodeName.toLowerCase(); // get the element type
while (nodeName !== "body" && nodeName !== "div" && nodeName !== "li" && nodeName !== "th" && nodeName !== "td") { // if have not reached an element that it is valid to insert a div into (stopping eventually with 'body' if no others are found first)
insertionElement = insertionElement.parentNode; // go up the hierarchy
nodeName = insertionElement.nodeName.toLowerCase(); // get the element type
}
// add element to document
insertionElement.appendChild(pasteDiv);
// paste the current clipboard text into the element
pasteDiv.focus();
document.execCommand('paste');
// get the pasted text from the div
var clipboardText = pasteDiv.innerText;
// remove the temporary element
insertionElement.removeChild(pasteDiv);
// return the text
return clipboardText;
}
Почему бы не вставить в элемент TEXTAREA или ввода, а не в contentEditable DIV? Вот что ответил на вопрос [this] (https://stackoverflow.com/questions/22702446/how-to-get-clipboard-data-in-chrome-extension). – ShreevatsaR
Ну, я не уверен, почему, но это сработало, в то время как в textarea этого не было. Благодаря! – ShreevatsaR