Include Custom Javascript

Hey,

I’m trying to include a custom JavaScript file for one specific site (the Omeka instance has multiple sites). For now I have site x using the default theme; I used the way that themes work to include a custom JavaScript solution. However this option is not durable. An update to the themes or Omeka S would render this ‘hackish solution’ obsolete. The JavaScript code changes the appearance and target of URI’s when viewing an item publicaly. This is the code:

$(document).ready(function(){
	var uris = document.getElementsByClassName("uri-value-link");
	for (var x = 0; x < uris.length; x++){
		uris[x].text = "🔗 "+uris[x].text;
		uris[x].setAttribute("target","_blank");
		uris[x].style.color = "rgb(211,56,0)";
		uris[x].style.textDecoration = "underline";
		//console.log(uris[x]);
	}
});

I put it in a separate file called include.js. This file is stored in /themes/default/asset/js. I added it to the head of my item-pages by adding: $this->headScript()->prependFile($this->assetUrl('js/include.js')); // include custom script to /themes/default/view/layout.phtml.

There are some obvious problems with this pseudo-solution:

  1. Every site using the Default theme that has the same class on a page will be affected, whereas I want a more granular control (i.e. only affect one site of the entire Omeka instance)
  2. Updates to the theme will render the changes in themes/default/view/layout.phtml back to the default. I.e. After every style-update someone needs to make that change. I only work on the project as a temporary resource, so it has to be an easier solution than this (especially if one day the way themes work get a big redesign)

With all this said and done, my question is simple, but my search for a answer has been without results:
Is there a durable way to include custom JavaScript solutions for a specific site in Omeka S?

1 Like

This sounds like something a distinct module could handle. The module would let you set site settings for whether to include the javascript. That’s a little bit of site-specific configuration, but is the most straightforward way, I think. Then, the module would use one of the events to insert the javascript.

The Sharing module, which optionally inserts javascript for sharing on social media, is probably a good model, especially it’s viewShow method.

2 Likes

Thanks @patrickmj for pointing out a strategy. I’m completely new to the Zend-Framework, so it’s quite a daunting project.

I noticed that the Sharing module is not configured via /admin/modules/ but via admin/site/s/sitename/settings; though I don’t fully understand how it works (i.e. there are no comments in the code etc…). When combining it with the documentation I’ve gotten completely confused. You happen to know what section of the documentation is the most relevant to the method used in the Sharing module?

Probably looking at the Events reference will give some guidance about how the site settings and the javascript insertion work. That should show the structure of adding Events in Module.php’s attachListers() method. The controller in those structures specifies that it is triggered from sitename/settings, not modules, to allow it to run only in specific sites.

1 Like