最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Access GreasemonkeyTampermonkey variables from the browser console? - Stack Overflow

programmeradmin0浏览0评论

This is example of code from the userscript:

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

And when I try to call my functions from script: everything is OK, but I can't get access from browser console:

(ReferenceError: ExampleObj is not defined)


My Greasemonkey/Tampermonkey settings (Metadata):

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

The script works; I just need access to those functions from the browser console.

This is example of code from the userscript:

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

And when I try to call my functions from script: everything is OK, but I can't get access from browser console:

(ReferenceError: ExampleObj is not defined)


My Greasemonkey/Tampermonkey settings (Metadata):

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

The script works; I just need access to those functions from the browser console.

Share Improve this question edited Apr 3, 2018 at 17:32 Brock Adams 93.6k23 gold badges241 silver badges305 bronze badges asked Apr 3, 2018 at 15:26 Łukasz RząsaŁukasz Rząsa 1913 silver badges10 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

In @grant none mode, scripts still operate in a protected-ish scope. Place your object in the global scope by changing:

var ExampleObj = {

To:

window.ExampleObj = {

Then you'll be able to see and use that object. (Note that the target web page can also see and use it.)

See "Accessing Variables from Greasemonkey to Page & vice versa" for more information and for scenarios when @grant is not none.

发布评论

评论列表(0)

  1. 暂无评论