I just needed to do the same, tried out ractoon’s solution but it didn’t work. I managed then with the ACF method which i found on further investigation: https://www.advancedcustomfields.com/resources/adding-custom-javascript-fields/
Hope this helps somebody else.