Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactorization #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Code reorganization
- change code order
  • Loading branch information
Krzysztof Łukasik committed Jun 3, 2019
commit e723c3e5ab2d7af692d89587657ec67e0c64e137
49 changes: 24 additions & 25 deletions background-script.js
Original file line number Diff line number Diff line change
@@ -56,6 +56,11 @@ if (onFirefox()) {
communicationType = 'sync_over_http';
}

// Builds a match pattern for all HTTP URL's for the specified host
function buildPattern(host) {
return `http://${host}/*`;
}

function queryUpgradeNative(requestDetails, resolve) {
const url = new URL(requestDetails.url);
const host = url.host;
@@ -72,14 +77,6 @@ function queryUpgradeNative(requestDetails, resolve) {
pendingUpgradeChecks.get(host).add(resolve);
}

// upgradeAsync function returns a Promise
// which is resolved with the upgrade after the native DNSSEC app replies
function upgradeAsyncNative(requestDetails) {
return new Promise((resolve, reject) => {
queryUpgradeNative(requestDetails, resolve, reject);
});
}

// Adapted from Tagide/chrome-bit-domain-extension
// Returns true if timed out, returns false if hostname showed up
function sleep(milliseconds, queryFinishedRef) {
@@ -172,6 +169,14 @@ function upgradeSyncOverHttp(requestDetails) {
return buildBlockingResponse(url, upgrade, lookupError);
}

// upgradeAsync function returns a Promise
// which is resolved with the upgrade after the native DNSSEC app replies
function upgradeAsyncNative(requestDetails) {
return new Promise((resolve, reject) => {
queryUpgradeNative(requestDetails, resolve, reject);
});
}

function upgradeCompat(requestDetails) {
switch (communicationType) {
case 'native':
@@ -181,23 +186,6 @@ function upgradeCompat(requestDetails) {
}
}

// Builds a match pattern for all HTTP URL's for the specified host
function buildPattern(host) {
return `http://${host}/*`;
}

// Only use this on initial extension startup; afterwards you should use
// resetRequestListener instead.
function attachRequestListener() {
// add the listener,
// passing the filter argument and "blocking"
compatBrowser.webRequest.onBeforeRequest.addListener(
upgradeCompat,
{urls: [buildPattern(matchHost)]},
['blocking']
);
}

console.log(`Testing for Firefox: ${onFirefox()}`);

// Firefox is the only browser that supports async onBeforeRequest, and
@@ -230,4 +218,15 @@ if (communicationType === 'native') {
});
}

// Only use this on initial extension startup; afterwards you should use
// resetRequestListener instead.
function attachRequestListener() {
// add the listener,
// passing the filter argument and "blocking"
compatBrowser.webRequest.onBeforeRequest.addListener(
upgradeCompat,
{urls: [buildPattern(matchHost)]},
['blocking']
);
}
attachRequestListener();