Forum Academy Marketplace Showcase Pricing Features

Get row from database in bubble.io using javascript variable

Hi everyone,

Background:
I have a bubble.io based chrome extension that attempts to provide a discount to users based on the website they are visiting/browsing (similar to Honey). I get the url info from chrome and pass it back to bubble. From bubble, I have a plugin (Toolbox) that allows me to run javascript code:

//
window.addEventListener(‘message’, (event) => {
if (event.data.msg) {
// We parse the message and create a JSON variable
var msg = JSON.parse(event.data.msg);

    console.log(msg.hostname);  // I want to use this value to query my bubble.io Discounts table
}

})

In the addEventListener() function, I am able to console.log the url of the current site. I want to be able to retrieve a particular row from my Discounts table based on the value of the url using javascript or the “Insert Dynamic Data” option. Can anyone provide suggestions?

Thanks in advance!

Can you use the dynamic data as a ‘do a search for’ item, with constraint of host name = get data from url or this url?

So that’s the thing. I am not able to use my variable in the constraints field. It has no idea what I am referring to.

Edit: I realized that you meant something else. Getting a item/value using ‘this url’ doesn’t work because bubble doesn’t have access to the main page url in chrome since it is embedded within an extension.

Ah I see now. I didn’t realize when reading.

Good question. I’m not entirely sure. I know @shot has experience with chrome extensions and bubble. Might be worth reaching out

Have you tried using the publish value property of the js to bubble element? If you’re not familiar with it, search for fn_bubble here in the forum and that may turn up some threads that show how to use it.

What I’m thinking is that you could use this to publish the value of your variable and then use that in your constraint.

I thought about this actually. To store the url, I could create a temporary database entry and then delete it after I fetch the discount. But then, I would have to design additional logic to make sure when there are concurrent users, there aren’t any conflicts (generate some unique session id key per user and store the url as the value ). And I feel it wouldn’t scale well as the users grow. I can try this out for now. But there must be some other way?

Thanks, I will try reaching out to him.