Need help to fix my first (and very simple) plugin

Hey everyone, i want to make a very simple plugin who search if a text contains at least one word from a list of text.

It’s an invisible element with this

With this code, who will run when text_to_check is changed (maybe i misunderstand ?)

But when i test it’s look like the update fonction not run because nb_test doesn’t increase whereas text_to_check and words_to_search are good, see

it’s maybe because initialize function is empty but i don’t know what i should put here because i already initialize the states with their own function, see

Any idea ?

It doesn’t matter if the initialize function is empty. One thing I see is that you didn’t set your initial var correctly in update (var splited_text = …) Same for nb_test and contains_words_searched. So there’s a few thing in your script syntax to review. You can use a tool like jshint.com that may help you.
Also, you can check console log in browser to see any error. I guess that actually, the plugin doesn’t load because the script is not correct.

Well, there’s a typo in line 5 (.lenght means to be .length of course).

Also, properties.words_to_search is not array of texts, it is a List object as described in the documentation (click show documentation link above your Function update text entry area).

You must transform properties.words_to_search into an actual array using the .get() function.

Here’s a generic function for turning List-type properties objects into arrays of whatever type they are (it gets the entire list):

instance.data.funcGetList = function getList(List) {
    var returnList = []
    if (List.length() < 1) return returnList 
    returnList = List.get(0, List.length())
    return returnList
  } // end getList
2 Likes

In terms of the actual logic of your function, BTW, you don’t need those nested for loops. The following one-liner will return true if any of the words in array words is found in the string str:

words.some(word => str.includes(word))

Use it like so:

  // our long string of text:
  var str = "Hello world, welcome to the universe." 
  //  our array of words we wish to check for in our long string of text:
  var words = ['nothing', 'taco', 'welcome']
  var is_word_found = words.some(word => str.includes(word))
  // is_word_found is true. publish that value to your desired state

If instead words is:

var words = ['nothing', 'taco']
var is_word_found = words.some(word => str.includes(word))
// is_word_found is false
1 Like