2013-10-24 27 views
0

我正在尝试从DevTools的codeeditor中获取选定的文本。我可以在onSelectionChanged处理程序中获取selectionInfo,但我不知道如何获取文本。如何从DevTools代码编辑器中获取选定的文本?

如何在onSelectionChanged之前获取selectionInfo(当前选择)?

chrome.devtools.panels.sources.createSidebarPane(
    "title", 
    function(sidebar) { 
     function update(selectionInfo) { 
      //alert([selectionInfo.url, selectionInfo.startLine, selectionInfo.endLine, selectionInfo.startColumn, selectionInfo.endColumn]); 
      sidebar.setObject(JSON.parse(JSON.stringify(selectionInfo))); 
      // How to extract text using data from selectionInfo ??? 
     } 

     update(/*selectionInfo should be there*/); 
     chrome.devtools.panels.sources.onSelectionChanged.addListener(update); 
    } 
); 

回答

1

chrome.devtools.panels.elements.onSelectionChanged.addListener回调不带任何参数,比照API documentation。这意味着您的selectionInfo将始终未定义。

要获取所选元素,可以使用$0变量。因此,您的代码应该是这样的:

chrome.devtools.panels.elements.createSidebarPane(
    "title", 
    function(sidebar) { 
     function update() { 
      sidebar.setExpression("$0"); 
     } 

     update(); 
     chrome.devtools.panels.elements.onSelectionChanged.addListener(update); 
    } 
); 

注:我换成chrome.devtools.panels.sources,它不存在,通过chrome.devtools.panels.elements

+0

chrome.devtools.panels.sources存在并允许附加在代码管理器中更改选择的监听器。 (也许你正在使用一些旧的浏览器...我使用Chrome 32.0.1671.3?) selectionInfo也被定义并包含5个属性: selectionInfo.url, selectionInfo.startLine, selectionInfo.endLine, selectionInfo .startColumn, selectionInfo.endColumn。 但selectionInfo.selectedText是真的错过了... – user2916617

+0

我使用Chrome 30.0.1599.101(稳定通道),所以我错过了那一个。查看Chromium源代码,它看起来不像selectionInfo具有selectionText属性,请参阅https://chromiumcodereview.appspot.com/23904024。 –

相关问题