net-art-and-cultures / data-bodies

GNU General Public License v3.0
1 stars 6 forks source link

loading data (data portrait) from local storage #14

Closed harae37 closed 4 years ago

jwham92 commented 4 years ago

Hi @nbriz @zylemma I'm wondering which one should I work to communicate between background.js and popup.js. Do you have any suggestions? I found a question that has similar situation with task#14. https://discourse.mozilla.org/t/getbackgroundpage-in-private-mode/14046

runtime.connect() https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/connect

runtime.getBackgroundPage() https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/getBackgroundPage

nbriz commented 4 years ago

i think a better answer to ur question can be found here: https://stackoverflow.com/a/20023723/1104148

there's more going on in that example than u need, but take a look at the code they wrote for popuup.js && content.js as an example for how communication between the two can work.

this example is written for chrome specifically, to make it work on all browsers replace every instance of the chrome variable w/the browser variable && then it should work.

try that out && let me know if u have any problems/challenges.

zylemma commented 4 years ago

I'm trying to change the "Run" button to "Stop" when click on it, it seems work with the code but when closing the popup window, the word just goes back to "Run"

`const runButton = document.querySelector('#run') let isRunning = false

runButton.addEventListener('click', function (e) { if (isRunning === false) { runButton.textContent = 'Stop' isRunning = true } else if (isRunning === true) { runButton.textContent = 'Run' isRunning = false } })`

nbriz commented 4 years ago

ah, that makes sense, what might be happening is that the popup "refreshes" everytime it opens/closes. maybe what u could do instead is keep track of the state of the app (whether it's running or not) in the background script && in the popup, have a line of code that sends a message to the background script asking to see what the current state is. that way everytime the popup opens it first checks w/the background script && then set's the button accordingly.

have u had success w/communicating between the background script && popup?

On Thu, Dec 5, 2019 at 11:48 AM YULI ZHAO notifications@github.com wrote:

I'm trying to change the "Run" button to "Stop" when click on it, it seems work with the code but when closing the popup window, the word just goes back to "Run"

`const runButton = document.querySelector('#run') let isRunning = false

runButton.addEventListener('click', function (e) { if (isRunning === false) { runButton.textContent = 'Stop' isRunning = true } else if (isRunning === true) { runButton.textContent = 'Run' isRunning = false } })`

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/net-art-and-cultures/data-bodies/issues/14?email_source=notifications&email_token=AATEANWX5S2FUZI7HD4SIQLQXE5INA5CNFSM4JJJTYD2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEGBRIPI#issuecomment-562238525, or unsubscribe https://github.com/notifications/unsubscribe-auth/AATEANSB5HKKIAK6VK4KQQTQXE5INANCNFSM4JJJTYDQ .

-- ../n!ck

PGP Publick Key https://keybase.io/nickbriz/pgp_keys.asc 7DDF 4128 C8D6 C755 nickbriz.com @nbriz https://twitter.com/nbriz

zylemma commented 4 years ago

ah, that makes sense, what might be happening is that the popup "refreshes" everytime it opens/closes. maybe what u could do instead is keep track of the state of the app (whether it's running or not) in the background script && in the popup, have a line of code that sends a message to the background script asking to see what the current state is. that way everytime the popup opens it first checks w/the background script && then set's the button accordingly. have u had success w/communicating between the background script && popup?

Yes I already figured out how to communicate between background script && popup. Actually I'm a little confused about our files structure. So for now we have the background-script under the content-script folder, and also according to the manifest.json file, this _background-script_is actually a content script. I got big trouble with the naming things when I first started to learn how to communicate between these scripts. Now I figure out how to communicate between background script, content script && popup, so in our case, the one named _background-script_now should work as background script or content script?

nbriz commented 4 years ago

@zylemma right, i realize this is confusing. first, technically speaking it doesn't matter what u name any of ur .js files, as long as their referenced in the right place in the manifest.json.

that said, what we chose to name our files might have caused a bit of confusion, this is b/c our "content script" is called background-script.js... && in addon lingo, "background script" is something different from a "content script". a content script is a js file that gets injected into the web page u're visiting (it runs in the "backgorund" of that website, which i think is why we called our content script background-script.js). a background script is a js file that runs as soon as ur addon is loaded && keeps running as long as ur addon is loaded.

so "content scripts" get injected into specific pages u define (&& only run on those specific tabs) where as a "background script" runs behind the scenes the entire time an addon is on (regardless what pages/tabs u have open). does that make sense? u can read about these different files && the structure of an addon here: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Anatomy_of_a_WebExtension

in our case, we're not actually using any "background scripts", we're just using a "content script", b/c we want to inject code into a web page to track the users's movements on that page. we just happned to call our "content script" background-script.js. if that's confusing, feel free to rename the content script something else other than background-script.js (just make sure to also update the manifest.json w/the new name)

zylemma commented 4 years ago

@nbriz thanks to clarify it. I was wondering if we need "background script" or not. Thus we only need the "content script", I will keep working on it.