-
Notifications
You must be signed in to change notification settings - Fork 5
/
content-script.js
42 lines (34 loc) · 1.22 KB
/
content-script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"use strict";
browser.runtime.onMessage.addListener(request => {
//console.log("Message from the background script:");
//console.log(request.action);
return Promise.resolve({response: window.getSelection().toString().trim()});
});
function hasNumbers(string)
{
return /\d/.test(string);
}
// Workaround without onBeforeShow for Firefox < 60
// https://bugzilla.mozilla.org/show_bug.cgi?id=1215376
// Event for normal text
document.addEventListener("selectionchange", function() {
var selectedText = window.getSelection();
sendNumber(selectedText);
});
// Event for input/textbox
document.addEventListener("select", function(e){
var selectedText = e.target.value.substring(e.target.selectionStart, e.target.selectionEnd);
sendNumber(selectedText);
});
function sendNumber(selectedText){
// Check if the selected string has number in it
if (hasNumbers(selectedText.toString().trim())) {
var selectedNumber = selectedText.toString().trim().replace(/\+/g, "00").replace(/\D/g,'');
//console.log('Selected number is ' + selectedNumber);
var sending = browser.runtime.sendMessage({
selectedNumber: selectedNumber,
action: "selectionText"
});
//runtime.sendMessage();
}
}