Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google chrome extension 如何从DevTools代码编辑器中获取所选文本?_Google Chrome Extension_Google Chrome Devtools - Fatal编程技术网

Google chrome extension 如何从DevTools代码编辑器中获取所选文本?

Google chrome extension 如何从DevTools代码编辑器中获取所选文本?,google-chrome-extension,google-chrome-devtools,Google Chrome Extension,Google Chrome Devtools,我正在尝试从DevTools的codeeditor中获取所选文本。我可以在onSelectionChanged处理程序中获取selectionInfo,但我不知道如何获取文本 另外,在启动onSelectionChanged之前,如何获取selectionInfo(当前选择) chrome.devtools.panels.sources.createSidebarPane( “头衔”, 函数(侧栏){ 功能更新(选择信息){ //警报([selectionInfo.url,selectionIn

我正在尝试从DevTools的codeeditor中获取所选文本。我可以在onSelectionChanged处理程序中获取selectionInfo,但我不知道如何获取文本

另外,在启动onSelectionChanged之前,如何获取selectionInfo(当前选择)


chrome.devtools.panels.sources.createSidebarPane(
“头衔”,
函数(侧栏){
功能更新(选择信息){
//警报([selectionInfo.url,selectionInfo.startLine,selectionInfo.endLine,selectionInfo.startColumn,selectionInfo.endColumn]);
setObject(JSON.parse(JSON.stringify(selectionInfo));
//如何使用selectionInfo中的数据提取文本???
}
更新(/*选择信息应在那里*/);

chrome.devtools.panels.sources.onSelectionChanged.addListener(更新); } );
chrome.devtools.panels.elements.onSelectionChanged.addListener的回调不接受任何参数,cf。这意味着您的
选择信息将始终处于未定义状态

要获取所选元素,可以使用
$0
变量。因此,您的代码如下所示:

chrome.devtools.panels.elements.createSidebarPane(
“头衔”,
函数(侧栏){
函数更新(){
sidebar.setExpression(“$0”);
}
更新();
chrome.devtools.panels.elements.onSelectionChanged.addListener(更新);
}
);

注意:我将不存在的
chrome.devtools.panels.sources
替换为
chrome.devtools.panels.elements

chrome.devtools.panels.sources存在,并允许附加在codeetor中更改选择的侦听器。(可能您使用的是一些旧浏览器?…我使用的是Chrome 32.0.1671.3)selectionInfo也已定义并包含5个属性:selectionInfo.url、selectionInfo.startLine、selectionInfo.endLine、selectionInfo.startColumn、selectionInfo.endColumn。但是selectionInfo.selectedText确实错过了…我使用的是Chrome 30.0.1599.101(稳定频道),所以我确实错过了那个。查看Chromium源代码,selectionInfo似乎没有selectionText属性,cf。