mirror of
https://github.com/zero-peak/ZeroOmega.git
synced 2025-02-02 02:58:13 -05:00
Try to use base domain names when adding conditions. Fix #276.
This commit is contained in:
parent
62133c2d7c
commit
806f3215e8
@ -38,3 +38,20 @@ class AttachedCache
|
|||||||
obj[@prop] = value
|
obj[@prop] = value
|
||||||
|
|
||||||
exports.AttachedCache = AttachedCache
|
exports.AttachedCache = AttachedCache
|
||||||
|
|
||||||
|
exports.getBaseDomain = (domain) ->
|
||||||
|
return domain if domain.indexOf(':') > 0 # IPv6
|
||||||
|
lastCharCode = domain.charCodeAt(domain.length - 1)
|
||||||
|
return domain if 48 <= lastCharCode <= 57 # IP address ending with number.
|
||||||
|
segments = domain.split('.')
|
||||||
|
if segments.length <= 2
|
||||||
|
return domain
|
||||||
|
if segments[0] == 'www'
|
||||||
|
segments.shift()
|
||||||
|
len = segments.length
|
||||||
|
if len <= 2
|
||||||
|
return segments.join('.')
|
||||||
|
if segments[len - 2].length <= 2
|
||||||
|
return segments[len - 3] + '.' + segments[len - 2] + '.' + segments[len - 1]
|
||||||
|
else
|
||||||
|
return segments[len - 2] + '.' + segments[len - 1]
|
||||||
|
29
omega-pac/test/utils.coffee
Normal file
29
omega-pac/test/utils.coffee
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
chai = require 'chai'
|
||||||
|
should = chai.should()
|
||||||
|
Utils = require '../src/utils'
|
||||||
|
|
||||||
|
describe 'getBaseDomain', ->
|
||||||
|
{getBaseDomain} = Utils
|
||||||
|
it 'should return domains with zero level unchanged', ->
|
||||||
|
getBaseDomain('someinternaldomain').should.equal('someinternaldomain')
|
||||||
|
it 'should return domains with one level unchanged', ->
|
||||||
|
getBaseDomain('example.com').should.equal('example.com')
|
||||||
|
getBaseDomain('e.test').should.equal('e.test')
|
||||||
|
getBaseDomain('a.b').should.equal('a.b')
|
||||||
|
it 'should ignore the leading www with domains with two or more levels', ->
|
||||||
|
getBaseDomain('www.example.com').should.equal('example.com')
|
||||||
|
getBaseDomain('www.e.test').should.equal('e.test')
|
||||||
|
getBaseDomain('www.a.b').should.equal('a.b')
|
||||||
|
it 'should assume two-segment TLD if len(second segment from last) <= 2', ->
|
||||||
|
getBaseDomain('images.google.co.uk').should.equal('google.co.uk')
|
||||||
|
getBaseDomain('images.google.co.jp').should.equal('google.co.jp')
|
||||||
|
getBaseDomain('ab.de.ef.test').should.equal('de.ef.test')
|
||||||
|
it 'should assume one-segment TLD and keep two segments as base otherwise', ->
|
||||||
|
getBaseDomain('subdomain.example.com').should.equal('example.com')
|
||||||
|
getBaseDomain('some.site.example.net').should.equal('example.net')
|
||||||
|
getBaseDomain('some.site.abc.test').should.equal('abc.test')
|
||||||
|
getBaseDomain('ab.de.efg.test').should.equal('efg.test')
|
||||||
|
it 'should not try to modify IP address literals', ->
|
||||||
|
getBaseDomain('127.0.0.1').should.equal('127.0.0.1')
|
||||||
|
getBaseDomain('[::1]').should.equal('[::1]')
|
||||||
|
getBaseDomain('::f').should.equal('::f')
|
@ -119,36 +119,19 @@ angular.module('omegaTarget', []).factory 'omegaTarget', ($q) ->
|
|||||||
d = $q['defer']()
|
d = $q['defer']()
|
||||||
chrome.tabs.query {active: true, lastFocusedWindow: true}, (tabs) ->
|
chrome.tabs.query {active: true, lastFocusedWindow: true}, (tabs) ->
|
||||||
if not tabs[0]?.url
|
if not tabs[0]?.url
|
||||||
d.resolve(undefined)
|
d.resolve(null)
|
||||||
return
|
return
|
||||||
|
args = {tabId: tabs[0].id, url: tabs[0].url}
|
||||||
if tabs[0].id and requestInfoCallback
|
if tabs[0].id and requestInfoCallback
|
||||||
connectBackground('tabRequestInfo', {tabId: tabs[0].id},
|
connectBackground('tabRequestInfo', args,
|
||||||
requestInfoCallback)
|
requestInfoCallback)
|
||||||
getBadge = $q['defer']()
|
d.resolve(callBackground('getPageInfo', args))
|
||||||
chrome.browserAction.getBadgeText {tabId: tabs[0]?.id}, (result) ->
|
return d.promise
|
||||||
getBadge.resolve(result)
|
|
||||||
$q.all([getBadge.promise, omegaTarget.state('inspectUrl')
|
|
||||||
]).then ([badge, url]) ->
|
|
||||||
if badge != '#' || not url
|
|
||||||
d.resolve(tabs[0]?.url)
|
|
||||||
else
|
|
||||||
clearBadge = false
|
|
||||||
d.resolve(url)
|
|
||||||
return d.promise.then (url) ->
|
|
||||||
# First, try to clear badges on opening the popup.
|
|
||||||
callBackground('clearBadge') if clearBadge
|
|
||||||
return null if not url or isChromeUrl(url)
|
|
||||||
urlParser.href = url
|
|
||||||
domain = urlParser.hostname
|
|
||||||
callBackground('queryTempRule', domain).then (profileName) ->
|
|
||||||
url: url
|
|
||||||
domain: domain
|
|
||||||
tempRuleProfileName: profileName
|
|
||||||
refreshActivePage: ->
|
refreshActivePage: ->
|
||||||
d = $q['defer']()
|
d = $q['defer']()
|
||||||
chrome.tabs.query {active: true, lastFocusedWindow: true}, (tabs) ->
|
chrome.tabs.query {active: true, lastFocusedWindow: true}, (tabs) ->
|
||||||
if tabs[0].url and not isChromeUrl(tabs[0].url)
|
if tabs[0].url and not isChromeUrl(tabs[0].url)
|
||||||
chrome.tabs.reload(tabs[0].id)
|
chrome.tabs.reload(tabs[0].id, {bypassCache: true})
|
||||||
d.resolve()
|
d.resolve()
|
||||||
return d.promise
|
return d.promise
|
||||||
openManage: ->
|
openManage: ->
|
||||||
|
@ -2,7 +2,7 @@ OmegaTarget = require('omega-target')
|
|||||||
OmegaPac = OmegaTarget.OmegaPac
|
OmegaPac = OmegaTarget.OmegaPac
|
||||||
Promise = OmegaTarget.Promise
|
Promise = OmegaTarget.Promise
|
||||||
xhr = Promise.promisify(require('xhr'))
|
xhr = Promise.promisify(require('xhr'))
|
||||||
url = require('url')
|
Url = require('url')
|
||||||
chromeApiPromisifyAll = require('./chrome_api')
|
chromeApiPromisifyAll = require('./chrome_api')
|
||||||
proxySettings = chromeApiPromisifyAll(chrome.proxy.settings)
|
proxySettings = chromeApiPromisifyAll(chrome.proxy.settings)
|
||||||
parseExternalProfile = require('./parse_external_profile')
|
parseExternalProfile = require('./parse_external_profile')
|
||||||
@ -16,10 +16,10 @@ class ChromeOptions extends OmegaTarget.Options
|
|||||||
|
|
||||||
fetchUrl: (dest_url, opt_bypass_cache) ->
|
fetchUrl: (dest_url, opt_bypass_cache) ->
|
||||||
if opt_bypass_cache
|
if opt_bypass_cache
|
||||||
parsed = url.parse(dest_url, true)
|
parsed = Url.parse(dest_url, true)
|
||||||
parsed.search = undefined
|
parsed.search = undefined
|
||||||
parsed.query['_'] = Date.now()
|
parsed.query['_'] = Date.now()
|
||||||
dest_url = url.format(parsed)
|
dest_url = Url.format(parsed)
|
||||||
xhr(dest_url).get(1)
|
xhr(dest_url).get(1)
|
||||||
|
|
||||||
updateProfile: (args...) ->
|
updateProfile: (args...) ->
|
||||||
@ -209,7 +209,7 @@ class ChromeOptions extends OmegaTarget.Options
|
|||||||
else
|
else
|
||||||
chrome.browserAction.setBadgeText(text: '', tabId: tabId)
|
chrome.browserAction.setBadgeText(text: '', tabId: tabId)
|
||||||
@_tabRequestInfoPorts[tabId]?.postMessage(
|
@_tabRequestInfoPorts[tabId]?.postMessage(
|
||||||
@_requestMonitor.summarizeErrors(info))
|
@_requestMonitor.summarizeErrors(info, OmegaPac.getBaseDomain))
|
||||||
|
|
||||||
chrome.runtime.onConnect.addListener (port) =>
|
chrome.runtime.onConnect.addListener (port) =>
|
||||||
return unless port.name == 'tabRequestInfo'
|
return unless port.name == 'tabRequestInfo'
|
||||||
@ -219,7 +219,9 @@ class ChromeOptions extends OmegaTarget.Options
|
|||||||
tabId = msg.tabId
|
tabId = msg.tabId
|
||||||
@_tabRequestInfoPorts[tabId] = port
|
@_tabRequestInfoPorts[tabId] = port
|
||||||
info = @_requestMonitor.tabInfo[tabId]
|
info = @_requestMonitor.tabInfo[tabId]
|
||||||
port.postMessage(@_requestMonitor.summarizeErrors(info)) if info
|
if info
|
||||||
|
summ = @_requestMonitor.summarizeErrors info, OmegaPac.getBaseDomain
|
||||||
|
port.postMessage(summ)
|
||||||
port.onDisconnect.addListener =>
|
port.onDisconnect.addListener =>
|
||||||
delete @_tabRequestInfoPorts[tabId] if tabId?
|
delete @_tabRequestInfoPorts[tabId] if tabId?
|
||||||
|
|
||||||
@ -299,5 +301,24 @@ class ChromeOptions extends OmegaTarget.Options
|
|||||||
onFirstRun: (reason) ->
|
onFirstRun: (reason) ->
|
||||||
chrome.tabs.create url: chrome.extension.getURL('options.html')
|
chrome.tabs.create url: chrome.extension.getURL('options.html')
|
||||||
|
|
||||||
|
getPageInfo: ({tabId, url}) ->
|
||||||
|
getBadge = new Promise (resolve, reject) ->
|
||||||
|
chrome.browserAction.getBadgeText {tabId: tabId}, (result) ->
|
||||||
|
resolve(result)
|
||||||
|
|
||||||
|
getInspectUrl = @_state.get({inspectUrl: ''})
|
||||||
|
Promise.join getBadge, getInspectUrl, (badge, {inspectUrl}) =>
|
||||||
|
if badge == '#' and inspectUrl
|
||||||
|
url = inspectUrl
|
||||||
|
else
|
||||||
|
@clearBadge()
|
||||||
|
return null if not url or url.substr(0, 6) == 'chrome'
|
||||||
|
domain = OmegaPac.getBaseDomain(Url.parse(url).hostname)
|
||||||
|
return {
|
||||||
|
url: url
|
||||||
|
domain: domain
|
||||||
|
tempRuleProfileName: @queryTempRule(domain)
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = ChromeOptions
|
module.exports = ChromeOptions
|
||||||
|
|
||||||
|
@ -150,12 +150,13 @@ module.exports = class WebRequestMonitor
|
|||||||
for callback in @_tabCallbacks
|
for callback in @_tabCallbacks
|
||||||
callback(req.tabId, info, req, status)
|
callback(req.tabId, info, req, status)
|
||||||
|
|
||||||
summarizeErrors: (info) ->
|
summarizeErrors: (info, domainOfHost) ->
|
||||||
domains = []
|
domains = []
|
||||||
domainInfoByName = {}
|
domainInfoByName = {}
|
||||||
for reqId, req of info.requests
|
for reqId, req of info.requests
|
||||||
if @eventCategory[info.requestStatus[reqId]] == 'error'
|
if @eventCategory[info.requestStatus[reqId]] == 'error'
|
||||||
domain = Url.parse(req.url).hostname
|
domain = Url.parse(req.url).hostname
|
||||||
|
domain = domainOfHost(domain) if domainOfHost
|
||||||
domainInfo = domainInfoByName[domain]
|
domainInfo = domainInfoByName[domain]
|
||||||
if not domainInfo
|
if not domainInfo
|
||||||
domainInfo = domainInfoByName[domain] = {
|
domainInfo = domainInfoByName[domain] = {
|
||||||
|
Loading…
Reference in New Issue
Block a user