[英]Chrome extension
我是新手,並嘗試進行chrome擴展,以便在彈出窗口中向我顯示網站中具有特定類名稱的所有元素的ID。 我想知道我的實施是否是解決此問題的最佳選擇。 感謝您的幫助,對不起我的英語不好。
的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
<!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
// 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
// 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});
});
任何反饋表示贊賞,謝謝!
您尚未在清單文件中注冊內容腳本文件...請檢查以下鏈接以獲取更多詳細信息...其他工作似乎還可以
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.