1

Where do I put my main code, in background.html,or popup.html,or eventPage.js or other?

Some info on my Extension : My extension works to calculate the percentage of marks for our college semester results. We have our college results site and it doesn't display the percentage of total marks obtained.I'd like to build an extension for that.

1)I used 'Page Action' in my manifest.json file, since it is only on a specific site, my extension works. 2)The popup.html has the UI to display the percentage.

But what about background.html, eventPage.js and other pages. I think I also need contentScript.js since I'm accessing the result's page's html.

Where do I put my JavaScript code( I wrote JS code to obtain values from the table and calculate % ) ??

Any brief Idea or outline about which pages I need and what code should I put into them would we highly useful to me. I'm dumbstruck on this work and can't find any way out.

Looking for some help and guidance. Thanks in advance!

Nikhil
  • 6,493
  • 10
  • 31
  • 68

2 Answers2

2

Your chrome extension folder generally consists of these and possibly more of the:

1) manifest.json (The required file by Chrome to initialize your extension)

2) popup.html (Or any other name. This is the page that will open up when you click your extension. Remember, you are not allowed to have any eval or inline-event-handler statements as per the Chrome CSP (Content Security Policy)

3) icon.png (This is the icon of your extension that you see in the top right extensions area)

4) script.js (This is where you put the logic of the calculations. You then have to include it in your popup.html file as follows: <script src="script.js"></script>)

5) Other scripts (If needed. All these need to be included in the popup.html as I described above)

You should read-up on this: https://developer.chrome.com/extensions/getstarted

sshashank124
  • 31,495
  • 9
  • 67
  • 76
  • I've done that.as u said, I included script.js in popup.html. But my doubt is the script runs on popup.html only. How can and what can I do to run it on current page. – Nikhil Mar 17 '14 at 08:50
  • @nikhil, I'm sorry, I do not understand what you mean by "current page"? Could you explain? – sshashank124 Mar 17 '14 at 08:52
  • Ya,sure. When user is on the URL(www.example.com/results),and if he clicks on my extension and he should gets his percentage.So,what I meant by current page is "the page the user is on when he clicked the extension".My question is are we linking the 'script' to the page the user is on? I think the script only works on popup.html.Correct me if I'm wrong. – Nikhil Mar 17 '14 at 10:11
  • @nikhil, You are right to think so. However, you can get the current page by chrome.tabs as follows: http://stackoverflow.com/questions/6871309/google-chrome-extension-get-page-information Also, if you need to get content from a certain webpage you can use XMLHttpRequests and send requests to that webpage. – sshashank124 Mar 17 '14 at 10:27
0

You have to add content script to your manifest.json

"content_scripts": [ { "matches": ["http://*/*","https://*/*"],
"js": ["mycalculator.js"] } ]

(In this mycalculator.js file must contain your java script that reads the values and calculates the percentage.) also put the file in your extensions folder.

The js file will be automatically included in your loaded page.
For more details : content scripts

Syam Kumar S
  • 832
  • 2
  • 8
  • 26