Google chrome extension 在弹出窗口中显示源代码的特定行
我对chrome扩展的整个世界都很陌生 我已经阅读了教程“hello world”页面,并试图了解content_脚本和background.html-但我可能服药过量,似乎找不到答案,我确信这是一项简单的任务 在选项卡中,站点包含以下隐藏的HTML:Google chrome extension 在弹出窗口中显示源代码的特定行,google-chrome-extension,Google Chrome Extension,我对chrome扩展的整个世界都很陌生 我已经阅读了教程“hello world”页面,并试图了解content_脚本和background.html-但我可能服药过量,似乎找不到答案,我确信这是一项简单的任务 在选项卡中,站点包含以下隐藏的HTML: <div class="XYZ"> <input id="form_ID" type="hidden" value="REF_CODE#Product_CODE#Product Name#"> </div>
<div class="XYZ">
<input id="form_ID" type="hidden" value="REF_CODE#Product_CODE#Product Name#">
</div>
manifest.json
{
// Required
"name": "WP Debug",
"version": "0.0.1",
// Recommended
"description": "A plain text description",
"icons": { "48": "icon.png" },
//"default_locale": "en",
// Pick one (or none)
"browser_action": {
"default_icon": "icon.png", // optional
"default_title": "WP Debug", // optional; shown in tooltip
"popup": "popup.html"
},
"permissions": [ "http://*/", "https://*/", "tabs" ],
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["content_script.js" ],
"run_at": "document_idle"
}
]
}
弹出窗口需要向内容脚本发送消息,内容脚本收集隐藏字段信息并将响应发送回弹出窗口 以下是一个例子: popup.html
<html>
<head>
<script>
function readIds() {
console.log('Send request to content script');
document.getElementById("response").innerText = "Requesting...";
chrome.tabs.getSelected(null,function(tab){
chrome.tabs.sendRequest(tab.id, {cmd: "readIds"}, function(response){
console.log('Response from page is:' + response);
document.getElementById("response").innerText = JSON.stringify(response);
});
});
}
</script>
</head>
<body style="width:400px;">
<a href="javascript:readIds();return false;">Click to read ids</a>
<pre id="response"></pre>
</body>
</html>
mainfest.json
{
// Required
"name": "Foo Extension",
"version": "0.0.1",
// Recommended
"description": "A plain text description",
"icons": { "48": "foo.png" },
//"default_locale": "en",
// Pick one (or none)
"browser_action": {
"default_icon": "Foo.png", // optional
"default_title": "Foo Extension", // optional; shown in tooltip
"popup": "popup.html"
},
"permissions": [ "http://*/", "https://*/", "tabs" ],
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["content_script.js" ],
"run_at": "document_idle"
}
]
}
请参阅文档:弹出窗口需要向内容脚本发送一条消息,然后内容脚本将收集隐藏字段信息并将响应发送回弹出窗口 以下是一个例子: popup.html
<html>
<head>
<script>
function readIds() {
console.log('Send request to content script');
document.getElementById("response").innerText = "Requesting...";
chrome.tabs.getSelected(null,function(tab){
chrome.tabs.sendRequest(tab.id, {cmd: "readIds"}, function(response){
console.log('Response from page is:' + response);
document.getElementById("response").innerText = JSON.stringify(response);
});
});
}
</script>
</head>
<body style="width:400px;">
<a href="javascript:readIds();return false;">Click to read ids</a>
<pre id="response"></pre>
</body>
</html>
mainfest.json
{
// Required
"name": "Foo Extension",
"version": "0.0.1",
// Recommended
"description": "A plain text description",
"icons": { "48": "foo.png" },
//"default_locale": "en",
// Pick one (or none)
"browser_action": {
"default_icon": "Foo.png", // optional
"default_title": "Foo Extension", // optional; shown in tooltip
"popup": "popup.html"
},
"permissions": [ "http://*/", "https://*/", "tabs" ],
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["content_script.js" ],
"run_at": "document_idle"
}
]
}
请参阅文档:感谢您的反馈和示例。。将在未来几天内进行测试。不用担心。如果这回答了您的问题,请将其标记为答案。谢谢。嗨,Richard,我收到了未捕获的SyntaxError:非法返回语句弹出窗口。html:1不确定如何处理?。请用您的代码更新您的问题,我可以看一下。嗨,Richard,我已用文件中当前的代码更新了我的问题。。感谢您的反馈和示例Richard。。将在未来几天内进行测试。不用担心。如果这回答了您的问题,请将其标记为答案。谢谢。嗨,Richard,我收到了未捕获的SyntaxError:非法返回语句弹出窗口。html:1不确定如何处理?。请用您的代码更新您的问题,我可以看一下。嗨,Richard,我已用文件中当前的代码更新了我的问题。。谢谢
{
// Required
"name": "Foo Extension",
"version": "0.0.1",
// Recommended
"description": "A plain text description",
"icons": { "48": "foo.png" },
//"default_locale": "en",
// Pick one (or none)
"browser_action": {
"default_icon": "Foo.png", // optional
"default_title": "Foo Extension", // optional; shown in tooltip
"popup": "popup.html"
},
"permissions": [ "http://*/", "https://*/", "tabs" ],
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["content_script.js" ],
"run_at": "document_idle"
}
]
}