Append query parameter to module's javascript based on custom field on page I am new to Drupal development.

I am currently working on maintaining a Drupal module that was developed by a past colleague.

What I want to do is have an available field where a user can enter in an integer which corresponds to a JS query parameter that will be added to one of the module's script upon page load. 

I am wondering if it is possible to add a custom field type that users can add through the Drupal UI to their content (nodes), and then check for the existence of this field and append it to the script?

I have found this tutorial helpful in adding custom fields through my module. However, it doesn't provide any links to documentation that specifies what values I can/should put on dependencies, module, along with other parameters. Also after quite a bit of Google searching, and a bit of combing through Drupal docs, I ended up with nothing.

Further, I believe that I am more or less on the right track in using the hook_page_attachments_alter function to check against fields and conditionally load JS scripts. Although, I don't believe that the $page array passed through this hook supplies node fields. 

Currently the module is loading the custom JS like so:

  function my_module_page_attachments_alter(&amp;$page) {

    $page['#attached']['library'][] = 'my_module/script';

  }


Obviously it is loading it through libraries, which would make appending a custom query string parameter difficult. Maybe there is a better way?
You probably want to approach this through hook_ENTITY_TYPE_view(). It's easy enough to get the current node in the page attachments hook, but as you only need to take action when a node is being viewed, using the more specific hook seems appropriate.

A basic example:

function my_module_node_view(array &amp;$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
  if ($view_mode == 'full' &amp;&amp; $entity-&gt;getType() == 'foo' &amp;&amp; $entity-&gt;hasField('field_bar')) {
    if ( ($val = $entity-&gt;field_bar-&gt;value) ) {
      $build['#attached']['library'][] = 'my_module/script';
      $build['#attached']['drupalSettings']['my_module']['script']['bar'] = $val;
    }
  }
}


The settings you add to the #attached array will be available in JS after a change to your libraries yml file to add a dependency:

script:
  js:
    ...
  dependencies:
    ...
    - core/drupalSettings


The value will be available in the JS var drupalSettings.my_module.script.bar.