Skip to content

Commit

Permalink
feat: add startup delay for ip check (#540)
Browse files Browse the repository at this point in the history
  • Loading branch information
sereneblue committed Aug 28, 2022
1 parent 52e87a4 commit e42874d
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 15 deletions.
3 changes: 3 additions & 0 deletions src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,9 @@
"text-searchRules": {
"message": "Search rules"
},
"text-startupDelay": {
"message": "Startup delay (sec)"
},
"text-timezone": {
"message": "Timezone"
},
Expand Down
6 changes: 4 additions & 2 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ let messageHandler = (request: any, sender: any, sendResponse: any) => {
sendResponse('done');
} else if (request.action === 'reloadIPInfo') {
if (chameleon.settings.options.timeZone === 'ip' || (chameleon.settings.headers.spoofAcceptLang.value === 'ip' && chameleon.settings.headers.spoofAcceptLang.enabled)) {
chameleon.updateIPInfo(request.data);
chameleon.updateIPInfo();
sendResponse('done');
}
} else if (request.action === 'reloadProfile') {
Expand Down Expand Up @@ -135,7 +135,9 @@ browser.runtime.onMessage.addListener(messageHandler);
await chameleon.init(await webext.getSettings(null));

if (chameleon.settings.options.timeZone === 'ip' || (chameleon.settings.headers.spoofAcceptLang.value === 'ip' && chameleon.settings.headers.spoofAcceptLang.enabled)) {
await chameleon.updateIPInfo(false);
setTimeout(() => {
chameleon.updateIPInfo();
}, chameleon.settings.config.reloadIPStartupDelay * 1000);
}

if (!!browser.privacy) {
Expand Down
14 changes: 6 additions & 8 deletions src/lib/chameleon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export class Chameleon {
cache: null,
lang: '',
tz: '',
updated: 0,
},
profile: '',
screenSize: '',
Expand Down Expand Up @@ -222,6 +221,8 @@ export class Chameleon {
}
}
}

this.settings.config.reloadIPStartupDelay = this.settings.config.reloadIPStartupDelay || 0;
}

public async init(storedSettings: any): Promise<void> {
Expand Down Expand Up @@ -805,16 +806,12 @@ export class Chameleon {
});
}

public async updateIPInfo(forceCheck: boolean): Promise<void> {
public async updateIPInfo(): Promise<void> {
try {
let notificationMsg: string;

// cache results for 1m
if (forceCheck || this.tempStore.ipInfo.updated + 60000 < new Date().getTime()) {
let res = await fetch('https://geoip-lookup.vercel.app/api/geoip');
this.tempStore.ipInfo.cache = await res.json();
this.tempStore.ipInfo.updated = new Date().getTime();
}
let res = await fetch('https://geoip-lookup.vercel.app/api/geoip');
this.tempStore.ipInfo.cache = await res.json();

let data = this.tempStore.ipInfo.cache;

Expand Down Expand Up @@ -1041,6 +1038,7 @@ export class Chameleon {
['config.notificationsEnabled', impSettings.config.notificationsEnabled, 'boolean'],
['config.theme', impSettings.config.theme, ['light', 'dark']],
['config.hasPrivacyPermission', impSettings.config.hasPrivacyPermission == undefined ? !!browser.privacy : impSettings.config.hasPrivacyPermission, 'boolean'],
['config.reloadIPStartupDelay', impSettings.config.reloadIPStartupDelay || 0, 'number'],
];

for (let i = 0; i < options.length; i++) {
Expand Down
20 changes: 20 additions & 0 deletions src/options/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,20 @@
<span v-t="'options-ipRules-reload.message'"></span>
</div>
</button>
<div class="flex gap-x-2 items-center">
<label for="config.reloadIPStartupDelay">
<span class="text-dark" v-t="'text-startupDelay.message'"></span>
</label>
<input
id="reloadIPStartupDelay"
@input="changeSetting($event)"
name="config.reloadIPStartupDelay"
v-model.number="settings.config.reloadIPStartupDelay"
type="number"
min="0"
class="block form-input"
/>
</div>
</div>
<div class="flex flex-wrap pb-12">
<table id="iprules" class="w-full">
Expand Down Expand Up @@ -535,6 +549,12 @@ export default class App extends Vue {
});
}
async changeSetting(evt: any): Promise<void> {
console.log(this.settings);
webext.sendToBackground(this.settings);
}
changeTab(tab: string): void {
window.location.hash = '#' + tab;
}
Expand Down
34 changes: 29 additions & 5 deletions src/popup/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,21 @@
<div>{{ currentProfile.timezone }}</div>
<div>{{ currentProfile.lang }}</div>
</div>
<div v-show="isRandomProfile" class="flex justify-center text-sm">
<div id="changeProfile" @click="changeProfile" class="rounded-lg cursor-pointer fg">
<div class="flex items-center px-2 py-1">
<feather type="refresh-cw" size="1em"></feather>
<span class="ml-2" v-t="'popup-home-change.message'"></span>
<div class="flex flex-col gap-y-2">
<div v-show="isRandomProfile" class="flex justify-center text-sm">
<div id="changeProfile" @click="changeProfile" class="rounded-lg cursor-pointer fg">
<div class="flex items-center px-2 py-1">
<feather type="refresh-cw" size="1em"></feather>
<span class="ml-2" v-t="'popup-home-change.message'"></span>
</div>
</div>
</div>
<div v-show="canReloadIP" class="flex justify-center text-sm">
<div id="reloadIPInfo" @click="reloadIPInfo" class="rounded-lg cursor-pointer fg">
<div class="flex items-center px-2 py-1">
<feather class="mr-2" type="refresh-cw" size="1em"></feather>
<span v-t="'options-ipRules-reload.message'"></span>
</div>
</div>
</div>
</div>
Expand Down Expand Up @@ -905,6 +915,14 @@ export default class App extends Vue {
}
}
get canReloadIP(): boolean {
if (this.settings.options.timeZone === 'ip' || (this.settings.headers.spoofAcceptLang.value === 'ip' && this.settings.headers.spoofAcceptLang.enabled)) {
return true;
}
return false;
}
get isRandomProfile(): boolean {
if (this.settings.profile.selected.includes('random') || ['windows', 'macOS', 'linux', 'iOS', 'android'].includes(this.settings.profile.selected)) {
return true;
Expand Down Expand Up @@ -1111,6 +1129,12 @@ export default class App extends Vue {
window.close();
}
reloadIPInfo(): void {
browser.runtime.sendMessage({
action: 'reloadIPInfo',
});
}
resizeProfileList(): void {
// @ts-ignore
let positionX: number = document.querySelector('ul').offsetTop + document.querySelector('ul').offsetHeight + 10;
Expand Down
1 change: 1 addition & 0 deletions src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default new Vuex.Store({
enabled: true,
notificationsEnabled: false,
theme: 'light',
reloadIPStartupDelay: 0,
},
excluded: [],
headers: {
Expand Down

0 comments on commit e42874d

Please sign in to comment.