显示扩展中包含的 HTML 文件 [英] Show HTML file contained within the extension

查看:12
本文介绍了显示扩展中包含的 HTML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建网站拦截器:在您访问已拦截的网站后,浏览器会显示一个新的 HTML 页面,上面写着网站已拦截".新的 HTML 页面在我的 Chrome 扩展程序中保存为 message.html.有没有办法在浏览器中显示 message.html ?如果没有,我将只使用内容脚本来注入一些 JavaScript.

I'm creating a website blocker: after you visit a website you've blocked, the browser displays a new HTML page saying "website blocked". The new HTML page is saved in my Chrome extension as message.html. Is there any way to display message.html in the browser? If not, I'll just use a content script to inject some JavaScript.

推荐答案

更新选项卡以显示 message.html

假设以下所有情况都为真:

Updating a tab to display message.html

Assuming all of the following are true:

  • 您是通过在后台上下文中运行的脚本执行此操作的.
  • 您想要更新现有标签以显示 message.html
  • 您要更新的 ID 选项卡是 tabId.
  • 您的 message.html 与您的 manifest.json 位于同一目录中.
  • You are doing this from a script running in the background context.
  • You are wanting to update an already existing tab to display message.html
  • The ID tab which you are wanting to update is tabId.
  • Your message.html is located in the same directory as your manifest.json.

您可以执行以下操作,它使用 chrome.tabs.update() (Firefox docs) 更改包含在 tabId 中的 ID 的选项卡以显示您的 message.html:

You could do the following, which uses chrome.tabs.update() (Firefox docs) to change the tab with the ID contained in tabId to display your message.html:

chrome.tabs.update(tabId ,{url:'/message.html'});

chrome.tabs.update(tabId ,{url:chrome.runtime.getURL('/message.html'}));

如果您要更改活动窗口中当前选定的选项卡,则不需要 tabId,您可以省略该参数.

If you are changing the currently selected tab in the active window, then the tabId is not required, and you can omit that argument.

假设以下所有情况都为真:

Assuming all of the following are true:

  • 您是通过在后台上下文中运行的脚本执行此操作的.
  • 您想要创建一个新标签来显示 message.html
  • 您的 message.html 与您的 manifest.json 位于同一目录中.
  • You are doing this from a script running in the background context.
  • You are wanting to create a new tab to display message.html
  • Your message.html is located in the same directory as your manifest.json.

您可以使用 chrome.tabs.create() (Firefox 文档) 创建一个新标签来显示 message.html:

You can use chrome.tabs.create() (Firefox docs) to create a new tab to display message.html:

chrome.tabs.create({url:'/message.html'});

chrome.tabs.create({url:chrome.runtime.getURL('/message.html'}));

在新窗口中打开message.html

假设以下所有情况都为真:

Open message.html in a new window

Assuming all of the following are true:

  • 您是通过在后台上下文中运行的脚本执行此操作的.
  • 您想要创建一个新窗口来显示 message.html
  • 您的 message.html 与您的 manifest.json 位于同一目录中.
  • You are doing this from a script running in the background context.
  • You are wanting to create a new window to display message.html
  • Your message.html is located in the same directory as your manifest.json.

您可以使用 chrome.windows.create()(Firefox 文档)打开一个新窗口以显示message.html:

You can use chrome.windows.create() (Firefox docs) to open a new window to display message.html:

chrome.windows.create({url:'/message.html'});

chrome.windows.create({url:chrome.runtime.getURL('/message.html'}));

这篇关于显示扩展中包含的 HTML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆