有一个第三方javascript被注入,在构建特定路径的逻辑之后创建一个新Image(),但不将其追加到文档中。
(function(){
{....}
new Image().src = "some_image_path";
})();问题是,我希望能够得到生成的"some_image_path“。我不想改变它什么的,只要得到它的价值。我不能控制那个第三方JS,它是不能改变的。我试过使用cujojs/meld,但是它要么什么也不做,要么出现了“非法调用”错误。
我是否可以通过观察Image/HTMLImageElement obj或将onload属性自动添加到我定义的函数中来获得src值?
发布于 2015-12-04 16:02:51
您可以尝试使用自定义的Image构造函数在闭包中加载第三方脚本。
(function() {
var img;
var Image = function Image() {
return img = new (Function.prototype.bind.apply(window.Image, arguments));
}
// This is where the 3rd party code should be injected.
// I'm assuming that since you're including it on your page, you know its source and trust it enough to `eval` it.
// You won't be able to use a `<script>` tag to load it since that would execute in the global scope.
// You'll likely need to use AJAX to fetch the script contents and pass to eval here.
eval("new Image().src = some_image_path");
return img;
})().src == some_image_path;编辑
您可以重写全局Image (然后友好地处理它,然后再修复它)。因为脚本标记是按顺序计算的,所以这应该有效。
<script>
var img;
var oldImage = Image;
Image = function Image() {
return img = new (Function.prototype.bind.apply(oldImage, arguments));
}
</script>
<script src="..."></script>
<script>
// img now contains the instantiated `Image` object
img.src; // do what you will with this
// Clean up
Image = oldImage;
</script>https://stackoverflow.com/questions/34092189
复制相似问题