在chrome扩展中访问当前选项卡的所有窗口变量 [英] Accessing all the window variables of the current tab in chrome extension

查看:86
本文介绍了在chrome扩展中访问当前选项卡的所有窗口变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以在chrome扩展程序中发布用于访问窗口变量的代码.单击chrome扩展按钮时,我需要访问窗口变量.我正在获取window对象,但未加载所有变量.我知道我们可以通过注入脚本来创建它.但是我没有得到实现它的方法.目前,我正在尝试使用以下代码来获取当前活动标签页的页面来源.

Can anyone post the code for accessing the window variables in chrome extension. I need to access the window variables when i click the chrome extension button. I am getting the window object, but not with all the variables loaded. I know we can create it by injecting the script. But i am not getting how to achieve it. Currently I am trying the following code to get the page source of the current active tab.

 chrome.tabs.executeScript({code:
            "document.getElementsByTagName('html')[0].innerHTML"
        }, function(result) {
    var value = result[0];
    console.log(value);
    chrome.tabs.query({currentWindow: true, active: true}, function(tabs) {
        callback({
            url: tabs[0].url,
            title: tabs[0].title,
            jsonValue: value
        });
    });
}); 

请帮助我解决此问题.我们将不胜感激.

Please help me in solving this issue. It would be highly appreciated.

推荐答案

您要的是注入脚本并获取原始页面脚本中定义的变量的值.这个问题有两个答案:

What you are asking for is to inject a script an get the value of a variable defined in the original page script. There are two answers to this problem:

这是不可能的.注入到页面中的内容脚本已沙盒化,无法访问原始页面的JavaScript范围.这意味着您无法访问原始页面的javascript中定义的变量,函数和对象.并且您的变量,函数和对象将无法从原始页面访问.

This is not possible. Content script injected in a page is sandboxed and can't access to original page javascript scope. This means that you can't access to variable, function and objects defined in the original page's javascript. And your variable, function and objects will not be accessible from the original page.

仅页面的DOM被共享.这样您就可以修改页面的内容.但是,例如,您不能删除现有的事件处理程序.

Only the DOM of the page is shared. That allow you to modify the content of the page. But you can't, for example, delete an existing event handler.

这是出于明显的安全性原因.如果您不了解原始页面的功能便对其进行覆盖,则会破坏该页面.

This is for evident security and safety reason. If you override without knowing it a function of the original page, it would break it.

在此处查看更多信息

有一种方法可以绕过铬砂箱的限制.这与共享DOM一起提供,该共享DOM允许您将< script src ="......">< \ script> 添加到页面.该脚本将在原始页面的javascript VM中加载并执行,因此您可以访问全局javascript作用域.

There is a way to bypass the chrome sand box restriction. This come with the shared DOM that allow you to add a <script src="..."><\script> to the page. The script will be loaded and executed in the original page's javascript VM so you will have access to the global javascript scope.

但是,这样一来,您将无法访问Chrome扩展程序API,因为您正在原始页面中运行代码.因此,与背景页面或注入的内容脚本的通信将很困难.

But this way, will not have access to the Chrome extension API, because you are running code in the original page. So the communication with the background page or the injected Content Script will be difficult.

执行此操作的一种常见方法是在页面上添加隐藏的< div> ,然后将要发送到结果脚本的结果放入页面中.您将结果作为文本输入(例如,使用JSON.stringify),然后使用内容脚本读取结果.

A common way to do this is to add a hidden <div> to the page and put in it the result you want to send to your Content Script. You put the result as text (with JSON.stringify for example) and then read the result with your Content Script.

它真的很脏,只能在最后一次尝试中使用.

It's really dirty and it have to be use only in last try.

这篇关于在chrome扩展中访问当前选项卡的所有窗口变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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