Javascript 从选项卡获取HTML内容
我在这里完全不知所措。我想从Chrome的标签中获取html内容 manifest.jsonJavascript 从选项卡获取HTML内容,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,我在这里完全不知所措。我想从Chrome的标签中获取html内容 manifest.json { "manifest_version": 2, "name": "Test",is a test.", "version": "1.0", "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'", "background": { "scripts": ["main.j
{
"manifest_version": 2,
"name": "Test",is a test.",
"version": "1.0",
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
"background": {
"scripts": ["main.js"],
"persistent": false
},
"permissions": [
"tabs",
"https://www.google.com"
]
}
{
"manifest_version": 2,
"name": "Test is a test.",
"version": "1.0",
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"content.js"
]
}
],
"background": {
"scripts": [
"background.js"
],
"persistent": false
},
"browser_action": {
"default_title": "title"
},
"permissions": [
"tabs",
"<all_urls>"
]
}
main.js
var timerObj = new Timer({'interval':5000});
chrome.runtime.onStartup.addListener(timerObj.start(mainF));
function mainF() {
chrome.tabs.query( {} ,function (tabs) {
for (var i = 0; i < tabs.length; i++) {
var url = tabs[i].url;
if (url != null) {
console.log(tabs[i].url);
//I want to get html source here
}
}
});
};
function Timer( obj ){
var timerObj=新计时器({'interval':5000});
chrome.runtime.onStartup.addListener(timerObj.start(mainF));
函数mainF(){
chrome.tabs.query({},函数(tabs){
对于(变量i=0;i
最后一行
函数计时器(obj){
为简洁起见被截断。console.log(制表符[i].url);
用于测试。对于每个选项卡,我希望获得html源代码。有了该源代码,我将解析标记和其他内容。我看到其他资源提到了sendMessage
和onMessage
,但我没有真正了解它。许多其他资源都提到了不推荐使用的sendRequest
和onRequest
>.据我所知,有三种方法可以实现这一点
manifest.json
的方式注入内容脚本,然后使用chrome.runtime.sendMessage
和chrome.runtime.onMessage
传输数据browserAction
触发事件,然后您可以切换不同的方法,通过注释掉其他两种方法来获取网页的源代码,并且只保留一种
manifest.json
{
"manifest_version": 2,
"name": "Test",is a test.",
"version": "1.0",
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
"background": {
"scripts": ["main.js"],
"persistent": false
},
"permissions": [
"tabs",
"https://www.google.com"
]
}
{
"manifest_version": 2,
"name": "Test is a test.",
"version": "1.0",
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"content.js"
]
}
],
"background": {
"scripts": [
"background.js"
],
"persistent": false
},
"browser_action": {
"default_title": "title"
},
"permissions": [
"tabs",
"<all_urls>"
]
}
方法2不工作,我有“TypeError:无法读取未定义的”属性“sourceCode”在IE上运行吗?我有一个类似的问题,我必须使用IE。