Request help to make a simple generic plugin that loads a Javascript onto every page in the site

Hello there, community!

So, the title really says it all. I need to make a plugin, that allows to me load a specific .js file into the template, and run it on every page that the plugin is enabled on. I decided to try and build a generic plugin after looking at several other different plugins that, amongst other things, loads a .js file.

Now, i build up the plugin in a fairly staight forward manner, and with all the proper files (locale files, index, plugin.inc.php file, version file, and a folder containing my js file). The interesting part is the plugin.inc.php file where I build in the functionality as it’s done in the other plugins I’ve looked into. The new added functionality in the plugin.inc.php file is my problem. I have the getName and getDescription functions aswell, and they work seeing as name and description is displayed properly on the plugin-page. The new added stuff however, does not work properly.

I have a register function as follows:

function register($category, $path) {
$success = parent::register($category, $path);
if ($success && $this->getEnabled()) {
HookRegistry::register(‘TemplateManager::display’, array($this, ‘callback’));
}
return $success;
}

and the callback function is as follows:

function callback($hookName, $args) {
$templateMgr = TemplateManager::getManager();
$templateMgr->addJavaScript(
‘cookiePolicy’,
$request->getBaseUrl() . ‘/’ . $this->getPluginPath() . ‘/js/cookiePolicy.js’
);
}

Now, this does nothing at all. I get no php errors, nor do i get the script onto the site, nothing just happens. The plugin is enabled, and if i do something weird in the php code, i do get an error that crashes the plugin-page, so I know the plugin is enabled and executed properly. But nothing else happens.

I was hoping someone could point me in the right direction. All i really need is the .js file loaded and executed (which i assume would happen if the file was properly read) on the site that the plugin is enabled on.

Does anyone know how to fix that? :slight_smile:

Kind regards,
Jesper.

Hi @jesperlauridsen,

I literally just wrote a plugin to do this on Friday :slight_smile:

It’s not packaged up yet for release, as it needs testing. But if you’re using a current OJS release, put its code into plugins/generic/customHeader, then use lib/pkp/tools/installPluginVersion.php to install it into the database.

It hasn’t been tested with many OJS releases, but should work with 3.1.0-1.

Thanks,
Alec Smecher
Public Knowledge Project Team

Hi @asmecher,

I packed it up into a tar.gz file, and uploaded it through the interface - no errors so far, and seems to activate fine aswell. However, I cant really find any place to modify or select what to change. I thought a “settings” or “configure” option would present itself when i expand the plugin on the plugin page, but all I see is upgrade and delete. Could you clarify how I can modify it? :slight_smile:

Edit: With a little modification to the .inc.php file, I got it to read and execute a javascript file i packed with the plugin. So that kinda fixes my problem - but im still curious if there actually is a “admin” interface where you configure your custom header.

Kind regards,
Jesper.

Hi @asmecher
Does the plugin allow any js codes to be placed in any pages e.g. Additional content?

Hi @jesperlauridsen,

Hmm, there should be a settings form for that plugin. Are you working on the site-wide plugins area (for a multi-journal site), or the journal-specific one?

Regards,
Alec Smecher
Public Knowledge Project Team

Hi @asmecher,

I’m working on ste site-wide plugins area at the moment, and that’s where I tested the plugin. I did not test for a journal-specific one. I took plugin and modified it a little to cover my exact needs - thank you very much for your help.

But i’m quite sure the plugin you’re currently building and testing will be very useful for people setting up their servers and need to customize them a little more.

On a slightly different subject - is there a way to make a plugin “undisableable” - so only a site administrator can enable/disable it, and everyone else cant? I can see some plugins have the checkbox greyed out, and cant be disabled. Is this done in the settings of the plugin, and if yes, how? Or should i simply just make another topic for this question?

Kind regards,
Jesper.

Hi @jesperlauridsen,

Ah, that explains it – the current state of the plugin doesn’t permit it to work site-wide. I’ve noted this on the implementation issue; it shouldn’t be much work to get it working in both contexts. Watch for an update on that issue.

Regards,
Alec Smecher
Public Knowledge Project Team