From 0ac9f4d4aeda811946f402fb13e2d301557c7c6b Mon Sep 17 00:00:00 2001
From: Daniel Triendl <daniel@pew.cc>
Date: Mon, 2 Nov 2020 15:34:34 +0100
Subject: [PATCH] Improve caller id

---
 3CX_TAPI.user.js | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/3CX_TAPI.user.js b/3CX_TAPI.user.js
index 3d177b8..0c6421d 100644
--- a/3CX_TAPI.user.js
+++ b/3CX_TAPI.user.js
@@ -3,7 +3,7 @@
 // @author       Daniel Triendl
 // @namespace    http://cp-solutions.at
 // @copyright    Copyright 2020 CP Solutions GmbH
-// @version      3
+// @version      4
 // @grant        GM.xmlHttpRequest
 // @grant        GM.notification
 // @include      https://192.168.0.154:5001/webclient*
@@ -231,15 +231,26 @@ const tapi = {
   },
 
   showCallNotification: (element) => {
-    var number = element.textContent;
-    var rx = /\+([0-9]+)/
+    var number = element.dataset.id;
+    console.log('TAPI call notification', number);
+    var rx = /(\+?[0-9]+)/;
     var match = rx.exec(number);
-    number = '00' + match[1];
+    if (!match) {
+      console.log('TAPI callerid no number found');
+      return;
+    }
+
+    number = match[1];
+
+    if (number.startsWith('+')) {
+      number = number.replace('+', '00');
+    }
+    console.log('TAPI searching callerid for', number);
     GM.xmlHttpRequest({
       method: 'GET',
       url: 'http://cpatapi.cpsrvweb2016.cp-austria.at/callerid/' + encodeURIComponent(number),
       onload: function (response) {
-        console.log('TAPI Search response', response);
+        console.log('TAPI callerid response', response);
         var callerId = JSON.parse(response.responseText);
         var notification = {
           text: number
@@ -254,4 +265,4 @@ const tapi = {
 };
 
 waitForKeyElements('div.nav-search', tapi.createSearchBox, true);
-waitForKeyElements('.toasterName', tapi.showCallNotification, false);
+waitForKeyElements('call-view', tapi.showCallNotification, false);