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>
2

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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..:)
Well, I was trying to add more features and now I have been stuck on this problem- stackoverflow.com/questions/25584927/… Please take a look at it and see if u can help.
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>

1 Comment

Nothing bad will happen. Chrome automatically creates the <body> element and wraps the script and the button in it.
0

You can try with this,

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

This will work in Microsoft EDGE browser also.

3 Comments

It may work in Edge, but that is beside the point: this won't work in a Chrome extension.
It must be work in Chrome, IE, Edge, Firefox and Maxthon also.
Working in a chrome extension is different than working in chrome. Extensions explicitly disallow onclick. See the documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.