Skip to content

Instantly share code, notes, and snippets.

@soundyogi
Last active August 21, 2019 11:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save soundyogi/e28c343f062edfa5285b7590dba76755 to your computer and use it in GitHub Desktop.
Save soundyogi/e28c343f062edfa5285b7590dba76755 to your computer and use it in GitHub Desktop.
Require your devtools snippets in your devtools snippets.
window.__modules__ = {}
window.require = require
var modules = window.__modules__
Promise
.resolve()
.then(get_snippets)
.then(deserialize)
.then(function(snippets){
snippets.map(function(module){define(module.name, module.content)})
})
function require(id) {
if(!modules[id]) throw new Error(id + ' module not yet defined')
if(typeof modules[id] === "string") return modules[id] = eval(modules[id])
return modules[id]
}
function define(id, string) {
if(modules[id]) throw new Error(id + ' module or property already exists')
return modules[id] = string
}
function deserialize(string){
if(typeof string !== "string") throw Error("deserialize needs a string")
if(string === "") throw Error("no snippets present")
return JSON.parse(string)
}
function get_snippets(){
var deferred = Promise.defer()
InspectorFrontendHost.getPreferences(function(prefs){
deferred.resolve(prefs.scriptSnippets)
})
return deferred.promise
}
// derp snippets require inspired by
// https://gist.github.com/gordonbrander/3861744
function require(id){if(!modules[id])throw Error(id+" module not yet defined");return"string"==typeof modules[id]?modules[id]=eval(modules[id]):modules[id]}function define(e,r){if(modules[e])throw Error(e+" module or property already exists");return modules[e]=r}function deserialize(e){if("string"!=typeof e)throw Error("deserialize needs a string");if(""===e)throw Error("no snippets present");return JSON.parse(e)}function get_snippets(){var e=Promise.defer();return InspectorFrontendHost.getPreferences(function(r){e.resolve(r.scriptSnippets)}),e.promise}window.__modules__={},window.require=require;var modules=window.__modules__;Promise.resolve().then(get_snippets).then(deserialize).then(function(e){e.map(function(e){define(e.name,e.content)})});
function test(msg, f){
console.log(msg)
f({
ok: (expr, msg) => expr ? console.log("pass: "+msg) : console.log("fail: "+msg)
})
}
test("should be true", t => {
t.ok(true, "istrue")
})
// devtools export
;({
test
})
var test = require("harness") //this is the name of the snippet
@soundyogi
Copy link
Author

Important:

Open Inspector (ctrl+shift+I)
Undock Inspector in Seperate Window
Inspect Inspector (ctrl+shift+i)
Load the require script.

Last Value in a snippet will be the "export"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment