6

Is there any way use Tampermonkey's API in Chrome's JavaScript console?

I want to mess with functions like GM_xmlhttpRequest() and GM_listValues().

Tim B
  • 40,716
  • 16
  • 83
  • 128
Jake
  • 2,106
  • 1
  • 24
  • 23

1 Answers1

8
  1. Create the following script:

    // ==UserScript==
    // @name       Exports some GM functions
    // @namespace  Whatever
    // @match      *
    // ==/UserScript==
    // WARNING: Any page can now use these methods! Be careful,
    // or change "@match *" to "@match https://example.com" and
    // visit https://example.com to test the API.
    unsafeWindow.GM_xmlhttpRequest = GM_xmlhttpRequest;
    unsafeWindow.GM_listValues = GM_listValues;
    
  2. Open any page where Tampermonkey can be activated ( https://stackoverflow.com/ for instance).

  3. Open the Developer tools -> Console.
  4. Done, you can now do whatever you want with the exported methods (from step 1).

Screenshot of console

Community
  • 1
  • 1
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • I dont have chrome-extension://dhd... in the context dropdown box. I only have chrome-extension://gig... there for the user script I suppose. Also the functions don't appear in my console's window object. – Jake Dec 27 '12 at 18:57
  • @Jake, make sure the Tampermonkey script runs on the page in question, then close that tab and open the page in a fresh one. Then the Tampermonkey context will appear. Also, only the functions you export will appear. – Brock Adams Dec 27 '12 at 18:58
  • @Jake Visit `chrome://extensions/` to find out the extensionID of the extension you're looking for (Tampermonkey). After creating the user script, you have to reload the page. If the console was already open, close and re-open the console. These steps will always work. – Rob W Dec 27 '12 at 18:59
  • Do you know how to do this in the latest version? I can't find any page context option! Changing from `` to `Tampermonkey` doesn't work either - it says `GM_x is not defined`. – ᔕᖺᘎᕊ Jan 18 '15 at 19:42
  • 1
    @ṧнʊß You can use `unsafeWindow`. Apparently Tampermonkey now runs in the context of the page instead of the extension – Rob W Jan 18 '15 at 23:18