Last active
August 21, 2019 11:42
-
-
Save soundyogi/e28c343f062edfa5285b7590dba76755 to your computer and use it in GitHub Desktop.
Require your devtools snippets in your devtools snippets.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)})}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var test = require("harness") //this is the name of the snippet |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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"