how to add custom script in shopify without using script tag

815 Views Asked by At

like HT Script application( https://apps.shopify.com/ht-script )。you can add custom script like

"var a=1";

and we can see custom script in html header like

<script>var a=1;</script>

i know Shopify API support script tag to add custom script like

<script src="http://xxxx.js"></script>

so,what's the principle behind HT Sciprt application,why it don't need src 。

1

There are 1 best solutions below

2
On

App has following permissions: enter image description here

That means it uses following access scopes

  • read_themes
  • write_themes

Way how it probably works

  1. Reads list of themes

    GET /admin/api/2021-01/themes.json

  2. With that list it can find active theme.

    Field "role" equals "main".

  3. With active theme id it can access assets.

    GET /admin/api/2021-01/themes/{theme_id}/assets.json?asset[key]=layout/theme.liquid

  4. With content of that file you can modify headers as you want with html parser.

  5. Update the same file with another api call

    PUT /admin/api/2021-01/themes/{theme_id}/assets.json

Of course you can create some cron job in your application to make sure that your modified scripts are always up to date with template.

Helpful links: