4

I was making a Chrome extension, for which I have an html file, a JavaScript file which opens a modified link in a new tab, the manifest file and the icon.

It works fine but now I want the javascript function to work only when the user clicks a button. So I made a button in the html file, put the js code inside a function and called the function using onclick.

But for some reason, it is not working. On clicking the button nothing seems to happen. I have tried reloading the extension. Also, I took a working example of a simple program in which on clicking the button, a simple "Hello world" message is displayed using alert().

This works fine when I open the html page directly in chrome but when I replaced this with the function that I made, nothing seems to happen on clicking.

Can someone please find the bug/problem?

The urltry.html file is:

<!DOCTYPE html>
<html>
<button onclick="editorial()">View Editorial</button>
<script>
function editorial()
{
chrome.tabs.query({currentWindow: true, active: true}, function(tabs){
    var tab_url=tabs[0].url;
    var new_url=tab_url.slice(11);
    chrome.tabs.create({ url:"http://www.discuss." + new_url});        
});
}
</script>
</html>
Oleg
  • 8,818
  • 2
  • 40
  • 55
Naman Sancheti
  • 294
  • 7
  • 17
  • I'm pretty sure Chrome Extensions don't allow inline event handlers. I suggest reading https://developer.chrome.com/extensions/tut_migration_to_manifest_v2#inline_scripts – Oleg Aug 30 '14 at 09:26
  • possible duplicate of [onClick within Chrome Extension not working](http://stackoverflow.com/questions/13591983/onclick-within-chrome-extension-not-working) – Xan Sep 01 '14 at 09:45

3 Answers3

4

Due to the default Content Security Policy (CSP) in Google Chrome extensions, the following is disallowed:

  • Eval and related functions
  • Inline JavaScript

The suggestion, as provided by Google Chrome Extensions documentation on SCP is to place the code to a separate file and use proper binding to click event from JavaScript. See below.

Your HTML file:

<!DOCTYPE html>
<html>
<head>
  <script src="editorial.js"></script>
</head>
<body>
  <button id="viewEditorial">View Editorial</button>
</body>
</html>

Your JavaScript file, editorial.js

function editorial() {
  chrome.tabs.query({currentWindow: true, active: true}, function(tabs){
    var tab_url=tabs[0].url;
    var new_url=tab_url.slice(11);
    chrome.tabs.create({ url:"http://www.discuss." + new_url});        
  });
}

document.addEventListener('DOMContentLoaded', function () {
  var btn = document.getElementById('viewEditorial');
  if (btn) {
    btn.addEventListener('click', editorial);
  }
});

Note: don't forget that you need to declare "tabs" permission to be able to modify the URL. See the tabs documentation.

Oleg
  • 8,818
  • 2
  • 40
  • 55
  • Thanks a lot for taking time out and helping me solve this. I wasn't aware of the CSP as I am new to Chrome extensions. Your solution was well-written and it did work..:) – Naman Sancheti Aug 30 '14 at 15:42
  • Well, I was trying to add more features and now I have been stuck on this problem- http://stackoverflow.com/questions/25584927/issue-opening-new-tab-in-multiple-functions-in-javascript-for-chrome-extension Please take a look at it and see if u can help. – Naman Sancheti Aug 31 '14 at 04:42
1

You must put your button inside the body tag, otherwise many bad things can happen and probably the browser goes in the quirks mode.

Solution:

<!DOCTYPE html>
<html>
<head>
<script>
function editorial()
{
chrome.tabs.query({currentWindow: true, active: true}, function(tabs){
    var tab_url=tabs[0].url;
    var new_url=tab_url.slice(11);
    chrome.tabs.create({ url:"http://www.discuss." + new_url});        
});
}
</script>
</head>
<body>
   <button onclick="editorial()">View Editorial</button>
</body>
</html>
Adam M.
  • 969
  • 1
  • 6
  • 22
  • Nothing bad will happen. Chrome automatically creates the `` element and wraps the script and the button in it. – Oleg Aug 30 '14 at 09:30
0

You can try with this,

<body>
  <button onclick="javascript:editorial()">View Editorial</button>
</body>

This will work in Microsoft EDGE browser also.

Hardik Leuwa
  • 1,946
  • 2
  • 10
  • 25
  • It may work in Edge, but that is beside the point: this won't work in a Chrome extension. – Teepeemm Jan 08 '17 at 04:28
  • It must be work in Chrome, IE, Edge, Firefox and Maxthon also. – Hardik Leuwa Jan 09 '17 at 07:17
  • Working in a chrome *extension* is different than working in chrome. Extensions explicitly disallow `onclick`. See [the documentation](https://developer.chrome.com/extensions/contentSecurityPolicy#JSExecution). – Teepeemm Jan 09 '17 at 15:26