Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function login(){
- var login = document.getElementById('login').value;
- var pass = document.getElementById('pass').value;
- //Get oAuth Token Step 1.
- $.ajax({
- url: url + 'oauth2/token/',
- type: 'post',
- data: {'grant_type': 'password',
- 'client_id' : clientID,
- 'client_secret': clientSecret,
- 'username': login,
- 'password': pass,},
- headers: { "Content-Type":'application/x-www-form-urlencoded' },
- contentType: 'application/x-www-form-urlencoded',
- success: function (data) {
- log('Login Successful');
- log("Set: " + url);
- chrome.storage.sync.set({
- pass: pass,
- login_name: login,
- logged: 'true',
- }, function() {
- setTimeout(function() {
- status.textContent = '';
- }, 750);
- save();
- setDisplayMode();
- });
- },
- error: function(xhr, exception){
- if(xhr.status == 400 || xhr.status == 403){
- log(xhr);
- log(exception);
- invalidLogin();
- return "Invalid Credentials";
- }
- },
- });
- }
- /**
- * Query and store user data to chrome.storage
- */
- function save() {
- var login = '';
- var pass = '';
- //Get oAuth Token Step 1.
- chrome.storage.sync.get({
- pass: '',
- login_name: '',
- }, function(items){
- login = items.login_name;
- pass = items.pass;
- $("#result").html("<img width='20px' src='loading.gif'>");
- $.ajax({
- url: url + 'oauth2/token/',
- type: 'post',
- data: {'grant_type': 'password',
- 'client_id' : clientID,
- 'client_secret': clientSecret,
- 'username': login,
- 'password': pass,},
- headers: { "Content-Type":'application/x-www-form-urlencoded' },
- contentType: 'application/x-www-form-urlencoded',
- success: function (data) {
- var user = login.split('@')[0];
- //oAuth Step 2
- $.ajax({
- url: url,
- type: 'post',
- data: {
- 'action': 'read',
- 'object': 'subscriber',
- 'login': login,
- },
- headers: {
- 'Authorization': 'Bearer ' + data.access_token
- },
- success: function (data) {
- log(data);
- user = $(data).find("user").text()
- $('#login_name').html(login);
- $('#extension').html($(data).find("user").text());
- $('#domain_name').html($(data).find("domain").text());
- var areaCode = $(data).find("area_code").text();
- document.getElementById("area_code").value = areaCode ;
- $("#result").html("SIP Username: " + user + "<br>Area Code: " + areaCode);
- chrome.storage.sync.set({
- user: $(data).find("user").text(),
- domain: $(data).find("domain").text(),
- dialstring_length: document.getElementById('dialstring_length').value,
- intl_prefix: document.getElementById("intl_prefix").value,
- country_initials: document.getElementById("country_code")
- [document.getElementById("country_code").selectedIndex]
- .getAttribute('country_initials'),
- country_code: document.getElementById("country_code").value,
- areacode: areaCode,
- use_list: $("input.use_list:checked").val(),
- logged: 'true'
- }, function() {
- // Update status to let user know options were saved.
- var status = document.getElementById('status');
- status.textContent = 'Options saved.';
- log('Save Successful');
- status.textContent = '';
- setDisplayMode();
- });
- },
- error: function(xhr, exception){
- chrome.storage.sync.set({
- logged: 'false'
- });
- if(xhr.status == 400 || xhr.status == 403){
- alert("Invalid Credentials");
- $("#result").html("Invalid Credentials");
- }
- },
- });
- },
- error: function(xhr, exception, third){
- if(xhr.status == 400 || xhr.status == 403){
- log(xhr);
- log(exception);
- log(third);
- invalidLogin();
- return "Invalid Credentials";
- }
- },
- });
- });
- }
- /**
- * Query and store user data to chrome.storage
- */
- function save() {
- var login = '';
- var pass = '';
- //Get oAuth Token Step 1.
- chrome.storage.sync.get({
- pass: '',
- login_name: '',
- }, function(items){
- login = items.login_name;
- pass = items.pass;
- $("#result").html("<img width='20px' src='loading.gif'>");
- $.ajax({
- url: url + 'oauth2/token/',
- type: 'post',
- data: {'grant_type': 'password',
- 'client_id' : clientID,
- 'client_secret': clientSecret,
- 'username': login,
- 'password': pass,},
- headers: { "Content-Type":'application/x-www-form-urlencoded' },
- contentType: 'application/x-www-form-urlencoded',
- success: function (data) {
- var user = login.split('@')[0];
- //oAuth Step 2
- $.ajax({
- url: url,
- type: 'post',
- data: {
- 'action': 'read',
- 'object': 'subscriber',
- 'login': login,
- },
- headers: {
- 'Authorization': 'Bearer ' + data.access_token
- },
- success: function (data) {
- log(data);
- user = $(data).find("user").text()
- $('#login_name').html(login);
- $('#extension').html($(data).find("user").text());
- $('#domain_name').html($(data).find("domain").text());
- var areaCode = $(data).find("area_code").text();
- document.getElementById("area_code").value = areaCode ;
- $("#result").html("SIP Username: " + user + "<br>Area Code: " + areaCode);
- chrome.storage.sync.set({
- user: $(data).find("user").text(),
- domain: $(data).find("domain").text(),
- dialstring_length: document.getElementById('dialstring_length').value,
- intl_prefix: document.getElementById("intl_prefix").value,
- country_initials: document.getElementById("country_code")
- [document.getElementById("country_code").selectedIndex]
- .getAttribute('country_initials'),
- country_code: document.getElementById("country_code").value,
- areacode: areaCode,
- use_list: $("input.use_list:checked").val(),
- logged: 'true'
- }, function() {
- // Update status to let user know options were saved.
- var status = document.getElementById('status');
- status.textContent = 'Options saved.';
- log('Save Successful');
- status.textContent = '';
- setDisplayMode();
- });
- },
- error: function(xhr, exception){
- chrome.storage.sync.set({
- logged: 'false'
- });
- if(xhr.status == 400 || xhr.status == 403){
- alert("Invalid Credentials");
- $("#result").html("Invalid Credentials");
- }
- },
- });
- },
- error: function(xhr, exception, third){
- if(xhr.status == 400 || xhr.status == 403){
- log(xhr);
- log(exception);
- log(third);
- invalidLogin();
- return "Invalid Credentials";
- }
- },
- });
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement