2011-12-14 2 views
2

클립 보드의 내용을 읽을 수있는 Google 크롬 확장 프로그램을 만들고 있습니다.
그러나 이에 대한 설명서를 얻을 수 없습니다. IE의 클립 보드 API에서와 같이 클립 보드 내용을 가져오고 싶습니다. 매니페스트 파일에서
내가 내가내 확장 기능에서 document.execCommand ('paste')가 작동하지 않는 이유

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) { 
if (request.method == "getClipData") 
    sendResponse({data: document.execCommand('paste')}); 
else 
    sendResponse({}); // snub them. 
}); 

아래와 같은 배경 페이지에서 함수를 만든 및 컨텐트 스크립트에서 나는이

chrome.extension.sendRequest({method: "getClipData"}, function(response) { 
    alert(response.data); 
}); 
같은 함수를 호출하고

clipboardRead and clipboardWrite. 

권한을 준

하지만이 값은 정의되지 않았습니다 ...

+0

[Google 크롬 확장에서 클립 보드 텍스트를 읽는 방법] (http : /stackoverflow.com/questions/8509670/how-to-read-the-clipboard-text-in-google-chrome-extension) –

답변

1
var str = document.execCommand('paste'); 

clipboardReadpermission도 추가해야합니다.

+0

안녕하세요, 클립 보드 대신 True를 반환합니다! 코드가 위와 동일하며 권한을 부여했는데 내가 뭘 잘못하고 있니? – espectalll

+0

Mine도 true를 반환합니다 ... execCommand()가 문자열을 전혀 반환하지 않는 것처럼 보입니다. http://help.dottoro.com/ljcvtcaw.php 문자 그대로 paste 명령을 호출하는 것 같습니다. 이 해결 방법처럼 보입니다. http://stackoverflow.com/questions/7144702/the-proper-use-of-execcommandpaste-in-a-chrome-extension –

0

document.execCommand ('paste')는 클립 보드의 내용이 아니라 성공 또는 실패를 반환합니다.

이 명령은 백그라운드 페이지의 포커스 된 요소에 붙여 넣기 작업을 트리거합니다. 백그라운드 페이지에서 TEXTAREA 또는 DIV contentEditable = true를 만들고 붙여 넣기 컨텐트를 수신하려면 포커스를 지정해야합니다.

당신은 내 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; 
: 여기

https://github.com/jeske/BBCodePaste

이 배경 페이지에서 클립 보드의 텍스트를 읽는 방법의 한 예입니다

HTML 대신 일반 텍스트를 사용하려면 helperdiv.innerText를 사용하거나 텍스트 영역을 사용하도록 전환 할 수 있습니다. 어떤 방식 으로든 HTML을 구문 분석하려면 DIV 내부의 HTML 돔을 걸을 수 있습니다 (내 BBCodePaste 확장 프로그램 참조).

관련 문제