Chrome Extension: How to convert a local HTML file into a URL to display?
up vote
1
down vote
favorite
I am currently developing a chrome extension, and am attempting to make one of my local HTML files into a URL so I can open it from my content script. One solution I found was to use:
chrome.tabs.create({url: chrome.extension.getURL('notes.html')});
This did not work though. Some people have reported that content scripts may not work with all of the chrome extension API. I need this function to work from my content script though, to make sure it will fire when I need it to. I also found:
var urlChanged = window.url.createObjectURL("notes.html");
window.open(urlChanged);
This did not work either. I ended with trying:
var urlChanged = chrome.runtime.getURL("notes.html");
window.open(urlChanged);
A new tab opens, but I only get a blank HTML page. I was wondering if anyone can give me some insight as to why none of these methods may be working?
tl;dr My content script does not want to open and display a local HTML file I made. I used multiple methods to try and open it, but the file does not want to open from the content script. The HTML file is in the same extension folder as the content script and manifest.json. Any help with this would be appreciated!
javascript url google-chrome-extension
add a comment |
up vote
1
down vote
favorite
I am currently developing a chrome extension, and am attempting to make one of my local HTML files into a URL so I can open it from my content script. One solution I found was to use:
chrome.tabs.create({url: chrome.extension.getURL('notes.html')});
This did not work though. Some people have reported that content scripts may not work with all of the chrome extension API. I need this function to work from my content script though, to make sure it will fire when I need it to. I also found:
var urlChanged = window.url.createObjectURL("notes.html");
window.open(urlChanged);
This did not work either. I ended with trying:
var urlChanged = chrome.runtime.getURL("notes.html");
window.open(urlChanged);
A new tab opens, but I only get a blank HTML page. I was wondering if anyone can give me some insight as to why none of these methods may be working?
tl;dr My content script does not want to open and display a local HTML file I made. I used multiple methods to try and open it, but the file does not want to open from the content script. The HTML file is in the same extension folder as the content script and manifest.json. Any help with this would be appreciated!
javascript url google-chrome-extension
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am currently developing a chrome extension, and am attempting to make one of my local HTML files into a URL so I can open it from my content script. One solution I found was to use:
chrome.tabs.create({url: chrome.extension.getURL('notes.html')});
This did not work though. Some people have reported that content scripts may not work with all of the chrome extension API. I need this function to work from my content script though, to make sure it will fire when I need it to. I also found:
var urlChanged = window.url.createObjectURL("notes.html");
window.open(urlChanged);
This did not work either. I ended with trying:
var urlChanged = chrome.runtime.getURL("notes.html");
window.open(urlChanged);
A new tab opens, but I only get a blank HTML page. I was wondering if anyone can give me some insight as to why none of these methods may be working?
tl;dr My content script does not want to open and display a local HTML file I made. I used multiple methods to try and open it, but the file does not want to open from the content script. The HTML file is in the same extension folder as the content script and manifest.json. Any help with this would be appreciated!
javascript url google-chrome-extension
I am currently developing a chrome extension, and am attempting to make one of my local HTML files into a URL so I can open it from my content script. One solution I found was to use:
chrome.tabs.create({url: chrome.extension.getURL('notes.html')});
This did not work though. Some people have reported that content scripts may not work with all of the chrome extension API. I need this function to work from my content script though, to make sure it will fire when I need it to. I also found:
var urlChanged = window.url.createObjectURL("notes.html");
window.open(urlChanged);
This did not work either. I ended with trying:
var urlChanged = chrome.runtime.getURL("notes.html");
window.open(urlChanged);
A new tab opens, but I only get a blank HTML page. I was wondering if anyone can give me some insight as to why none of these methods may be working?
tl;dr My content script does not want to open and display a local HTML file I made. I used multiple methods to try and open it, but the file does not want to open from the content script. The HTML file is in the same extension folder as the content script and manifest.json. Any help with this would be appreciated!
javascript url google-chrome-extension
javascript url google-chrome-extension
asked Nov 21 at 18:12
Augusto Celis
84
84
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
To open your extension's page via window.open() in a content script you'll have to expose it using web_accessible_resources in manifest.json. It'll make your extension detectable from web though.
"web_accessible_resources": [
"notes.html"
]
A better solution is to send a message from the content script to the background script, which can use chrome.tabs API to open your extension page.
It's a better solution because window.open from a web page tab may be blocked by the anti-popup policy. And also because you don't expose your extension's pages to the web.
manifest.json should declare the background script:
"background": {
"scripts": ["background.js"],
"persistent": false
}
content script simply sends a message:
chrome.runtime.sendMessage({action: 'openNotes'});
background.js does the work:
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
switch (msg.action) {
case 'openNotes':
chrome.tabs.create({
url: '/notes.html',
active: true,
index: sender.tab.index + 1,
openerTabId: sender.tab.id,
});
return;
}
});
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
To open your extension's page via window.open() in a content script you'll have to expose it using web_accessible_resources in manifest.json. It'll make your extension detectable from web though.
"web_accessible_resources": [
"notes.html"
]
A better solution is to send a message from the content script to the background script, which can use chrome.tabs API to open your extension page.
It's a better solution because window.open from a web page tab may be blocked by the anti-popup policy. And also because you don't expose your extension's pages to the web.
manifest.json should declare the background script:
"background": {
"scripts": ["background.js"],
"persistent": false
}
content script simply sends a message:
chrome.runtime.sendMessage({action: 'openNotes'});
background.js does the work:
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
switch (msg.action) {
case 'openNotes':
chrome.tabs.create({
url: '/notes.html',
active: true,
index: sender.tab.index + 1,
openerTabId: sender.tab.id,
});
return;
}
});
add a comment |
up vote
1
down vote
accepted
To open your extension's page via window.open() in a content script you'll have to expose it using web_accessible_resources in manifest.json. It'll make your extension detectable from web though.
"web_accessible_resources": [
"notes.html"
]
A better solution is to send a message from the content script to the background script, which can use chrome.tabs API to open your extension page.
It's a better solution because window.open from a web page tab may be blocked by the anti-popup policy. And also because you don't expose your extension's pages to the web.
manifest.json should declare the background script:
"background": {
"scripts": ["background.js"],
"persistent": false
}
content script simply sends a message:
chrome.runtime.sendMessage({action: 'openNotes'});
background.js does the work:
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
switch (msg.action) {
case 'openNotes':
chrome.tabs.create({
url: '/notes.html',
active: true,
index: sender.tab.index + 1,
openerTabId: sender.tab.id,
});
return;
}
});
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
To open your extension's page via window.open() in a content script you'll have to expose it using web_accessible_resources in manifest.json. It'll make your extension detectable from web though.
"web_accessible_resources": [
"notes.html"
]
A better solution is to send a message from the content script to the background script, which can use chrome.tabs API to open your extension page.
It's a better solution because window.open from a web page tab may be blocked by the anti-popup policy. And also because you don't expose your extension's pages to the web.
manifest.json should declare the background script:
"background": {
"scripts": ["background.js"],
"persistent": false
}
content script simply sends a message:
chrome.runtime.sendMessage({action: 'openNotes'});
background.js does the work:
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
switch (msg.action) {
case 'openNotes':
chrome.tabs.create({
url: '/notes.html',
active: true,
index: sender.tab.index + 1,
openerTabId: sender.tab.id,
});
return;
}
});
To open your extension's page via window.open() in a content script you'll have to expose it using web_accessible_resources in manifest.json. It'll make your extension detectable from web though.
"web_accessible_resources": [
"notes.html"
]
A better solution is to send a message from the content script to the background script, which can use chrome.tabs API to open your extension page.
It's a better solution because window.open from a web page tab may be blocked by the anti-popup policy. And also because you don't expose your extension's pages to the web.
manifest.json should declare the background script:
"background": {
"scripts": ["background.js"],
"persistent": false
}
content script simply sends a message:
chrome.runtime.sendMessage({action: 'openNotes'});
background.js does the work:
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
switch (msg.action) {
case 'openNotes':
chrome.tabs.create({
url: '/notes.html',
active: true,
index: sender.tab.index + 1,
openerTabId: sender.tab.id,
});
return;
}
});
answered Nov 21 at 18:38
wOxxOm
25.8k34461
25.8k34461
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418218%2fchrome-extension-how-to-convert-a-local-html-file-into-a-url-to-display%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown