javascript-如何从带有Chrome扩展名的活动标签中获取段落?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-如何从带有Chrome扩展名的活动标签中获取段落?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6603字,纯文字阅读大概需要10分钟。
内容图文
在我的background.html中,我得到了当前选项卡的ID,URL和标题,如下所示:
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甚至没有运行,我将“ 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>
更新:编辑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***")
}
}
);
使用显示有以下标记的console.logs更新最新的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>
解决方法:
您不能从后台页面执行此操作,但是可以向所有页面注入content script,然后使用message passing将此信息按需传递回后台页面.
内容总结
以上是互联网集市为您收集整理的javascript-如何从带有Chrome扩展名的活动标签中获取段落?全部内容,希望文章能够帮你解决javascript-如何从带有Chrome扩展名的活动标签中获取段落?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。