Selaa lähdekoodia

add url parser to detect current site

pull/342/head
Guillaume Vincent 8 vuotta sitten
vanhempi
commit
5ea110cbfb
3 muutettua tiedostoa jossa 117 lisäystä ja 0 poistoa
  1. +1
    -0
      package.json
  2. +45
    -0
      src/domain/url-parser.js
  3. +71
    -0
      test/url-parser.js

+ 1
- 0
package.json Näytä tiedosto

@@ -38,6 +38,7 @@
"pilou": "^0.1.4",
"style-loader": "^0.13.1",
"tether": "^1.3.7",
"tldjs": "^1.7.0",
"url-loader": "^0.5.7",
"vue": "^2.0.1",
"vue-loader": "^9.5.1",


+ 45
- 0
src/domain/url-parser.js Näytä tiedosto

@@ -0,0 +1,45 @@
'use strict';
import tld from 'tldjs';

function _ipIsValid(ipAddress) {
return Boolean(/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipAddress));
}

function getDomainName(urlStr) {
const domain = tld.getDomain(urlStr);
const subDomain = tld.getSubdomain(urlStr);
const ip = `${subDomain}.${domain}`;
if (_ipIsValid(ip)) {
return ip;
}
return domain;
}

function isWebExtension() {
if (typeof chrome !== 'undefined' && typeof chrome.tabs !== 'undefined') {
return typeof chrome.tabs.query === 'function';
}
return false;
}

function getCurrentUrl() {
return new Promise(function (resolve) {
chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
resolve(tabs[0].url);
});
});
}


function getSite() {
if (isWebExtension()) {
return getCurrentUrl().then(currentUrl => {
return getDomainName(currentUrl)
});
}
return new Promise(function (resolve) {
resolve('')
});
}

export {getDomainName, _ipIsValid, isWebExtension, getCurrentUrl, getSite};

+ 71
- 0
test/url-parser.js Näytä tiedosto

@@ -0,0 +1,71 @@
import test from 'ava';
import {getDomainName, _ipIsValid, isWebExtension, getCurrentUrl, getSite} from '../src/domain/url-parser';

test(t => {
t.is('lesspass.com', getDomainName('https://lesspass.com/#!/'));
t.is('lesspass.com', getDomainName('https://lesspass.com/api/'));
t.is('lesspass.com', getDomainName('https://api.lesspass.com/'));
t.is('192.168.1.1', getDomainName('https://192.168.1.1:10443/webapp/'));
});

test('ip validator', t => {
t.true(_ipIsValid('192.168.23.215'));
t.true(_ipIsValid('127.0.0.1'));
t.false(_ipIsValid('210.110'), 'must have 4 octets');
t.false(_ipIsValid('255'), 'must have 4 octets');
t.false(_ipIsValid('y.y.y.y'), 'only digits are allowed');
t.false(_ipIsValid('255.0.0.y'), 'only digits are allowed');
t.false(_ipIsValid('666.10.10.20'), 'octet number must be between [0-255]');
t.false(_ipIsValid('4444.11.11.11'), 'octet number must be between [0-255]');
t.false(_ipIsValid('33.3333.33.3'), 'octet number must be between [0-255]');
});

test('get web extension context', t => {
global.chrome = undefined;
t.false(isWebExtension())
});

test('get web extension context', t => {
global.chrome = {
tabs: {
query(a, b){
console.log(a, b)
}
}
};
t.true(isWebExtension())
});

test('get current tab', t => {
const url = 'example.org';
global.chrome = {
tabs: {
query(a, callback){
callback([{url}])
}
}
};
return getCurrentUrl().then(currentUrl => {
t.is(currentUrl, url)
});
});

test('get default site', t => {
global.chrome = {
tabs: {
query(a, callback){
callback([{url: 'https://example.org'}])
}
}
};
return getSite().then(site => {
t.is(site, 'example.org')
});
});

test('get default site if not in web extension', t => {
global.chrome = undefined;
return getSite().then(site => {
t.is(site, '')
});
});

Ladataan…
Peruuta
Tallenna