0

I have a file.js to perform some calculation,

file.js

 (function () {
  var x =1; 
  var y = //value from dom
  z = x +y;
  })()

I run it in popup.js via

popup.js

function excute() {
    chrome.tabs.executeScript({
        file: 'file.js'
    });
}
excute();

popup.html

<html>
<head>
    <title>title</title>
  </head>
<body>

  <script type="text/javascript" src="popup.js"></script>

      <input type="text" id="info"> 

  </body>
</html>

When I click on the extension the code in the file.js is executed on the DOM and get the y value! and the z variable is calculated properly!

But my issue

  • I can't get and show variable z from file.js in a control at <input type="text" id="info"> in popup.html
  • I can't get the value of <input type="text" id="info"> in popup.html and set it to the variable z in file.js
Mohamed
  • 806
  • 13
  • 30
  • 1
    [chrome.tabs.executeScript(): How to get result of content script?](//stackoverflow.com/a/41578299) – wOxxOm Oct 07 '17 at 17:03
  • thanks for your prompt reply but the code in file.js is very long, I just add the above as example! so I don't know How I can return variable from file.js and show it in popup.html – Mohamed Oct 07 '17 at 17:08
  • 1
    The returned value is the last evaluated expression so if file.js is synchronous code, simply return the result from your IIFE (`return z` in your function + executeScript callback as shown in the linked answer). – wOxxOm Oct 07 '17 at 17:10
  • indeed, I am new in Google extensions, and I have 4 calculated variables in file.js and need to show all in popup.html, if you could help me with code sample for this scenario I would be fully appreciated – Mohamed Oct 07 '17 at 17:15
  • 1
    Simply return an array [x, y, z] or an object {x: 'foo', y: 'bar'} – wOxxOm Oct 07 '17 at 17:20
  • @wOxxOm Thank you so much for your valuable reply it guides me in the right direction, additional bit thing please, could you guide me how to get value from popup.html and use it in file.js! by the way please add your instruction at answer section to accept it :) – Mohamed Oct 08 '17 at 09:12
  • Use messaging or chrome.storage – wOxxOm Oct 08 '17 at 11:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156202/discussion-between-m-qassas-and-woxxom). – Mohamed Oct 08 '17 at 11:31

0 Answers0