I have a simple Chrome extension that opens new Chrome tabs on clicking some URLs.
I was using with success a simple in-line javascript
...
<script type="text/javascript">
function LaunchURL(oURL) {
var launchType = localStorage["LS_LaunchType"];
switch (launchType) {
case "TN":
chrome.tabs.create({ url: oURL });
break;
case "WN":
chrome.windows.create({ url: oURL });
break;
default:
chrome.tabs.create({ url: oURL });
break;
}
}
</script>
...
<a href="javascript:LaunchURL('http://foo.com')">My hyperlink</a>
I want to upgrade manifest.json to version 2, but now the in-line script are blocked for security reasons.
How can I open new tabs?
I have tried to create a separate javascript file with the previous function, but without success.
What you have should work correctly in an external javascript file, but you will need remove the script from the anchor tag. Instead just have the url in the anchor tag and add an event listener in the javascript that prevents the default action.
So in the page you have:
And in external.js you have: