0

我正在引入一个使用 的 3rd 方 JavaScript 文件document.write,但是需要对正在编写的内容进行操作 - 最好在它到达页面之前进行操作。我想出的是以下内容:

// Hijack document.write to buffer all output...
var dwrite = document.write;
var hijacked = '';
document.write = function(content) {
  hijacked += content;
};
// Call the script...
dwrite("<script type='text/javascript' src='http://www.example.com/file.js'></script>");
// Manipulate the output...
hijacked
  .replace(/a/gi, '4')
  .replace(/e/gi, '3')
  .replace(/i/gi, '1')
  .replace(/o/gi, '0');
// Write the output into the page...
dwrite(hijacked);
// Restore document.write and free our buffer...
document.write = dwrite;
hijacked = null;

有了这个,无论我试图调用什么,我都会得到 NS_ERROR_XPC_BAD_CONVERT_JS dwrite。任何人都可以就为什么会发生这种情况提出建议吗?我不明白为什么通过不同的名称调用 document.write 会爆炸。

更新我在 Firefox 4.0.1 中看到了这一点。


4

1 回答 1

1

我试过了,它奏效了。基本上我在使用后替换了document.write

document.write(""
  + "<script>"
  + "var hijacked = '';"
  + "var dw = document.write;"
  + "document.write = function(content) { hijacked += content; }"
  + "<" + "/script>"

  + "<script type='text/javascript' src='test.js'><" + "/script>"

  + "<script>"
  + "document.write = dw;"
  + "dw = null;"
  + "document.write(hijacked.replace(/e/gi, '4'));"
  + "<" + "/script>");
于 2011-06-07T15:00:23.483 回答