简体   繁体   English

修改javascript以显示函数范围的变量

[英]Modify javascript to expose a function-scoped variable

I would like to be able to expose a function-scoped variable that is part of a website's (not mine) javascript. 我希望能够公开一个功能范围的变量,该变量是网站(不是我的)JavaScript的一部分。

Here's a simplified example: 这是一个简化的示例:

function addHooks(e, t, n, i) {
    var props = {                    // Would like to expose this
        update: function() { ... },
        remove: function() { ... },
        find: function() { ... },
    };
    ...
}

In the above example, I would like to expose the props variable so that my webextension can then access this to modify the behavior of the website. 在上面的示例中,我想公开props变量,以便我的webextension可以访问该变量以修改网站的行为。 Please note, that the website that serves this JS file isn't under my control and thus, I cannot simply modify the source file to export this variable. 请注意,提供此JS文件的网站不受我的控制,因此,我不能简单地修改源文件以导出此变量。 However, I'm fully open to modifying the final code that the browser runs (I just don't know how to). 但是,我完全愿意修改浏览器运行的最终代码(我只是不知道该怎么做)。 The javascript file containing the addHooks function appears to be added dynamically via XHR . 包含addHooks函数的javascript文件似乎是通过XHR动态添加的。

Although I have no idea on how to accomplish this programmatically, I have had some success setting a breakpoint and then issuing window.siteController = props in the browser's developer console. 尽管我不知道如何以编程方式完成此任务,但我在设置断点然后在浏览器的开发人员控制台中发布window.siteController = props取得了一些成功。 Unfortunately, manual user-intervention is not something I can package and distribute. 不幸的是,我不能打包和分发手动用户干预。

One method that I have been toying with is the idea of making an AJAX request for the JS file, modifying its script contents and appending it to the page. 我一直在用的一种方法是对JS文件提出AJAX请求,修改其脚本内容并将其附加到页面上。

Is there a canonical, programmatic way in which a function-scoped variable can be exposed? 是否存在可以公开功能范围变量的规范化编程方式?

EDIT: Thanks to Makyen who pointed out my error. 编辑:感谢Makyen指出了我的错误。 Apparently you might be able to do it if you re-define the function in a page script and insert it as a script element. 显然,如果您在页面脚本中重新定义该函数并将其作为脚本元素插入,则可能可以执行此操作。 (See the comment for two references). (请参阅注释以获得两个参考)。

Just be sure that your <script> element is inserted after the original definition and then you can either return props at the end of the function, or use this.props and use the function either as a constructor or using the call function, passing an object that will get the props property added. 只要确保将<script>元素插入到原始定义之后,然后您就可以在函数的末尾return props ,或者使用this.props并将该函数用作构造函数或使用call函数,并传递将添加props属性的对象。


Old answer ( __ wrong__ apparently) 旧答案(显然是__错误__)

Short answer: you can't. 简短的答案:您不能。

Full explanation: if you can't change the code of the function and you can only use the function (call it) you cannot alter its variable's scope (and good thing you can't, too. There's a reason why the variable was limited to its current scope). 完整说明:如果您无法更改函数代码,而只能使用函数(调用它),则无法更改其变量的范围(而且也不能更改。这是变量受限制的原因到当前范围)。

And you can't change the website's JavaScript. 而且您不能更改网站的JavaScript。 As a Firefox or Chrome extension (and possibly the same fires to other browsers I'm not sure) you can't even access the function (let alone it's inner variables). 作为Firefox或Chrome扩展程序(可能不确定是否会触发其他浏览器),您甚至无法访问该函数(更不用说它是内部变量了)。 I mean to say you can't even call it from the extension of it's in the website's code. 我的意思是说,您甚至不能从网站代码中的扩展名中调用它。

content scripts cannot see JavaScript variables defined by page scripts More info and source 内容脚本看不到页面脚本定义的JavaScript变量更多信息和源

Firefox/Chrome runs your JavaScript code in a parallel environment, isolated from the website's environment. Firefox / Chrome在与网站环境隔离的并行环境中运行JavaScript代码。 This is of course for security reasons. 这当然是出于安全原因。 It also helps for you to be able to develop without knowing the exact inner workings of each website your code might be injected into, as you don't need to bother regarding naming conflicts (which would be impossible if it weren't for the separation environments). 它还有助于您在不知道可能要插入代码的每个网站的确切内部运作的情况下进行开发,因为您无需担心命名冲突(如果不进行分隔,则是不可能的。环境)。 The only thing you do have access to is the DOM, through which you can gather information, alter the website and even talk with the website in some cases (the website should follow a protocol you decide upon as well). 您唯一可以访问的是DOM,在某些情况下,您可以通过DOM来收集信息,更改网站甚至与网站交谈(该网站也应遵循您决定的协议)。

if it's the props object variable that you want exposed, you could return he entire object from the functin, like in closures. 如果这是您要公开的props对象变量,则可以从functin返回整个对象,就像在闭包中一样。 Or , you could, simple declare a the enclosing function as a constructor function, set props as this.props and then, invoke the same function elsewhere, using the new keyword. 或者,您可以简单地将一个封闭函数声明为构造函数,将props设置为this.props,然后使用new关键字在其他地方调用相同的函数。 Let me know if this helps 让我知道这是否有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM