@@ -231,16 +231,80 @@ plugin.{PluginName}.{messageKey}
231231 └── language_es.php
232232```
233233
234- ## 9.8 Complete plugin example with CSS and translations
234+ ## 9.8 Plugin JavaScript
235235
236- See the ` EnhancedExample ` plugin for a complete working example demonstrating both features:
236+ Plugins can provide pre-compiled JavaScript files that will be automatically injected into both frontend and admin pages.
237+
238+ ### 9.8.1 Adding JavaScript to your plugin
239+
240+ Implement the ` getScripts() ` method in your plugin class:
241+
242+ ``` php
243+ public function getScripts(): array
244+ {
245+ return [
246+ 'assets/script.js', // Frontend script
247+ 'assets/admin-script.js' // Admin-specific script
248+ ];
249+ }
250+ ```
251+
252+ ** Important notes:**
253+ - Paths are relative to your plugin directory
254+ - Provide ** pre-compiled JavaScript files** only (not TypeScript source)
255+ - JavaScript files are loaded before core scripts
256+ - Scripts are automatically injected at the end of ` <body> `
257+ - Works in both frontend and admin areas
258+
259+ ### 9.8.2 Plugin directory structure for JavaScript
260+
261+ ```
262+ /content/plugins/YourPlugin/
263+ ├── YourPluginPlugin.php
264+ └── assets/
265+ ├── script.js
266+ └── admin-script.js
267+ ```
268+
269+ ### 9.8.3 JavaScript best practices
270+
271+ ** Example frontend script:**
272+ ``` javascript
273+ (function () {
274+ ' use strict' ;
275+
276+ // Wait for DOM to be ready
277+ if (document .readyState === ' loading' ) {
278+ document .addEventListener (' DOMContentLoaded' , init);
279+ } else {
280+ init ();
281+ }
282+
283+ function init () {
284+ console .log (' My Plugin: Loaded' );
285+ // Your plugin functionality here
286+ }
287+ })();
288+ ```
289+
290+ ** Security notes:**
291+ - Always use strict mode (` 'use strict' ` )
292+ - Wrap code in IIFE to avoid global namespace pollution
293+ - Validate and sanitize user input
294+ - Use DOM API safely
295+
296+ ## 9.9 Complete plugin example with CSS, JavaScript, and translations
297+
298+ See the ` EnhancedExample ` plugin for a complete working example demonstrating all features:
237299
238300```
239301/content/plugins/EnhancedExample/
240302├── EnhancedExamplePlugin.php
241303├── assets/
242304│ ├── style.css
243- │ └── admin-style.css
305+ │ ├── admin-style.css
306+ │ ├── script.js
307+ │ └── admin-script.js
244308└── translations/
245309 ├── language_en.php
246310 ├── language_de.php
@@ -256,7 +320,7 @@ See the `EnhancedExample` plugin for a complete working example demonstrating bo
256320<p>{{ 'plugin.EnhancedExample.adminMessage' | translate }}</p>
257321```
258322
259- ## 9.9 Plugin version history
323+ ## 9.10 Plugin version history
260324
261325- 0.1.0: Initial version, shipped with phpMyFAQ 4.0.0
262- - 0.2.0: Added support for plugin configuration options, plugin stylesheets and translations, shipped with phpMyFAQ 4.1.0
326+ - 0.2.0: Added support for plugin configuration, stylesheets, JavaScript, and translations, shipped with phpMyFAQ 4.1.0
0 commit comments