Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
125 views
in Technique[技术] by (71.8m points)

javascript - Show HTML file contained within the extension

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...