I was wondering if it is possible to modify <script>
tags on the fly with the Shopify API?
Scenario:
A page renders some <script>
tags.
Before the <script>
get rendered, my app adds an attribute to them, so that the tag is rendered with the attribute.
Thanks in advance
2
Answers
No. Your script is taken from Shopify with a GET request where it is inserted into the Liquid rendering pipeline for eventual inclusion in the client’s HTML payload. If you want that script to do custom stuff, have it do a callback with an App Proxy which would then make it dynamic.
Read this to learn a lot: https://help.shopify.com/api/sdks/shopify-apps/modifying-online-store/use-javascript-responsibly
A Shopify App with the
write_themes
scope (see Shopify Documentation) can access and edit Assets and the Theme. Your editing of the Theme could include the modification of a<script>
tag to as your put it “add an attribute”.Take into consideration whether the Theme designer hard coded the URL of the script or used liquid tags such as
{{ 'example.js' | asset_url | script_tag }}
to create the asset URL.