gpt4 book ai didi

javascript - 从浏览器控制台访问 Greasemonkey/Tampermonkey 变量?

转载 作者:行者123 更新时间:2023-11-29 20:55:12 29 4
gpt4 key购买 nike

这是来自用户脚本的代码示例:

var ExampleObj = {
somevar1:'value1',
somevar2:'value2',
somevar3:'value3',
somefunction1:function(){
//do sth
},
somefunction2:function(){
//do sth else
}
}

当我尝试从脚本中调用我的函数时:一切正常,但是我无法从浏览器控制台访问:

(ReferenceError: ExampleObj is not defined)


我的 Greasemonkey/Tampermonkey 设置(元数据):

// ==UserScript==
// @name [this is my secret]
// @version 1
// @run-at document-end
// @include [this is my secret]
// @grant none
// ==/UserScript==

脚本有效;我只需要从浏览器控制台访问这些功能。

最佳答案

@grant none 模式下,脚本仍然在 protected 范围内运行。通过更改将您的对象置于全局范围内:

var ExampleObj = {

收件人:

window.ExampleObj = {

然后您将能够看到并使用该对象。 (注意目标网页也可以看到和使用它。)

请参阅“Accessing Variables from Greasemonkey to Page & vice versa”了解更多信息以及 @grant 不为 none 的情况。

关于javascript - 从浏览器控制台访问 Greasemonkey/Tampermonkey 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49633780/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com