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

问题描述

我正在创建一个网站拦截器:在您访问一个您被阻止的网站后,浏览器会显示一个新的HTML页面,显示网站被拦截。新的HTML页面以 message.html 的形式保存在我的Chrome扩展程序中。有没有什么方法可以在浏览器中显示 message.html ?如果不是,我只会使用内容脚本来注入一些JavaScript。 $ b

假设以下所有条件都成立:


  • 您正在使用在后台环境中运行的脚本执行此操作。

    li>
  • 您需要更新已存在的选项卡以显示 message.html

  • 您想要更新的ID选项卡是 tabId

  • 您的 message.html 位于与清单相同的目录中.json



您可以执行以下操作,它使用()更改包含ID的标签在 tabId 中显示您的 message.html

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

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

如果您在活动窗口中更改当前选定的选项卡,则



创建一个标签来显示 message.html / h2>

假设以下所有情况都是正确的:


  • 脚本运行在后台上下文中。

  • 您需要创建一个新选项卡来显示 message.html

  • message.html 位于与 manifest.json 相同的目录中。



您可以使用()创建一个新选项卡来显示 message.html

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

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



在新窗口中打开 message.html



假设以下所有条件都成立:


  • 您正在使用在后台运行的脚本执行此操作

  • 您需要创建一个新窗口来显示 message.html

  • 您的消息。 html 与您的 manifest.json 位于同一个目录中。



您可以使用()打开一个新窗口来显示 message.html
$ b

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

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


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.

解决方案

Updating a tab to display message.html

Assuming all of the following are true:

  • 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.

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'});

or

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

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

Create a tab to display message.html

Assuming all of the following are true:

  • 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.

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

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

or

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

Open message.html in a new window

Assuming all of the following are true:

  • 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.

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

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

or

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

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

07-25 00:27