|
/*jshint -W028, esversion: 6 */ |
|
javascript: |
|
(function() { |
|
window.Portabilitate = { |
|
init: function() { |
|
// Împotriva site-urilor malefice care strică Dev Tools |
|
if(!console.info || !console.error) fixConsole(); |
|
// Există selecție făcută de utilizator pe pagină? |
|
var sel = (window.getSelection().toString()) ? window.getSelection() : null, |
|
// Preia numarul din selecția făcută de utilizator |
|
number = window.getSelection().toString().replace(/\D/g, ''), |
|
// Tag-ul care va ține raspunsul cu rețeaua curentă |
|
node = document.createElement('mark'), |
|
manual = false; |
|
// Utilizatorul nu a selectat nimic, preia numărul printr-un prompt |
|
if (!number) { |
|
manual = true; |
|
number = (prompt('Află dacă următorul număr e portat:\n', 'ex. 07xx xxx xx xx') || '') |
|
.replace(/\D/g, ''); |
|
} |
|
|
|
function fixConsole() { |
|
// Restore console object from iframe |
|
var i = document.createElement('iframe'); |
|
i.style.display = 'none'; |
|
document.body.appendChild(i); |
|
window.console = i.contentWindow.console; |
|
} |
|
|
|
function initUI() { |
|
// Afișează în consolă ce cautare a început |
|
console.info((sel || number) + ' e portat?...'); |
|
// Definește stilul răspunsului |
|
node.class = 'portabilitate'; |
|
node.style.margin = '0 4px'; |
|
node.style.padding = '0 8px'; |
|
// Ce placeholder să apară până se încarcă |
|
node.appendChild(document.createTextNode('?...')); |
|
// Inserează răspunsul după selecția făcuta de utilizator |
|
var range; |
|
if (sel && sel.getRangeAt && sel.rangeCount) { |
|
range = sel.getRangeAt(0); |
|
range.collapse(false); |
|
range.insertNode(node); |
|
} |
|
} |
|
|
|
initUI(); |
|
|
|
function showResults() { |
|
// Afișează rezultatul căutării în consolă |
|
var r = (Portabilitate.result.portat) ? 'Da. ' : 'Nu. ', |
|
msg = r + 'E in ' + Portabilitate.result.reteaCurenta + '.\n\n'; |
|
console.info(msg); |
|
// Dacă s-a introdus în prompt, afișează și în alertbox |
|
if (manual) { alert(msg); } |
|
// Afiseaza rezultatul în dreptul selecției |
|
node.textContent = Portabilitate.result.reteaCurenta; |
|
// Simbolizează că numărul e portat |
|
if (Portabilitate.result.portat) { node.style.color = 'red'; } |
|
} |
|
|
|
// Hitting the endpoint directly doesn't work on HTTPS pages |
|
// So we have to use a CORS proxy in order to make an HTTPS request |
|
let url = 'https://cors-anywhere.herokuapp.com/http://portabilitate.ro/ro-no-' + number; |
|
fetch(url, { headers: new Headers({'Origin': location.origin}), mode: 'cors' }) |
|
.then(response => response.text()) |
|
// .then(str => (new window.DOMParser()).parseFromString(str, "text/xml")) |
|
.then(data => Portabilitate.parse(data)) |
|
.then(data => showResults()) |
|
.catch(e => console.error('Sorry. The number you have dialed is not in service')); |
|
}, |
|
parse: function (data) { |
|
Portabilitate.result = { |
|
portat: !/\d{3,} nu este portat/g.test(data), |
|
reteaCurenta: /(?:lnkOperator">)(.+?)(?=<)/g.exec(data)[1] |
|
}; |
|
// Afișare mai omenească a rețelei curente |
|
const retele = { |
|
'VODAFONE ROMANIA': 'Vodafone', |
|
'ORANGE ROMANIA': 'Orange', |
|
'TELEKOM ROMANIA MOBILE (COSMOTE)': 'Telekom Mobil', |
|
'TELEMOBIL': 'ZAPP', |
|
'TELEKOM ROMANIA (ROMTELECOM)': 'Telekom Fix', |
|
'RCS & RDS': 'RDS', |
|
'UPC ROMANIA': 'UPC', |
|
}; |
|
Portabilitate.result.reteaCurenta = retele[Portabilitate.result.reteaCurenta]; |
|
} |
|
}; |
|
window.Portabilitate.init(); |
|
}()); |