<commit_before>"use strict";

let prefs = {
  favIconColor : "#0099FF"
};
let newEventsTimer, remindersTimer;
let unreadEmailsCount = 0;
let visibleRemindersCount = 0;
let chatNotificationsCount = 0;
const initialDocumentTitle = document.title;
const initialOwaIcon = getCurrentFavIcon();
const owaIcon = createIconElement();

browser.storage.onChanged.addListener(onPrefChanged);

getPrefsAndStart();

function createIconElement() {
  let icon = document.createElement("link");
  icon.rel = "icon";
  icon.type = "image/png";
  icon.sizes = "16x16";
  icon.href = generateTabIcon(0);
  return icon;
}

function getCurrentFavIcon() {
  let icons = document.head.querySelectorAll("link[rel*=icon]");
  let icon;
  if (icons.length > 0) {
    icon = icons[icons.length - 1];
  } else {
    icon = createIconElement();
  }
  return icon;
}

function generateTabIcon(number) {
  const canvas = document.createElement("canvas");
  canvas.width = 16;
  canvas.height = 16;
  const ctx = canvas.getContext("2d");
  // draw cliped circle (radius is bigger than canvas by 1px) - the effect is a
  // square with round corners
  ctx.fillStyle = prefs.favIconColor;
  ctx.beginPath();
  ctx.arc(8, 8, 9, 0, 2 * Math.PI);
  ctx.fill();
  // draw the number in center
  ctx.font = "bold 12px Helvetica, sans-serif";
  ctx.textBaseline = "middle";
  ctx.textAlign = "center";
  ctx.fillStyle = "white";
  ctx.shadowBlur = 1;
  ctx.shadowColor = "black";
  ctx.shadowOffsetX = 1;
  ctx.shadowOffsetY = 1;
  ctx.fillText(number, 8, 8);
  return canvas.toDataURL("image/png");
}

function restoreOriginalOwaIcon() {
  document.head.appendChild(initialOwaIcon);
}

function setFavicon(count) {
  const nr = Math.min(count, 99);
  if (nr) {
    owaIcon.href = generateTabIcon(nr);
    document.head.appendChild(owaIcon);
  } else {
    restoreOriginalOwaIcon();
  }
}

function restoreInitialDocumentTitle() {
  document.title = initialDocumentTitle;
}

function setDocumentTitle(emails, reminders, chats) {
  let countPrefix = "";
  if (emails > 0 || reminders > 0 || chats > 0) {
    countPrefix = "(" + emails + "/" + reminders;
    if (chats > 0) {
      countPrefix = countPrefix + "/" + chats;
    }
    countPrefix = countPrefix + ") ";
  }
  document.title = countPrefix + initialDocumentTitle;
}

function extractNumber(text) {
  if (text) {
    let digits = text.match(/\d/gi);
    if (digits) {
      return parseInt(digits.join(""), 10);
    }
  }
  return 0;
}

function getCountFromNodes(nodes) {
  let count = 0;
  if (nodes) {
    for (let i = nodes.length - 1; i >= 0; i--) {
      count += extractNumber(nodes[i].innerHTML);
    }
  }
  return count;
}

function getItemsWithActiveCount(folder) {
  return folder.querySelectorAll("[id*='.ucount']");
}

function getCountFromFolders(folders) {
  let count = 0;
  for (let i = folders.length - 1; i >= 0; i--) {
    count += getCountFromNodes(getItemsWithActiveCount(folders[i]));
  }
  return count;
}

function countUnreadEmails() {
  let nodes;
  if (prefs.cssForUnreadEmailsDetection) {
    // custom css
    return getCountFromNodes(document.querySelectorAll(prefs.cssForUnreadEmailsDetection));
  }
  if ((nodes = document.querySelectorAll("#spnUC #spnCV")).length > 0) {
    // OWA 2010
    return getCountFromNodes(nodes);
  }
  if ((nodes = document.querySelectorAll("[aria-label='Folder Pane']")).length > 0) {
    // OWA 2013
    return getCountFromFolders(nodes);
  }
  if ((nodes = document.querySelectorAll("[title='Favorites'] ~ div > div > [title='Inbox'] span:nth-of-type(2) > span")).length > 0) {
    // outlook.live.com
    return getCountFromNodes(nodes);
  }
  if ((nodes = document.querySelectorAll("[title='Folders'] ~ div > [title='Inbox'] span:nth-of-type(2) > span")).length > 0) {
    // outlook.live.com Inbox not added to Favorites
    return getCountFromNodes(nodes);
  }

  return 0;
}

function countVisibleReminders() {
  let nodes;
  if (prefs.cssForVisibleRemindersDetection) {
    // custom css
    return getCountFromNodes(document.querySelectorAll(prefs.cssForVisibleRemindersDetection));
  }
  if ((nodes = document.querySelectorAll("#spnRmT.alertBtnTxt")).length > 0) {
    // OWA 2010
    return extractNumber(nodes[0].innerHTML);
  }<commit_msg>update for Office365 owa, see #29<commit_after>"use strict";

let prefs = {
  favIconColor : "#0099FF"
};
let newEventsTimer, remindersTimer;
let unreadEmailsCount = 0;
let visibleRemindersCount = 0;
let chatNotificationsCount = 0;
const initialDocumentTitle = document.title;
const initialOwaIcon = getCurrentFavIcon();
const owaIcon = createIconElement();

browser.storage.onChanged.addListener(onPrefChanged);

getPrefsAndStart();

function createIconElement() {
  let icon = document.createElement("link");
  icon.rel = "icon";
  icon.type = "image/png";
  icon.sizes = "16x16";
  icon.href = generateTabIcon(0);
  return icon;
}

function getCurrentFavIcon() {
  let icons = document.head.querySelectorAll("link[rel*=icon]");
  let icon;
  if (icons.length > 0) {
    icon = icons[icons.length - 1];
  } else {
    icon = createIconElement();
  }
  return icon;
}

function generateTabIcon(number) {
  const canvas = document.createElement("canvas");
  canvas.width = 16;
  canvas.height = 16;
  const ctx = canvas.getContext("2d");
  // draw cliped circle (radius is bigger than canvas by 1px) - the effect is a
  // square with round corners
  ctx.fillStyle = prefs.favIconColor;
  ctx.beginPath();
  ctx.arc(8, 8, 9, 0, 2 * Math.PI);
  ctx.fill();
  // draw the number in center
  ctx.font = "bold 12px Helvetica, sans-serif";
  ctx.textBaseline = "middle";
  ctx.textAlign = "center";
  ctx.fillStyle = "white";
  ctx.shadowBlur = 1;
  ctx.shadowColor = "black";
  ctx.shadowOffsetX = 1;
  ctx.shadowOffsetY = 1;
  ctx.fillText(number, 8, 8);
  return canvas.toDataURL("image/png");
}

function restoreOriginalOwaIcon() {
  document.head.appendChild(initialOwaIcon);
}

function setFavicon(count) {
  const nr = Math.min(count, 99);
  if (nr) {
    owaIcon.href = generateTabIcon(nr);
    document.head.appendChild(owaIcon);
  } else {
    restoreOriginalOwaIcon();
  }
}

function restoreInitialDocumentTitle() {
  document.title = initialDocumentTitle;
}

function setDocumentTitle(emails, reminders, chats) {
  let countPrefix = "";
  if (emails > 0 || reminders > 0 || chats > 0) {
    countPrefix = "(" + emails + "/" + reminders;
    if (chats > 0) {
      countPrefix = countPrefix + "/" + chats;
    }
    countPrefix = countPrefix + ") ";
  }
  document.title = countPrefix + initialDocumentTitle;
}

function extractNumber(text) {
  if (text) {
    let digits = text.match(/\d/gi);
    if (digits) {
      return parseInt(digits.join(""), 10);
    }
  }
  return 0;
}

function getCountFromNodes(nodes) {
  let count = 0;
  if (nodes) {
    for (let i = nodes.length - 1; i >= 0; i--) {
      count += extractNumber(nodes[i].innerHTML);
    }
  }
  return count;
}

function getItemsWithActiveCount(folder) {
  return folder.querySelectorAll("[id*='.ucount']");
}

function getCountFromFolders(folders) {
  let count = 0;
  for (let i = folders.length - 1; i >= 0; i--) {
    count += getCountFromNodes(getItemsWithActiveCount(folders[i]));
  }
  return count;
}

function getOffice365CountFromNodes(nodes) {
  return Array.from(nodes)
              .filter(e => e.textContent==='unread')
              .map(e => parseInt(e.previousSibling.textContent, 10))
              .filter(v => !isNaN(v))
              .reduce((acc, curr) => { return acc + curr}, 0);
}

function countUnreadEmails() {
  let nodes;
  if (prefs.cssForUnreadEmailsDetection) {
    // custom css
    return getCountFromNodes(document.querySelectorAll(prefs.cssForUnreadEmailsDetection));
  }
  if ((nodes = document.querySelectorAll("#spnUC #spnCV")).length > 0) {
    // OWA 2010
    return getCountFromNodes(nodes);
  }
  if ((nodes = document.querySelectorAll("[aria-label='Folder Pane']")).length > 0) {
    // OWA 2013
    return getCountFromFolders(nodes);
  }
  if ((nodes = document.querySelectorAll("[title='Favorites'] ~ div > div > [title='Inbox'] span:nth-of-type(2) > span")).length > 0) {
    // outlook.live.com
    return getCountFromNodes(nodes);
  }
  if ((nodes = document.querySelectorAll("[title='Folders'] ~ div > [title='Inbox'] span:nth-of-type(2) > span")).length > 0) {
    // outlook.live.com Inbox not added to Favorites
    return getCountFromNodes(nodes);
  }
  if ((nodes = document.querySelectorAll("span span span.screenReaderOnly")).length > 0) {
    // Office365 owa
    return getOffice365CountFromNodes(nodes);
  }

  return 0;
}

function countVisibleReminders() {
  let nodes;
  if (prefs.cssForVisibleRemindersDetection) {
    // custom css
    return getCountFromNodes(document.querySelectorAll(prefs.cssForVisibleRemindersDetection));
  }
  if ((nodes = document.querySelectorAll("#spnRmT.alertBtnTxt")).length > 0) {
    // OWA 2010
    return extractNumber(nodes[0].innerHTML);
  }
{'fraction_non_alphanumeric': 0.14402234636871508, 'fraction_numerical': 0.017206703910614525, 'mean_word_length': 3.7561105207226353, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1165304', 'n_tokens_mistral': 3137, 'n_tokens_neox': 3019, 'n_words': 648}