Created
July 6, 2019 07:36
-
-
Save zckevin/6a62fb8e15273070553a00b0c4db487d to your computer and use it in GitHub Desktop.
Generate prepack input for xxx.
This file contains 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 m = {} | |
Object.getOwnPropertyNames(window).filter(key => { | |
let s = window[key] && window[key].toString() | |
return !key.startsWith("_$") && s && s.indexOf("native code") != -1 | |
}).map(key => { | |
let fn = window[key] | |
let ownKeys = [], protoKeys = [] | |
if (fn) { | |
ownKeys = Object.getOwnPropertyNames(fn).filter(s => { | |
if (s === "caller" || s === "callee" || s === "arguments" || s === "constructor") return false | |
let result | |
try { | |
result = typeof fn[s] === "function" && !s.startsWith("__") | |
} catch(err) { | |
result = false | |
} | |
return result | |
}) | |
} | |
if (fn.prototype) { | |
protoKeys = Object.getOwnPropertyNames(fn.prototype).filter(s => { | |
if (s === "caller" || s === "callee" || s === "arguments" || s === "constructor") return false | |
let result | |
try { | |
result = typeof fn.prototype[s] === "function" && !s.startsWith("__") | |
} catch(err) { | |
result = false | |
} | |
return result | |
}) | |
} | |
m[key] = fn | |
ownKeys.map(s => { | |
m[`${key}.${s}`] = fn[s] | |
}) | |
protoKeys.map(s => { | |
m[`${key}.prototype.${s}`] = fn.prototype[s] | |
}) | |
}) | |
Object.getOwnPropertyNames(Math).map(key => { | |
m[`Math.${key}`] = Math[key] | |
}) |
This file contains 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 fixNativeMapping = { | |
open: "XMLHttpRequest.prototype.open", | |
submit: "HTMLFormElement.prototype.submit", | |
Request: "Request", | |
fetch: "fetch", | |
clearInterval: "clearInterval" | |
}; | |
Object.entries(window) | |
.filter(e => e[0].startsWith("_$")) | |
.map(e => { | |
let [key, val] = e; | |
// 1. undefined values | |
if (!val) { | |
// 1.1 empty string | |
if (typeof val === "string" && val.length === 0) { | |
return `var ${key} = ""`; | |
} | |
return `var ${key} = ${val}`; | |
} | |
let s = val.toString(); | |
// 2. object values | |
if (s.startsWith("[object")) { | |
if (val === window) { | |
return `var ${key} = window`; | |
} else if (val === document) { | |
return `var ${key} = document`; | |
} else if (val === JSON) { | |
return `var ${key} = JSON`; | |
} else if (val === Math) { | |
return `var ${key} = Math`; | |
} else { | |
let result = {}; | |
Object.entries(val).map(e => { | |
let [k, v] = e; | |
if (!v || typeof v !== "function") { | |
result[k] = v; | |
return; | |
} | |
result[k] = v.toString(); | |
}); | |
return `var ${key} = ${JSON.stringify(result)}`; | |
} | |
} | |
if (typeof val === "function") { | |
let fn = val; | |
let result; | |
// 3. native functions | |
if (s.indexOf("native code") !== -1) { | |
let found = false; | |
for (let k in m) { | |
if (m[k] === fn) { | |
result = k; | |
found = true; | |
break; | |
} | |
} | |
if (!found) { | |
let fixName = window[key].name; | |
result = fixNativeMapping[fixName]; | |
} | |
} else { | |
// 4. user defined functions | |
result = fn.toString(); | |
} | |
return `var ${key} = ${result}`; | |
} | |
// 5. POD values | |
// TODO: Location | |
return `var ${key} = ${JSON.stringify(val)}`; | |
}) | |
.join("\n"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment