[英]Chrome extension
I am a novice at this and am trying to make a chrome extension for showing me in the popup window the id of all elements with certain class name in the website. 我是新手,并尝试进行chrome扩展,以便在弹出窗口中向我显示网站中具有特定类名称的所有元素的ID。 I wanted to know if my implementation was the best option to resolve the matter.
我想知道我的实施是否是解决此问题的最佳选择。 Thanks for your help, and sorry for my poor english.
感谢您的帮助,对不起我的英语不好。
manifest.json 的manifest.json
{
"name": "Test",
"version": "1.0",
"manifest_version" : 2,
"description": "",
"browser_action": {
"default_icon": "images/icon.png",
"default_popup": "popup.html"
},
"permissions": [ "tabs","http://*/*" ]
}
popup.html popup.html
<!doctype html>
<html>
<head>
<style>
body{
height: 150px;
width: 800px;
overflow: hidden;
margin: 0px;
padding: 0px;
background: white;
}
</style>
<script src="scripts/popup.js"></script>
</head>
<body>
</body>
</html>
popup.js popup.js
// Inserting javascript code
chrome.tabs.executeScript(null, {file: "scripts/content.js"});
// Sending request
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
document.write(response.farewell);
});
});
content.js content.js
// This function gets all the id of the elements that have a class name X and
// returns them in a string separated by ",".
function getId(className) {
// I get all elements containing className
var elements = document.getElementsByClassName(className);
// Creating array with id of the elements
var idElements= new Array();
for (var i = 0; i < elements.length; i++) {
idElements[i]=elements[i].id;
}
// Concatenate all id
var list = idElements.join(" , ");
return list;
}
var result=getId("classNameTest");
// Listening for message from popup.js
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.greeting == "hello")
sendResponse({farewell: result});
});
Any feedback is appreciated, thanks! 任何反馈表示赞赏,谢谢!
U have not registrd content script file in ur manifest file...check the below link for more details...else other work seems to be fine 您尚未在清单文件中注册内容脚本文件...请检查以下链接以获取更多详细信息...其他工作似乎还可以
http://developer.chrome.com/extensions/content_scripts.html http://developer.chrome.com/extensions/content_scripts.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.