*/ class action_plugin_jstweak extends ActionPlugin { const PLUGIN_PATH = DOKU_INC . 'lib/plugins/'; const OVERRIDE_PATH = DOKU_CONF . 'plugins.script.local.js.d/'; const SCRIPT_NAME = 'script.js'; /** @inheritDoc */ public function register(EventHandler $controller) { $controller->register_hook('JS_SCRIPT_LIST', 'AFTER', $this, 'handleJsScriptList'); } /** * Event handler for JS_SCRIPT_LIST * * For any plugin's script.js found in the list, it checks for existence * of a local override script.js in the subdirectory * plugins.script.local.js.d// * of the conf directory. * * @see https://www.dokuwiki.org/devel:events:JS_SCRIPT_LIST * @param Event $event Event object, $event->data holds the list * @param mixed $param optional parameter passed when event was registered * @return void */ public function handleJsScriptList(Event $event, $param) { $pluginPathLen = strlen(self::PLUGIN_PATH); $scriptNameLen = strlen(self::SCRIPT_NAME); foreach ($event->data as $key => $entry) { // if we have a plugin's script.js file... if (str_starts_with($entry, self::PLUGIN_PATH) && str_ends_with($entry, self::SCRIPT_NAME)) { // ... extract the plugin's name, ... $pluginName = substr( $entry, $pluginPathLen, strlen($entry) - $pluginPathLen - $scriptNameLen - 1 ); // ... check if there's a local replacement file ... $newEntry = self::OVERRIDE_PATH . $pluginName . '/' . self::SCRIPT_NAME; if (file_exists($newEntry)) { // ... and if yes: change the script list. $event->data[$key] = $newEntry; Logger::debug("jstweak: new script.js for plugin $pluginName -> $newEntry"); } } } } }