Skip to content

Commit

Permalink
fixed for multple time zones in a single node
Browse files Browse the repository at this point in the history
  • Loading branch information
codemonkey-uk committed Sep 19, 2020
1 parent 9f1728e commit 9b20bc6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
34 changes: 25 additions & 9 deletions content.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ function pad0(num)
return s;
}

function insert(main_string, ins_string, pos)
{
if(typeof(pos) == "undefined") {
pos = 0;
}
if(typeof(ins_string) == "undefined") {
ins_string = '';
}
return main_string.slice(0, pos) + ins_string + main_string.slice(pos);
}

function replaceText (node)
{
if (node.nodeType === Node.TEXT_NODE) {
Expand All @@ -20,11 +31,9 @@ function replaceText (node)
}

var text = node.nodeValue;
const regex = /\b(\d{1,2})(?::?(\d\d))?\s*([aApP]?[mM])?\s*([A-Z]{2,5})([+-]\d{1,2}:?\d{0,2})?/;
var found = text.match(regex);


if (found)
var updatedText = text;
var regex = /\b(\d{1,2})(?::?(\d\d))?\s*([aApP]?[mM])?\s*([A-Z]{2,5})([+-]\d{1,2}:?\d{0,2})?/g;
while(found = regex.exec(text))
{
var hour = parseInt(found[1]);
var minute = found[2] ? parseInt(found[2]) : 0;
Expand Down Expand Up @@ -86,13 +95,20 @@ function replaceText (node)
hour-=24;
}

var replacedText = text.replace(
found[0],
found[0] + " ("+pad0(hour)+":"+pad0(minute)+" "+localZone+")"
// found.index
updatedText = insert(
updatedText,
" ("+pad0(hour)+":"+pad0(minute)+" "+localZone+") ",
found.index + found[0].length + (updatedText.length-text.length)
);
node.textContent = replacedText;

//updatedText = updatedText.replace(
// found[0],
// found[0] + " ("+pad0(hour)+":"+pad0(minute)+" "+localZone+")"
//);
}
}
node.textContent = updatedText;
}
else {
// This node contains more than just text, call replaceText() on each
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "Localise Timezones",
"description": "Converts times to your local time zone",
"version": "1.1",
"version": "0.1",
"content_scripts": [
{
"matches": [
Expand Down

0 comments on commit 9b20bc6

Please sign in to comment.