Chrome扩展程序,用于加载活动选项卡的页面源 [英] Chrome Extension that loads Page Source of the active tab

查看:100
本文介绍了Chrome扩展程序,用于加载活动选项卡的页面源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望你能帮助我。



我试图在Chrome中创建一个扩展,它会将活动选项卡的源加载到变量中。



到目前为止,我有:

manifest.json

  {
name:My Extension,
manifest_version:2,
version:0.1 ,
description:做一些简单的事情,
browser_action:{
default_icon:logo.png
},

background:{
scripts:[main.js]}}



main.js

  chrome.browserAction.onClicked.addListener(
函数(tab){
var ps1 = document.getElementsByTagName('html')[0] .innerHTML;
window.alert(ps1);
});

但加载空白页面的页面源代码。我需要做些什么才能获得活动页面的来源。

我已经做了一些阅读,我认为我需要使用内容脚本来处理一些聆听功能,我一直在寻找,但所有的答案在我看来都非常复杂。你们中的任何一个人会如此友善地给我一些简单的例子吗?



非常感谢您的反馈!



此致




AdrianCooney回复后的更新:



我将清单更改为包含

 permissions:[
标签
]

然后在main.js中,我做了

 chrome.browserAction.onClicked.addListener(function(activeTab){
chrome.tabs.query({currentWindow:true,active:true},
function(tabs ){
var ps1 = document.getElementsByTagName('html')[0] .innerHTML;
window.alert(ps1);
})
});

当我按下扩展按钮时,我得到了类似的内容

 < html>< / html> 
< body>< script src =main.js>< / script>
< / body>

...无论我有哪些标签,都是有效的。

另一个尝试chrome.tabs.getCurrent



  chrome.browserAction.onClicked.addListener(function(activeTab){ 
chrome.tabs.getCurrent(
function(tabs){
var ps1 = document.getElementsByTagName('html')[0] .innerHTML;
window.alert(ps1) ;
})
});

以上版本的main.js给出了与前一版本完全相同的输出,无论是第一页

解决方案

后台脚本与当前页面隔离,因为它们被设计为持续不管页。您需要使用 chrome.tabs API,特别是 chrome.tabs.getCurrent 。一旦拥有当前选项卡,就可以将代码注入到窗口中。这就是你上面的代码片段的来源。



您需要在清单文件中为您的权限添加tab。


I hope you can help me.

I am trying to create an extension in Chrome which would load a source of the active tab into a variable.

So far I have:

manifest.json

{
"name": "My Extension",
"manifest_version": 2,
"version": "0.1",
"description": "Does some simple stuff",
"browser_action": {
    "default_icon": "logo.png"
},

"background": {    
"scripts": ["main.js"]}}

main.js

chrome.browserAction.onClicked.addListener(
  function(tab) {
   var ps1 = document.getElementsByTagName('html')[0].innerHTML;
   window.alert(ps1);
});

but that loads the page source of the blank page. What do I need to do to get the source of the active page.

I have done some reading and I think I need to use content script whit some listening functions, I have been searching but all answers seem to me very complicated. Would any of you be so kind to give me some easy example?

Highly appreciate your feedback!

Regards


UPDATE after AdrianCooney answer:

I changed my manifest to contain

 "permissions": [
  "tabs"
  ]

Then in main.js I did

chrome.browserAction.onClicked.addListener(function(activeTab) {
 chrome.tabs.query({ currentWindow: true, active: true }, 
  function (tabs) {
   var ps1=document.getElementsByTagName('html')[0].innerHTML;
   window.alert(ps1);
 })
});

When I press the Extension button I get something like that

<html></html>
  <body><script src="main.js"></script>
  </body>

...no matter what tab I have active.

Another try with chrome.tabs.getCurrent

chrome.browserAction.onClicked.addListener(function(activeTab) {
 chrome.tabs.getCurrent(
  function (tabs) {
   var ps1=document.getElementsByTagName('html')[0].innerHTML;
   window.alert(ps1);
  })
 });

The above version of main.js give exact same output as the one before, no matter what page I have active.

解决方案

Background scripts are isolated from the current page because they're designed to be persistant regardless of the content of the page. You need to use the chrome.tabs API, specifically chrome.tabs.getCurrent. Once you have the current tab, you can inject code into the window. That's where your snippet above comes in.

You will need to add "tab" to your permissions in the manifest file.

这篇关于Chrome扩展程序,用于加载活动选项卡的页面源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆