{jpm} 如何替换自己的页面?

{jpm} How to replace any page to own?

如何将 http://google.com/ 等任何页面替换为我的页面(我不想重定向),包括 <head>

我试过这个:

// Import the page-mod API
var pageMod = require("sdk/page-mod");

// Create a page-mod
// It will run a script whenever a ".io" URL is loaded
// The script replaces the page contents with a message
pageMod.PageMod({
  include: "*.io",
  contentScript: 'document.body.innerHTML = ' +
                 ' "<h1>Page matches ruleset</h1>";'
});

但这不会替换整个页面。我也想替换 head 。我想在页面加载前替换页面...

使用document.head.innerHTML您可以参考html页面的页眉,允许在其中设置内容。

还值得注意的是,页面-mod API 可以占用 multiple arguments as an array of string literals,并且能够从数据目录导入脚本。

最后,您可以在页面加载前将脚本设置为 运行,方法是将 page-mod Option contentScriptWhen 设置为 start 或 [=14] =].

根据文档:

"start": Load content scripts immediately after the document element is inserted into the DOM, but before the DOM content itself has been loaded

"ready": Load content scripts once DOM content has been loaded, corresponding to the DOMContentLoaded event

"end": Load content scripts once all the content (DOM, JS, CSS, images) has been loaded, at the time the window.onload event fires

因此,您在加载页面之前同时更改页眉和正文的示例类似于以下内容:

// Import the page-mod API
var pageMod = require("sdk/page-mod");

// Create a page-mod
// It will run a script whenever a ".org" URL is loaded
// The script replaces the page contents with a message
pageMod.PageMod({
  include: "*.io",
  contentScript: ['document.body.innerHTML = ' +
                 ' "<h1>foo</h1>"',
                 'document.head.innerHTML = ' + 
                 ' "<style>h1 {color:blue;}</style>";'],
  contentScriptWhen: "start"
});

还值得注意的是,当 contentScriptWhen 设置为 ready 时,此脚本可能对某些页面效果更好,否则更改可能不适用于页面上的所有元素。