Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save disco0/68798129c8673d460cbb7a4bc1517cd3 to your computer and use it in GitHub Desktop.
Save disco0/68798129c8673d460cbb7a4bc1517cd3 to your computer and use it in GitHub Desktop.
#debug_tools #user_script
///<reference types="tampermonkey"/>
// @ts-check
// ==UserScript==
// @name DevTools - GM API on console
// @name:zh-CN 开发工具:在控制台启用 GM API
// @namespace https://github.com/cologler/
// @version 0.1.0.3
// @description Modified fork of original script <gist.github.com/Cologler/323558e1e55d20b73eadc8a5eb6bebde>
// @description:zh-CN try to take over the world!
// @author cologler
// @match http*://*/*
// @grant unsafeWindow
//
// @grant GM_info
// @grant GM_log
// @grant GM_addStyle
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_deleteValue
// @grant GM_addValueChangeListener
// @grant GM_removeValueChangeListener
// @grant GM_listValues
// @grant GM_getResourceText
// @grant GM_getResourceURL
// @grant GM_openInTab
// @grant GM_download
// @grant GM_xmlhttpRequest
// @grant GM_getTab
// @grant GM_saveTab
// @grant GM_getTabs
// @grant GM_notification
// @grant GM_setClipboard
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
//
// (Unused)
// @grant GM.info
// @grant GM.log
// @grant GM.download
// @grant GM.xmlhttpRequest
// @grant GM.getResourceText
// @grant GM.getResourceURL
// @grant GM.getValue
// @grant GM.setValue
// @grant GM.deleteValue
// @grant GM.listValues
// @grant GM.addValueChangeListener
// @grant GM.removeValueChangeListener
// @grant GM.addStyle
// @grant GM.notification
// @grant GM.openInTab
// @grant GM.getTab
// @grant GM.getTabs
// @grant GM.saveTab
// @grant GM.setClipboard
// @grant GM.registerMenuCommand
// @grant GM.unregisterMenuCommand
//
// @noframes
// ==/UserScript==
(function(unsafeWindow)
{
'use strict';
const GM_NS =
/** @type {typeof Tampermonkey} */
( Object.create(null) );
/** @type {PropertyDescriptor} */
const propBase =
{
configurable: false,
set(value) { void 0 }
}
/**
* @type {<
* T extends Record<PropertyKey, any>,
* K extends PropertyKey,
* V extends any
* >(target: T, key: K, value: V) => T & Record<K, V>
* }
*/
const asGetter = (target, key, value) =>
Object.defineProperty(target, key, { ...propBase, get() { return value } })
// Can't seem to get these to show up with @grant GM.* in TM?
/*
const GM_Primordial = GM;
for(const key of Object.keys(GM_Primordial))
{
Object.defineProperty(USERSCRIPT_NS, key,
{
...propBase,
get() { return GM_Primordial[key] }
})
}
*/
const contextWindow = window;
const TM =
{
contextWindow,
GM_info,
GM_log,
GM_deleteValue,
GM_listValues,
GM_setValue,
GM_getValue,
GM_addValueChangeListener,
GM_removeValueChangeListener,
GM_getResourceText,
GM_getResourceURL,
GM_registerMenuCommand,
GM_unregisterMenuCommand,
GM_download,
GM_xmlhttpRequest,
GM_openInTab,
GM_getTab,
GM_saveTab,
GM_getTabs,
GM_addStyle,
GM_notification,
GM_setClipboard,
}
// Should probably move this into a single defineProperties and spread descriptors after
// building them w/ map
for(const key of Object.keys(TM))
asGetter(GM_NS, key, TM[key])
Object.assign(unsafeWindow, { GM: Object.freeze(GM_NS) })
})(unsafeWindow);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment