J Benjamin
J Benjamin

Reputation: 4782

Check remote URL for javascript snippet

My application interacts with my users' websites via a javascript snippet that they paste into their site....like Google Analytics, Stripe, AppInsights...etc.

I'd like for my web app to be able to ping the remote site, to verify the pasted snippet exists. If it does exists, I'd like the remote javascript method to respond.

I feel like this should be easy but I'm having trouble putting it all together. For the remote script, I made it so it checked the querystring for a trigger value...if found, it would execute a 'self check' and respond.

I wasn't quite sure how to make the script respond to my ajax call and I also didn't necessarily like the idea of checking the querystring every window.load

Can anyone point me in the right direction or suggest a more elegant approach? Thanks

Upvotes: 0

Views: 172

Answers (1)

Hirad Nikoo
Hirad Nikoo

Reputation: 1629

I think you have to think it all the other way around. How these kinds of scripts are working is that they have an API key generated just for that website and when the website is loaded it is the JavaScript code snipped that does send the API key to the API generated server to show that the snippet is available and gives the information about the page that it is loaded in.

This way you don't need to check if the snipped is available in a website. Of course if you need consistent dialog with the snipped you have to use Web Socket or other ways of communications (for the older browsers like long polling and ...)

For sure not using Web Socket and two-way communication gives the trust to the user that you are not doing any malicious work underneath and the first approach should help you enough. But if you persist on using the two-way communication please see MDN WebSocket documentation .

The first approach (not using two-way communication) is just an ajax call you can do it with JQuery: jQuery AJAX documentation, or do not use any library just pure JavaScript which is John Shipp on Vanilla JS AJAX requests .

Upvotes: 1

Related Questions