Skip to content

Commit f2375a3

Browse files
committed
add icon toggle for enable - disable
1 parent 69bd7f3 commit f2375a3

8 files changed

Lines changed: 54 additions & 17 deletions

File tree

bin/createConfig.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ if (!fs.existsSync(extDir)) {
1212
if (!fs.existsSync(extDir + '/scripts')) {
1313
fs.mkdirSync(extDir + '/scripts');
1414
}
15+
if (!fs.existsSync(extDir + '/assets')) {
16+
fs.mkdirSync(extDir + '/assets');
17+
}
1518

1619
fs.writeFileSync(process.cwd() + '/localhostify-ext/config.js', chromeConfig);
17-
fs.copyFileSync(__dirname + '/../chrome-ext/icon_128.png', process.cwd() + '/localhostify-ext/icon_128.png');
1820
fs.copyFileSync(__dirname + '/../chrome-ext/manifest.json', process.cwd() + '/localhostify-ext/manifest.json');
1921
fs.copyFileSync(__dirname + '/../chrome-ext/scripts/http.js', process.cwd() + '/localhostify-ext/scripts/http.js');
2022
fs.copyFileSync(__dirname + '/../chrome-ext/scripts/fetch.js', process.cwd() + '/localhostify-ext/scripts/fetch.js');
23+
fs.copyFileSync(__dirname + '/../chrome-ext/assets/green.png', process.cwd() + '/localhostify-ext/assets/green.png');
24+
fs.copyFileSync(__dirname + '/../chrome-ext/assets/red.png', process.cwd() + '/localhostify-ext/assets/red.png');
2125

2226
console.log("Localhostify: Chrome Ext created in localhostify-ext folder.");

chrome-ext/assets/green.png

4.08 KB
Loading

chrome-ext/assets/red.png

3.97 KB
Loading

chrome-ext/icon_128.png

-21.9 KB
Binary file not shown.

chrome-ext/manifest.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
{
22
"name": "Localhostify",
3-
"version": "0.0.9",
3+
"version": "0.1.0",
44
"manifest_version": 2,
55
"description": "Localhostify: External redirects interceptor",
66
"author": "Salvatore Ravidà",
77
"homepage_url": "https://github.com/salvoravida/localhostify",
8-
"icons": {
9-
"128": "icon_128.png"
10-
},
118
"background": {
129
"scripts": ["config.js", "scripts/http.js"]
1310
},
@@ -19,5 +16,11 @@
1916
"run_at": "document_start"
2017
}
2118
],
22-
"permissions": ["tabs", "webRequest", "webRequestBlocking", "<all_urls>"]
19+
"icons": {
20+
"128": "assets/green.png"
21+
},
22+
"browser_action": {
23+
"name": "Toggle Localhostify!"
24+
},
25+
"permissions": ["storage", "tabs", "webRequest", "webRequestBlocking", "<all_urls>"]
2326
}

chrome-ext/scripts/fetch.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function interceptor() {
1616
get: function () {
1717
let replacedResponse = response;
1818

19-
externalRedirects.forEach((rule) => {
19+
externalRedirects.forEach((rule) => {
2020
if (new RegExp(rule.match).test(replacedResponse)) {
2121
replacedResponse = replacedResponse.replace(rule.replace[0], rule.replace[1]);
2222
}
@@ -65,13 +65,17 @@ function interceptor() {
6565
};
6666
}
6767

68-
const interceptorScript = interceptor.toString();
69-
const config = 'window.__LOCALHOSTIFY__=' + JSON.stringify(window.__LOCALHOSTIFY__, 0, 2) + ';';
68+
chrome.storage.sync.get(['active'], function (data) {
69+
if (data.active) {
70+
const interceptorScript = interceptor.toString();
71+
const config = 'window.__LOCALHOSTIFY__=' + JSON.stringify(window.__LOCALHOSTIFY__, 0, 2) + ';';
7072

71-
const script = document.createElement('script');
72-
script.className = 'localhostify';
73-
script.type = 'text/javascript';
74-
script.appendChild(document.createTextNode(`${config} (${interceptorScript})()`));
73+
const script = document.createElement('script');
74+
script.className = 'localhostify';
75+
script.type = 'text/javascript';
76+
script.appendChild(document.createTextNode(`${config} (${interceptorScript})()`));
7577

76-
const parent = document.head || document.documentElement;
77-
parent.appendChild(script);
78+
const parent = document.head || document.documentElement;
79+
parent.appendChild(script);
80+
}
81+
});

chrome-ext/scripts/http.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,33 @@
44

55
const { externalRedirects } = window.__LOCALHOSTIFY__;
66

7+
let localhostifyEnabled = true;
8+
9+
chrome.runtime.onInstalled.addListener(function () {
10+
chrome.storage.sync.set({ active: localhostifyEnabled }, function () {
11+
console.log('Active :', localhostifyEnabled);
12+
});
13+
});
14+
15+
function onIconClick(toggle) {
16+
chrome.storage.sync.get(['active'], function (data) {
17+
const active = toggle ? !data.active : data.active;
18+
localhostifyEnabled = active;
19+
chrome.browserAction.setIcon({ path: active ? 'assets/green.png' : 'assets/red.png' });
20+
chrome.storage.sync.set({ active }, function () {
21+
console.log('Active 1:', active);
22+
});
23+
});
24+
}
25+
26+
chrome.browserAction.onClicked.addListener(() => onIconClick(true));
27+
28+
onIconClick();
29+
30+
//http intercept
731
chrome.webRequest.onHeadersReceived.addListener(
832
function (details) {
33+
if (!localhostifyEnabled) return;
934
const headers = details.responseHeaders;
1035

1136
for (var i = 0, l = headers.length; i < l; ++i) {

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "localhostify",
3-
"version": "0.0.9",
3+
"version": "0.1.0",
44
"description": "Easy HTTP reverse proxy - localhost:3001 -> prod.domain.com",
55
"source": "src/index.js",
66
"main": "./dist/index.js",
@@ -18,7 +18,8 @@
1818
"prettier": "2.4.1"
1919
},
2020
"scripts": {
21-
"build": "microbundle -i src/index.js -o dist/index.js --no-pkg-main -f cjs --no-sourcemap --compress"
21+
"build": "microbundle -i src/index.js -o dist/index.js --no-pkg-main -f cjs --no-sourcemap --compress",
22+
"chrome-ext": "node ./bin/createConfig.js --chrome-ext"
2223
},
2324
"repository": {
2425
"type": "git",

0 commit comments

Comments
 (0)