Fritz box name in call list

Show name from dastelefonbuch.de in fritz box caller list. Set your local area code in the source

  1. // ==UserScript==
  2. // @name Fritz box name in call list
  3. // @name:DE Fritz box Name in der Anruferliste
  4. // @namespace https://greatest.deepsurf.us/en/users/20068-cuzi
  5. // @version 1.2
  6. // @description Show name from dastelefonbuch.de in fritz box caller list. Set your local area code in the source
  7. // @description:DE Zeigt den Anrufernamen von dastelefonbuch.de in the Anruferliste. Ändere deine Vorwahl im Sourcecode
  8. // @author cvzi
  9. // @license MIT
  10. // @match http://192.168.178.1/*
  11. // @match https://192.168.178.1/*
  12. // @match https://fritz.box/*
  13. // @match http://fritz.box/*
  14. // @icon http://192.168.178.1/icon.png
  15. // @grant GM.xmlHttpRequest
  16. // @connect dastelefonbuch.de
  17. // ==/UserScript==
  18.  
  19. /* global GM */
  20. /* jshint asi: true, esversion: 8 */
  21.  
  22. (function () {
  23. 'use strict'
  24.  
  25. const areaCode = '06241' // Set to your local area code or empty string '' if you don't use one
  26.  
  27. const dastelefonbuchUrl = 'https://www.dastelefonbuch.de/R%C3%BCckw%C3%A4rts-Suche/'
  28.  
  29. function getCallerName (number) {
  30. return new Promise(function (resolve, reject) {
  31. const cached = window.localStorage.getItem(number)
  32. if (cached) {
  33. if (cached !== 'NOT_FOUND') {
  34. return resolve(cached)
  35. } else {
  36. return reject(new Error(cached + '+FROM_CACHE'))
  37. }
  38. }
  39.  
  40. const url = dastelefonbuchUrl + encodeURIComponent(number)
  41.  
  42. GM.xmlHttpRequest({
  43. method: 'GET',
  44. url,
  45. onerror: function (response) {
  46. console.error(`Error xmlHttpRequest "${url}"`, response)
  47. reject(new Error('ONERROR:xmlHttpRequest'))
  48. },
  49. onload: function (response) {
  50. const parts = response.responseText.split('<div class="vcard">')
  51. if (parts.length < 2) {
  52. console.debug('No results for number ' + number)
  53. window.localStorage.setItem(number, 'NOT_FOUND')
  54. return reject(new Error('NOT_FOUND'))
  55. }
  56. let s = parts[1]
  57. if (s.indexOf('</address>')) {
  58. s = s.split('</address>')[0] + '</address></div></div>'
  59. } else {
  60. s = s.split('</div>')[0] + '</div></div>'
  61. }
  62.  
  63. s = s.replace(/<a[^>]+>/gim, '<span style="color:#888">').replace('</a>', '</span>')
  64.  
  65. window.localStorage.setItem(number, s)
  66.  
  67. resolve(s)
  68. }
  69. })
  70. })
  71. }
  72.  
  73. function getCallerNameTryAreaCode (number, displayResult) {
  74. getCallerName(number).then(function (html) {
  75. displayResult(html)
  76. }).catch(function () {
  77. if (areaCode && !number.startsWith('0') && !number.startsWith('+')) {
  78. getCallerName(`${areaCode}${number}`).then(function (html) {
  79. displayResult(html)
  80. }).catch(function () {
  81. // no name found even with area code
  82. })
  83. } else {
  84. // no name found
  85. }
  86. })
  87. }
  88.  
  89. function addNameToUnknownCalls () {
  90. // Full caller list
  91. document.querySelectorAll('#uiCalls tr a[href*="number="]').forEach(function (a) {
  92. if ('asked' in a.dataset) {
  93. return
  94. }
  95. a.dataset.asked = 1
  96.  
  97. const m = a.href.match(/number=(\d+)/)
  98. if (!m) {
  99. return
  100. }
  101. const number = m[1]
  102.  
  103. const displayResult = function (html) {
  104. a.parentNode.parentNode.parentNode.getElementsByTagName('td')[2].innerHTML += html
  105. }
  106.  
  107. getCallerNameTryAreaCode(number, displayResult)
  108. })
  109.  
  110. // Small caller list on home page
  111. document.querySelectorAll('.info-table--phonecalls .grid-row--phonecall>div:first-of-type').forEach(function (div) {
  112. if ('asked' in div.dataset) {
  113. return
  114. }
  115. div.dataset.asked = 1
  116.  
  117. let m = div.textContent.trim().match(/^(\d+)$/)
  118. if (!m) {
  119. // Active calls:
  120. m = div.textContent.trim().match(/ — (\d+)/)
  121. if (!m) {
  122. return
  123. }
  124. }
  125. const number = m[1]
  126.  
  127. const displayResult = function (html) {
  128. div.innerHTML += html
  129. }
  130. getCallerNameTryAreaCode(number, displayResult)
  131. })
  132. }
  133.  
  134. window.setInterval(addNameToUnknownCalls, 1000)
  135. })()