2014-03-28 2 views

답변

13

기본적으로 document.execCommand('paste|copy|cut')을 사용하여 클립 보드를 조작 할 수 있습니다.

  • "clipboardWrite"permissions을 명시해야합니다.
  • <input> 요소 (또는 <textarea>) <input>value 속성에서 그
  • 전화 document.execCommand('paste')
  • 잡아 당신 문자열
  • 넣어 초점을 만듭니다.

이것은 클립 보드로 데이터를 복사하는 데 유용했습니다. 크롬 확장에 클립 보드의 텍스트를 읽기 위해서는

+0

'붙여 넣기'를 사용하여 클립 보드에서 데이터를 가져올 수 있습니까? – schumacherj

+0

예, 정상적으로 작동합니다. 대답을 업데이트했습니다. 그렇지 않으면 크롬이'clipboardWrite' 권한을 필요로하는 이유를 알 수 없습니다. –

+0

그것은 나를 위해 작동하지 않습니다 ... – ihorko

3

, 당신은에 있습니다에

  • 요청 "clipboardRead"권한 매니페스트 배경 만 스크립트가 클립 보드에 액세스 할 수 있기 때문에
  • 는 배경 스크립트를 만들
  • 배경 페이지에 요소를 만들어 클립 보드 붙여 넣기 작업을 수락하십시오. 이것을 텍스트 영역으로 만들면 일반 텍스트를 얻을 수 있습니다. contentEditable = true로 div를 만들면 클립 보드 데이터를 페이지 스크립트로 다시 전달하려면 서식이 지정된 HTML
  • , 여기에 클립 보드의 텍스트를 읽는 방법의 한 예는

    https://github.com/jeske/BBCodePaste

    입니다 :이 모든 작업의 ​​예를 볼 수있는 메시지 전달 API를

를 사용해야 할 것이다, 내 BBCodePaste 확장 참조 배경 페이지 :

bg = chrome.extension.getBackgroundPage();  // get the background page 
bg.document.body.innerHTML= "";     // clear the background page 

// add a DIV, contentEditable=true, to accept the paste action 
var helperdiv = bg.document.createElement("div"); 
document.body.appendChild(helperdiv); 
helperdiv.contentEditable = true; 

// focus the helper div's content 
var range = document.createRange(); 
range.selectNode(helperdiv); 
window.getSelection().removeAllRanges(); 
window.getSelection().addRange(range); 
helperdiv.focus();  

// trigger the paste action 
bg.document.execCommand("Paste"); 

// read the clipboard contents from the helperdiv 
var clipboardContents = helperdiv.innerHTML; 
관련 문제