如何获得一个段落与chrome扩展的活动选项卡
How to get a paragraph from the active tab with chrome extension?
在我的background.html
中,我得到当前选项卡的id
, url
和title
:
chrome.tabs.getSelected(null, function(tab) {
tabId = tab.id;
tabUrl = tab.url
tabTitle = tab.title
...
我还需要得到当前选项卡的第一段。这可能吗?在标签类型中我没有看到任何相关的内容。由于
更新响应serg的答案并从这里复制代码:
这是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"
]
}
插入控制台。用于调试的日志,但我在控制台中没有看到它们;事实上dom.js甚至不运行,我把"else"拼错为"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>
Update: Edited 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***")
}
}
);
更新最新的background.html与console.log
s显示标记:
<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>
您不能在后台页面中这样做,但您可以将内容脚本注入所有页面,并使用消息传递将此信息传递回后台页面。
相关文章:
- chrome扩展中的navigator.geolocation.getCurrentPosition
- chrome扩展:尽管运行了at:documentidle,js脚本还是过早启动
- chrome扩展更改主机/域警告
- 如何在chrome扩展中重定向到html页面
- Chrome扩展没有't在重新加载之前考虑期权价值
- 如何通过自己获得Chrome扩展的用户反馈/错误报告
- 试图阻止Chrome通过扩展关闭
- 如何在chrome扩展中存储数据/结果,以及如何使用setTimeout使其只被调用一次
- 从选项页面更新chrome扩展清单权限
- 如何将chrome扩展功能移植到移动设备(特别是jquery和trello)
- Chrome扩展:遍历不同的页面并收集数据
- Chrome扩展 chrome.storage 如何同时进行大量获取和设置并避免竞争条件
- Chrome扩展:Chrome.tabs.executeScript不工作
- 中的访问扩展chrome://extensions页
- 如何在扩展Chrome DevTool时检索请求的启动器
- Chrome扩展-Chrome报警API为创建的每个新报警重新安排所有报警
- 是否有任何方法来扩展chrome's __commandLineAPI
- 谷歌Chrome扩展Chrome .tab . onupdate . addlistener
- 创建在页面上注入脚本的扩展chrome内容
- Chrome扩展:Chrome扩展中未定义的变量,即使它存在于控制台中