Skip to content

Commit

Permalink
Merge pull request #21 from IC-Naming/feature/1.0
Browse files Browse the repository at this point in the history
1.0 support icp production and ticp test net
  • Loading branch information
icnaming-roy committed Feb 25, 2022
2 parents b4f35ca + 0b98a90 commit fb220f1
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 384 deletions.
8 changes: 3 additions & 5 deletions src/public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
"manifest_version": 2,
"name": "IC naming extensions",
"description": "Extensions to help you visit icnaming from you browser",
"version": "0.3.2",
"version": "1.0.0",
"background": {
"page": "background.html",
"persistent": true
},
"browser_action": {
"default_popup": "popup.html"
},
"icons": {
"16": "icon.png",
"48": "icon.png",
Expand All @@ -19,6 +16,7 @@
"storage",
"webRequest",
"webRequestBlocking",
"*://*.icp/*"
"*://*.icp/*",
"*://*.ticp/*"
]
}
55 changes: 30 additions & 25 deletions src/src/background.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import { get_redirect_host } from "./utils/ic_naming";
import { get_redirect_host, NameEnv } from "./utils/ic_naming";

const rule_id_start = 1000;
let current_rule_id = rule_id_start;
let rules = {};

console.info("Background script loaded, current env: " + process.env.EXTENSION_ENV);

chrome.storage.onChanged.addListener(function(changes, namespace) {
if (namespace == "local" && changes.extension_env) {
rules = {};
current_rule_id = rule_id_start;
console.info("Extension env changed to " + changes.extension_env.newValue);
let env_rules = {};
let get_env_rules = (name_env: NameEnv) => {
if (!env_rules[name_env]) {
env_rules[name_env] = {};
}
});
return env_rules[name_env];
};


let save_rule = async (name: string, redirect_host: string): Promise<string> => {
let result = rules;
let save_rule = async (name: string, redirect_host: string, name_env: NameEnv): Promise<string> => {
let result = get_env_rules(name_env);
if (result[name]) {
let cache = result[name];
cache["redirect_host"] = redirect_host;
Expand All @@ -32,37 +28,38 @@ let save_rule = async (name: string, redirect_host: string): Promise<string> =>
return redirect_host;
};

let get_rule_host = (name: string): string => {
let result = rules;
let get_rule_host = (name: string, name_env: NameEnv): string => {
let result = get_env_rules(name_env);
if (result[name]) {
let cache = result[name];
return cache["redirect_host"];
}
return "";
};

let upsert_redirect_url = async (name: string): Promise<string> => {
let redirect_host = get_rule_host(name);
let upsert_redirect_url = async (name: string, name_env: NameEnv): Promise<string> => {
let redirect_host = get_rule_host(name, name_env);
if (redirect_host) {
console.log(`${name} already has a redirect host: ${redirect_host}`);
return redirect_host;
}
console.log(`${name} does not have a redirect host`);
redirect_host = await get_redirect_host(name);
redirect_host = await get_redirect_host(name, name_env);
if (redirect_host) {
console.log(`found ${name} has a redirect host from resolver: ${redirect_host}`);
await save_rule(name, redirect_host);
await save_rule(name, redirect_host, name_env);
console.log(`saved ${name} has a redirect host from resolver: ${redirect_host}`);
}
return redirect_host;
};

chrome.webRequest.onBeforeRequest.addListener(details => {

let urlHandler = (name_env: NameEnv, details: any) => {
console.log("onBeforeRequest", details);
let hostname = new URL(details.url).hostname;
console.log("onBeforeRequest", hostname);
if (hostname.endsWith(".icp")) {
let redirect_host = get_rule_host(hostname);
if (hostname.endsWith(".icp") || hostname.endsWith(".ticp")) {
let redirect_host = get_rule_host(hostname, name_env);
if (redirect_host) {
// replace the hostname with the redirect host
// and update schema to https
Expand All @@ -71,16 +68,24 @@ chrome.webRequest.onBeforeRequest.addListener(details => {
redirect_url.protocol = "https:";
return { redirectUrl: redirect_url.toString() };
} else {
upsert_redirect_url(hostname);
let result = upsert_redirect_url(hostname, name_env);
// redirect to redirect.html with query string

// get redirect.html url from extensions
let redirect_url_base = chrome.runtime.getURL("redirect.html");
let url = `${redirect_url_base}?source=${encodeURIComponent(details.url)}`;
let url = `${redirect_url_base}?source=${encodeURIComponent(details.url)}&env=${name_env}`;
return { redirectUrl: url };
}
}
};

chrome.webRequest.onBeforeRequest.addListener(details => {
return urlHandler(NameEnv.MainNet, details);
}, {
urls: ["*://*.icp/*"]
}, ["blocking"]);

chrome.webRequest.onBeforeRequest.addListener(details => {
return urlHandler(NameEnv.TestNet, details);
}, {
urls: ["*://*.ticp/*"]
}, ["blocking"]);
53 changes: 0 additions & 53 deletions src/src/popup.tsx

This file was deleted.

80 changes: 55 additions & 25 deletions src/src/redirect.tsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,70 @@
import React, { useEffect, useState } from "react";
import ReactDOM from "react-dom";
import "./skin/popup.css";
import { get_redirect_host } from "./utils/ic_naming";
import { load_env_with_call } from "./utils/config";
import { get_redirect_host, NameEnv } from "./utils/ic_naming";

const Popup = () => {
load_env_with_call(env => {
let default_url = "https://github.com/IC-Naming";
// get uri from query string
let url = new URL(window.location.href);
let source = url.searchParams.get("source");
if (!source) {
// redirect to default url
window.location.href = default_url;
} else {
// get hostname from uri
let source_url = new URL(source);
let hostname = source_url.hostname;
// get redirect url
get_redirect_host(hostname).then((redirect_host) => {
const [redirectUrl, setRedirectUrl] = useState<string>("");
const [redirectFound, setRedirectFound] = useState<boolean>(false);
const [loading, setLoading] = useState<boolean>(true);

let default_url = "https://app.icnaming.com/search/";
// get uri from query string
let url = new URL(window.location.href);
let source = url.searchParams.get("source");
let env = url.searchParams.get("env");
if (!source) {
// redirect to default url
window.location.href = default_url;
} else {
// get hostname from uri
let source_url = new URL(source);
let hostname = source_url.hostname;
// parse name_env to NameEnv
let name_env = NameEnv.MainNet;
if (env) {
name_env = (NameEnv)[env];
}
// get redirect url
get_redirect_host(hostname, name_env)
.then((redirect_host) => {
source_url.hostname = redirect_host;
source_url.protocol = "https:";
setRedirectUrl(source_url.href);
setRedirectFound(true);
window.location.href = source_url.href;
}).catch((error) => {
})
.catch((error) => {
console.log(error);
window.location.href = default_url;
// get first part of hostname
let hostname_parts = hostname.split(".");
let first_part = hostname_parts[0];
let url = `${default_url}${first_part}`;
setRedirectUrl(url);
setRedirectFound(false);
window.location.href = url;
})
.finally(() => {
setLoading(false);
});
}
});
}

return (
<>
<div>
loading...
</div>
</>
<div>
{loading && <div>Loading...</div>}
{!loading && redirectFound && (
<div>
<div>Name target found, redirecting to:</div>
<div>{redirectUrl}</div>
</div>
)}
{!loading && !redirectFound && (
<div>
<div>Name target not found, redirecting to:</div>
<div>{default_url}</div>
</div>
)}
</div>
);
};

Expand Down

0 comments on commit fb220f1

Please sign in to comment.