2

The use case of my extension is when the user opens up the Gmail, fire an API call and fetch the list of topics(the topics can be more than 500). Then when the user opens up the message, if from that fetched topics any max 6 words match with the word in the opened message, it should be highlighted.

For example

If the fetched topic has following topics

topics: {
  hello: 10,
  how: 1,
  mandy: 12,
  google: 90,
  extension: 13,
  local: 20
}

And I have a Gmail message as following

hello, my name is mandy. I work at google. 

then the word hello, mandy and google should get highlighted because, those word from the message matched with the topics object.

For now, I could fetch the topic object and pass that topic object to the function searchPage but does not know the way to match.

Here is what I have done till now

function fetchTopics() {
    const apiUrl = `https://baseurl.ngrok.io/useful_topics`;
    chrome.storage.sync.get(['user_token'], function(result) {
        const decoded = JSON.parse(atob(result.user_token));
        const access_token = decoded['access-token'];
        const headers = new Headers();
        headers.append('Access-Token', access_token);
        fetch(apiUrl, {
            method: 'GET',
            headers
        })
            .then(function(response) {
                return response.json();
            })
            .then(function(data) {
                searchPage(data);
                // for (var key in data) {
                //  if (data.hasOwnProperty(key)) {
                //      searchPage(key);
                //  }
                // }
            });
    });
}


function searchPage(topicObj) {
  console.log('topicObj', topicObj);
  // read all the words in the currently opened gmail messages and highlight any max 
  // 6 words in the message if there is a match 
}

I will have topicObj in following format(hashmap structure)

topics: {
  hostname: 4,
  cto: 19,
  maharjan: 45,
  aws: 382,
  axosoft: 66,
  its: 26,
  repo: 15,
  anurag: 585,
  unsubscribe: 65,
  bitbucket: 313,
  having: 28,
  devops: 414,
}
Satendra
  • 6,755
  • 4
  • 26
  • 46
milan
  • 2,409
  • 2
  • 34
  • 71

1 Answers1

1

you can split the message text into an array and then compare the array entries with your list of keywords using a loop.

var array = msg.split(" ");

*this is how to split your msg text into an array

var i;
for (i = 0; i < array.length; i++) {

  var x;
  for (i = 0; x < keys.length; i++) {
    if(array[i] == keys[x])
     //match
   } 

  } 

*this is how to do the comparison

Me1o
  • 1,629
  • 1
  • 6
  • 8
  • maybe check the DOM to see if there is any class or id for the message body.. `var text = document.getElementById('msgBody').textContent;` – Me1o Jul 16 '18 at 07:06
  • 1
    Is treewalker efficient enough for crawling the message? – milan Jul 16 '18 at 07:13
  • it is actually.. check the first comment here: https://stackoverflow.com/questions/2579666/getelementsbytagname-equivalent-for-textnodes – Me1o Jul 16 '18 at 07:18
  • treewalker crawls whole document not just the currently opened message, i guess. – milan Jul 16 '18 at 07:51
  • 'user opens up the message' at the point, the document is the message div plus other divs.. but it should be detectable.. – Me1o Jul 16 '18 at 07:58