Javascript 铬延伸

Javascript 铬延伸,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,我是一个新手在这方面,并试图使一个chrome扩展显示在弹出窗口中的所有元素的id与某些类名在网站上我。 我想知道我的实施是否是解决问题的最佳选择。 谢谢你的帮助,很抱歉我的英语很差 manifest.json { "name": "Test", "version": "1.0", "manifest_version" : 2, "description": "", "browser_action": { "default_icon": "images/icon.

我是一个新手在这方面,并试图使一个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>
content.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);
    });
  }); 
// 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});
  });
//此函数获取类名为X和的元素的所有id
//以“,”分隔的字符串形式返回它们。
函数getId(类名称){
//我得到所有包含className的元素
var elements=document.getElementsByClassName(className);
//创建元素id为的数组
var ideelements=新数组();
对于(var i=0;i

任何反馈都将不胜感激,谢谢

您尚未在您的清单文件中注册内容脚本文件…请查看下面的链接以了解更多详细信息…否则其他工作似乎可以