2011-09-17 3 views
2

숫자 또는 텍스트의 선택 여부에 따라 다른 메뉴 옵션을 표시하려고합니다.선택에 따라 컨텍스트 메뉴 렌더링

콘텐츠 스크립트를 사용해 보았지만 작동에 필요한 gmail에서 작동하지 못했습니다. 다음은 Gmail이 아닌 다른 사이트 (그것이 HTTPS 것입니까?)

Background.html

<script src="driver.js"></script>

content_script.js

document.addEventListener("mousedown", function(event){ 
    if(event.button == 2) { 
    var selection = window.getSelection().toString(); 
    chrome.extension.sendRequest({cmd: selection}); 
    } 

}, true); 
에서 작동, 내가 가진 무엇

driver.js

chrome.extension.onRequest.addListener(function(request) { 
    alert(request.cmd); 
}); 

manifest.json을

{ 
"name": "Context Menu Search", 
"description": "Opens the selected text as keyword in a new window", 
"version": "0.1", 
"permissions": ["contextMenus"], 
    "content_scripts": [ 
    { 
     "matches": ["http://*/*","https://*/*"], 
     "js": ["content_script.js"] 
    } 
    ], 
"background_page": "background.html" 
} 
+0

볼 http://stackoverflow.com/questions/6382467/chrome-extension-context-menus-how-to-display-a-menu-item-only-when-there-is-no – serg

답변

1

Selection type changes context menu using chrome extension

당신은 아래로 마우스 리스너를 설정해야합니다. 메뉴를 만들기 전에 선택한 텍스트를 가져 오는 다른 방법은 없습니다.

chrome extension context menus, how to display a menu item only when there is no selection?

여기 나머지는 링크에있는 코드의 일부입니다

이 SO 질문을 참조하십시오.

document.addEventListener("mousedown", function(event){ 
//right click 
if(event.button == 2) { 
    if(window.getSelection().toString()) { 
     chrome.extension.sendRequest({cmd: "createSelectionMenu"}); 
    } else { 
     chrome.extension.sendRequest({cmd: "createRegularMenu"}); 
    } 
    } 
}, true); 
+0

감사합니다 , 나는 연극을 가지고 그 일을했습니다. 그러나 https 사이트에서는 작동하지 않습니다. 내 Gmail 계정에서 사용할 수 있도록이 확장 프로그램을 작성하고 있습니다. 매니페스트 파일에 다음을 추가했습니다. "content_scripts"[ { "일치": "HTTP : // */*", "https : //로 */*", "의 JS": [ "content_script.js"] } ], – BeepBoop

+1

내 코드로 질문을 업데이트했습니다. – BeepBoop

+1

제대로 작동했습니다. 매니페스트 파일에서 "all_frames"설정을 true로 설정하면 Gmail에서 작업을 시작할 수 있습니다. "권한": [ "contextMenus"], "content_scripts"[ { "일치": "HTTP : // */*", "https : //로 */*", "의 JS ": ["content_script.js "], "all_frames ": true } ], – BeepBoop