我想制作一个Chrome开发者工具扩展程序,需要访问源窗格中新添加的代码段。
chrome.devtools API有没有办法访问代码段?
答案 0 :(得分:12)
是的,您可以通过chrome.devtools.inspectedWindow API()
来完成您可以追踪
a) Content of all Snippets available
b) When ever a new Snippet is added and its content
c) When ever a Snippet is Updated with new content\modified.
如何启用调试等enable experimental developer flags。
您可以将以下代码作为参考,您可以根据自己的要求进行扩展。
manifest.json
你必须添加
“devtools_page”: “devtools.html”,
您的manifest.json文件的代码
示例manifest.json
{
"name":"Snippets Demo",
"description":"This demonstrates How to get content from Snippets API",
"devtools_page":"devtools.html",
"manifest_version":2,
"version":"2"
}
<强> devtools.html 强>
添加devtools.js
以避免inline scripting
示例devtools.html
<html>
<head>
<script src="devtools.js"></script>
</head>
<body>
</body>
</html>
<强> devtools.js 强>
为
添加相关代码a) chrome.devtools.inspectedWindow.getResources
b) chrome.devtools.inspectedWindow.onResourceAdded.addListener
c) chrome.devtools.inspectedWindow.onResourceContentCommitted.addListener()
示例devtools.js
//Fetching all available resources and filtering using name of script snippet added
chrome.devtools.inspectedWindow.getResources(function (resources){
// This function returns array of resources available in the current window
for(i=0;i<resources.length;i++){
// Matching with current snippet URL
if(resources[i].url == "Script snippet #1"){
resources[i].getContent(function (content,encoding){
alert("encoding is " + encoding);
alert("content is "+content);
});
}
}
});
//This can be used for identifying when ever a new resource is added
chrome.devtools.inspectedWindow.onResourceAdded.addListener(function (resource){
alert("resources added" + resource.url);
alert("resources content added " + resource.content);
});
//This can be used to detect when ever a resource code is changed/updated
chrome.devtools.inspectedWindow.onResourceContentCommitted.addListener(function(resource,content){
alert("Resource Changed");
alert("New Content " + content);
alert("New Resource Object is " + resource);
});
将所有3个代码放在一起后,你得到
输出1)
输出2)
输出3)
希望这有帮助:)
答案 1 :(得分:0)
我正在搜索此内容,但已接受的答案相当陈旧,截至2016年1月,您无法通过localStorage
访问代码段。
另见:
https://github.com/bahmutov/code-snippets/issues/23
Which file does Snippets of Chrome Dev Tool saved at?