Is there a way to detect that I'm in Pinegrow with JavaScript?

I have some JavaScript running that on my page that I DON’T want to run in Pinegrow (even when JS is enabled). How do I do this?

is there a class or element somewhere that exists only within the Pinegrow interface that I can check against? I’m sure there’s a way to find this out myself so a lil help on how to look inside Pinegrow would be fab! Otherwise, for now, I’m checking if the URL is localhost.

e.g.

if (!document.body.classList.contains('is-pinegrow')) {
  // run the code
}

Hi @fakesamgregory

Assuming your Pinegrow internal server is running at 127.0.0.1 at port 40000, try:

if(location.host === “127.0.0.1:40000) {
   console.log(“I’m within the Pinegrow matrix”)
}

Hey @Akayy,

Thanks for this. Unfortunately, this does not then run when I preview in the browser. I just want it to not run in the Pinegrow interface.

May be location.href? I think this has some url parameters, and in external browser, I think it doesn’t have url params… haven’t checked this yet at my end!

@fakesamgregory and @Akayy,
Yeah, @Akayy is right. location.href in Pinegrow will have have a pgedit URL parameter, while the browser won’t.
Cheers,
Bob

Here you go:

if(location.search.includes('pgedit'))
  console.log('Editing within pinegrow')
1 Like

Great this works!! Thanks both

1 Like

Checking for data-pg-id attributes would also work:

if (!document.body.hasAttribute('data-pg-id') {
  // run the code
}
1 Like