Scan text for words before publishing
Asked by Jonathan Jacobi:

I wanted to write a plugin that'd be very helpful to me,

the idea is before publishing an entry for the confluence, the plugin will search for certain words in the article (the words will be in a given list), and if it does find some words that are both in the article and in the list, it will ask the user if he wants to add these words as labels to the article.

Is that possible? if so, how do I approach the problem?

I have written a question but I think I haven't made myself clear enough, so re-posting in a much more understandable way...

BTW, I am using confluence 5.4.3 :)

Thanks in advance ! :-)


Answers:
author: Robert Reiner _smartics_
body: You could listen in your plugin to PageEvents.

[Event Listener Module](https://developer.atlassian.com/confdev/confluence-plugin-guide/confluence-plugin-module-types/event-listener-module), [Annotation Based Event Listener Example](https://developer.atlassian.com/confdev/confluence-plugin-guide/confluence-plugin-module-types/event-listener-module/annotation-based-event-listener-example), and [Writing an Event Listener Plugin Module](https://developer.atlassian.com/confdev/confluence-plugin-guide/confluence-plugin-module-types/event-listener-module/writing-an-event-listener-plugin-module) provide an introduction on how to write a plugin for Confluence Server.

comments: author: Jonathan Jacobi
body: Sorry for the ignorance, but I wanted to ask, how can I add labels using a plugin? :)


author: Robert Reiner _smartics_
body: No problem: You need to inject the com.atlassian.confluence.labels.LabelManager and use its methods to add labels to com.atlassian.confluence.labels.Labelable (e.g. pages).

This might be helpful: [Accessing Confluence Components from Plugin Modules](https://developer.atlassian.com/confdev/confluence-plugin-guide/writing-confluence-plugins/accessing-confluence-components-from-plugin-modules)


author: Jonathan Jacobi
body: and accessing the post that is going to be published is also from the Confluence Components?  

Thanks a lot Robert, you're the best :)!!!!!


author: Jonathan Jacobi
body: I assume it is some kind of an event that occurs whenever a user tries to post an "article", but is accessing the text possible as well as adding a label?  
(I tried finding the event and wasn't successful, beside:

"AbstractCreateAndEditPageAction" {#toc-hId-1762112606}
-------------------------------------------------------

but I'm not sure this is the correct one

)  
Thanks :D


author: Robert Reiner _smartics_
body: You may start with com.atlassian.confluence.event.events.content.ContentEvent as in:

```
 @EventListener
 public void onEvent(final ContentEvent event) {
  ...
 }
```

to check which events will be thrown. Probably the PageUpdateEvent (which is a subclasses of the ContentEvent) will do. The events have references to the page from which you can access the body content.


author: Robert Reiner _smartics_
body: Labels have their own events: com.atlassian.confluence.event.events.label.LabelEvent  
(e.g. com.atlassian.confluence.event.events.label.LabelAddEvent).


author: Jonathan Jacobi
body: I need the plugin to check the content of the publishment for certain words, if they do exist, to suggest the user to add them as labels, isn't a label event not used here?  
Or am I getting it wrong?  
Thanks a lot!


author: Robert Reiner _smartics_
body: Sorry, I'm completely wrong! :(

I thought you need to add labels automatically dependent on the content. This could be done on the save/update event of the page. I completely missed the fact to ask the user for consent. Sorry!

Now that I understand your use case I think you need to run another approach where you implement the query in the front end (I suppose that is where you have already been headed before I lured you on the wrong path). Unfortunately I have very limited knowledge on how to do that. I suppose you need to use the [REST API](https://developer.atlassian.com/confdev/confluence-server-rest-api) if the JavaScript API does not provide access to all information you require for your use case.

I found A[dding a Custom Action to Confluence](https://developer.atlassian.com/confdev/tutorials/adding-a-custom-action-to-confluence) , but this tutorial only shows how a label can be set on a web action (no user interaction and not bound to the save event). I'm not sure if you'd better search for hooks in the editor (TinyMCE). Maybe this could be a start: [Enabling TinyMCE Plugins](https://developer.atlassian.com/confdev/confluence-plugin-guide/writing-confluence-plugins/enabling-tinymce-plugins)?


author: Robert Reiner _smartics_
body: To inspect web elements (in order to find the hook in the frontend) the [Web Fragment Finder](https://marketplace.atlassian.com/plugins/com.wittified.webfragment-finder/server/overview) by [Wittified](https://marketplace.atlassian.com/vendors/1210682/wittified-atlassian-add-ons-an-appfire-company) may be very helpful.


author: Jonathan Jacobi
body: Thanks!!!!


author: Jonathan Jacobi
body: Also, if it were to be automatically add labels dependant on the content-  

How would I approach it?  
Writing an event listener module for a plugin?  
Using the " PageUpdateEvent " Event?

Thank you!

Edit:

I've started to read deeply more about the Event Listener modules, and I see that java code is needed (src: <https://developer.atlassian.com/confdev/confluence-plugin-guide/confluence-plugin-module-types/event-listener-module?_ga=2.228386526.23600538.1505396919-511758074.1501773387>),

where is the java file supposed to be kept?

also, I'm not sure that the required java packages are installed (for example, the ones that are used here : <https://developer.atlassian.com/confdev/confluence-plugin-guide/confluence-plugin-module-types/event-listener-module/annotation-based-event-listener-example> )...

Isn't there any other way to implement the plugin module? other than in java?

Thanks


author: Robert Reiner _smartics_
body: The Java code is required to be bundled in a JAR as an plugin (aka add-on, which is lately called an "App"). All dependencies are either already deployed with the Confluence Server or they can be added to the OBR bundle (which contains the JAR I mentioned above plus dependencies). So you would need the [Atlassian Plugin SDK](https://developer.atlassian.com/docs/getting-started/set-up-the-atlassian-plugin-sdk-and-build-a-project) to build your plugin/add-on/app.

If you do not want to use Java, you could go with JavaScript in the front end (I do not know if [Atlassian Connect](https://developer.atlassian.com/static/connect/docs/latest/index.html) is already running on server). But again I think you need to create a plugin to install on Confluence Server. The plugin will contain your JavaScript files that are applied with the atlassian-plugin.xml (e.g. web-fragment).

I do not think that there is an solution that can be directly configured to the server (such as [User Macros](https://confluence.atlassian.com/doc/writing-user-macros-4485.html)). Maybe there are third-party plugins that allow such a configuration, but I don't know such a plugin.


Tags: app

confluence-app