如何从带有chrome扩展名的活动标签中获取段落?

时间:2011-10-11 22:18:52

标签: javascript google-chrome-extension

在我的background.html我正在获取当前标签的idurltitle,如下所示:

chrome.tabs.getSelected(null, function(tab) {
  tabId = tab.id;
  tabUrl = tab.url
  tabTitle = tab.title
  ...

我还需要获取当前标签的第一段。这可能吗?我在tab type中没有看到任何相关内容。感谢

更新

更新以回复serg's answer并从here复制代码:

这是dom.js

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
 if (request.action == "getDOM")
   //sendResponse({dom: "The dommmmm that you want to get"});
   sendResponse({dom: document.getElementsByTagName("body")[0]});
   //sendResponse({dom: document.getElementsByTagName("p")});
 else
   sendResponse({}); // Send nothing..
});

这就是我在background.html中所拥有的:

  // Send a request to the content script.
  chrome.tabs.sendRequest(tab.id, {action: "getDOM"}, function(response) {
    console.log(response.dom);
  });

但似乎没有任何事情发生。我甚至不知道在哪里看。

更新

manifest.json:

{
  "name": "My First Extension",
  "version": "1.0",
  "description": "The first extension that I made.",
  "background_page": "background.html",
  "content_scripts": [
     {
       "matches": ["http://*/*"],
       "js": ["dom.js"]
     }
   ],  
  "browser_action": {
    "default_icon": "icon.png"
  },
  "permissions": [
    "tabs",
    "http://ting-1.appspot.com/submithandlertest"
  ]
}

更新

插入console.logs进行调试,但我在控制台中看不到它们;实际上dom.js甚至没有运行,我拼错了“其他”作为“els”并没有给出任何错误。但是否则扩展工作正常并执行xhr请求。我做错了什么?

dom.js

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
 if (request.action == "getDOM")
    console.log("dom.js - request action")
   sendResponse({dom: document.getElementsByTagName("body")[0]});
   console.log("***dom.js - sendResponse***")
 else
   sendResponse({}); // Send nothing..
   console.log("***dom.js - else***")
});

background.html

<html>
<script>
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.getSelected(null, function(tab) {

  // Send a request to the content script.
  chrome.tabs.sendRequest(tab.id, {action: "getDOM"}, function(response) {
    console.log(response.dom);
    console.log("background -- where is response.dom")
  });

    tabId = tab.id;
    console.log(tabId)
    tabUrl = tab.url
    console.log(tabUrl)
    tabTitle = tab.title
    console.log(tabTitle)

var formData = new FormData();
formData.append("url", tabUrl);
formData.append("title", tabTitle);
formData.append("pitch", "this is a note");
//formData.append("user_tag_list", "tag1, tag2");


var xhr = new XMLHttpRequest();
xhr.open("POST", "http://ting-1.appspot.com/submithandlertest", true);
xhr.onreadystatechange = function (aEvt) {
    if (xhr.readyState == 4) {
        if (xhr.status == 200){ 
            console.log("request 200-OK");
            chrome.browserAction.setBadgeText ( { text: "done" } );
            setTimeout(function () {
            chrome.browserAction.setBadgeText( { text: "" } );
            }, 2000);
        }else{
            console.log("connection error");
            chrome.browserAction.setBadgeText ( { text: "ERR" } );
     }        
  }        
};
xhr.send(formData);
console.log("tabUrl: "+ tabUrl)
console.log(formData)

        });
    });
</script>
</html>

修改

按照serg的建议编辑background.html“将所有需要它的代码包装到chrome.tabs.sendRequest回调(所有那些xhrs)”中。但是这个版本的背景不起作用。我没有收到任何错误消息,但没有任何反应。我做错了什么?

<html>
    <script>
        chrome.browserAction.onClicked.addListener(function(tab) 
        {
            //chrome.tabs.getSelected(null, function(tab) 
            //{
            // Send a request to the content script.
            chrome.tabs.sendRequest(tab.id, {action: "getDOM"}, function(response) 
            {
                console.log(response.dom);
                console.log("background -- where is response.dom")
                //}); moving this down to include xhr
                tabId = tab.id;
                console.log(tabId)
                tabUrl = tab.url
                console.log(tabUrl)
                tabTitle = tab.title
                console.log(tabTitle)

                var formData = new FormData();
                formData.append("url", tabUrl);
                formData.append("title", tabTitle);
                formData.append("pitch", "this is a note");
                //formData.append("user_tag_list", "tag1, tag2");

//xhr*********************************************************************************        
                var xhr = new XMLHttpRequest();
                xhr.open("POST", "http://ting-1.appspot.com/submithandlertest", true);
                xhr.onreadystatechange = function (aEvt) 
                {
                    if (xhr.readyState == 4) 
                    {
                        if (xhr.status == 200)
                        { 
                            console.log("request 200-OK");
                            chrome.browserAction.setBadgeText ( { text: "done" } );
                            setTimeout(function () 
                            {
                                chrome.browserAction.setBadgeText( { text: "" } );
                            }, 2000);
                        }
                        else
                        {
                            console.log("connection error");
                            chrome.browserAction.setBadgeText ( { text: "ERR" } );
                        }        
                    }        
                };
                xhr.send(formData);
                console.log("tabUrl: "+ tabUrl)
                console.log(formData)
//xhr**********************************************************************************                
            }); //chrome.tabs.sendRequest
        });
    </script>
</html>

更新:已编辑dom.js:

chrome.extension.onRequest.addListener
(
    function(request, sender, sendResponse) 
    {
        if (request.action == "getDOM")
        {
            console.log("dom.js - if (request.action == getDom)")
            //sendResponse({dom: "The dommmmm that you want to get"});
            //sendResponse({dom: document.getElementsByTagName("body")[0]});

            sendResponse({dom: document.getElementsByTagName("p")[0]});
            console.log("***dom.js - sendResponse***")
            //var pageInfo = {
            //"title": document.title};
            //chrome.extension.sendRequest(pageInfo);
        }
        else
        {
            sendResponse({}); // Send nothing..
            console.log("***dom.js - else***")
        }
    }
);

使用显示标记为{/ strong>

console.log的最新background.html进行更新
<html>
<script>
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.getSelected(null, function(tab) {

  // Send a request to the content script.
  chrome.tabs.sendRequest(tab.id, {action: "getDOM"}, function(response) {
    //NOT SHOWING
    console.log(response.dom)
    //NOT SHOWING
    console.log("background -- where is response.dom")
  });

    tabId = tab.id;
    //SHOWING
    console.log(tabId)
    tabUrl = tab.url
    //SHOWING
    console.log(tabUrl)
    //SHOWING
    tabTitle = tab.title
    //SHOWING
    console.log(tabTitle)

var formData = new FormData();
formData.append("url", tabUrl);
formData.append("title", tabTitle);
formData.append("pitch", "this is a note");
//formData.append("user_tag_list", "tag1, tag2");

var xhr = new XMLHttpRequest();
xhr.open("POST", "http://ting-1.appspot.com/submithandlertest", true);
xhr.onreadystatechange = function (aEvt) {
    if (xhr.readyState == 4) {
        if (xhr.status == 200){
            //SHOWING 
            console.log("request 200-OK");
            chrome.browserAction.setBadgeText ( { text: "done" } );
            setTimeout(function () {
            chrome.browserAction.setBadgeText( { text: "" } );
            }, 2000);
        }else{
            //NOT SHOWING
            console.log("connection error");
            chrome.browserAction.setBadgeText ( { text: "ERR" } );
     }        
  }        
};
xhr.send(formData);
//SHOWING
console.log("tabUrl: "+ tabUrl)
//SHOWING
console.log(formData)

        });
    });
</script>
</html>

1 个答案:

答案 0 :(得分:4)

您无法从后台页面执行此操作,但可以将content script注入所有页面,并使用message passing将此信息按需传递回背景页。