Skip to content

Commit

Permalink
Merge pull request #45 from tmxkn1/stable-ua
Browse files Browse the repository at this point in the history
Stable ua
  • Loading branch information
Anthony Jiang committed Apr 21, 2022
2 parents edf9826 + 7e35ba1 commit 50899a1
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 116 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
.vscode
src.zip
node_modules
developer.json
19 changes: 18 additions & 1 deletion src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
function onExtensionLoad() {
setBadge(new GreyBadge());
loadSavedSettings();
getDeveloperSettings();
setDelayedInitialisation(5000);
}

Expand All @@ -14,6 +15,22 @@ function loadSavedSettings() {
});
}

async function getDeveloperSettings() {
const devJson = chrome.runtime.getURL('developer.json');
const fetchProm = await fetch(devJson, {method: 'GET'}).then((response) => {
return response.json();
}).then((json) => {
developer = json;
console.log('Developer mode enabled.');
console.log(developer);
}).catch((ex) => {
if (ex.name == 'TypeError') {
return;
}
throw ex;
});
}

// -----------------------------
// Work
// -----------------------------
Expand Down Expand Up @@ -47,7 +64,6 @@ async function doBackgroundWork() {

setBadge(new BusyBadge());

await updateUA();
checkNewDay();
await checkDailyRewardStatus();

Expand Down Expand Up @@ -104,6 +120,7 @@ const WAIT_FOR_ONLINE_TIMEOUT = 60000;
const googleTrend = new GoogleTrend();
const userDailyStatus = new DailyRewardStatus();
const searchQuest = new SearchQuest(googleTrend);
let developer = false;
let userAgents;
let _compatibilityMode;

Expand Down
24 changes: 14 additions & 10 deletions src/exception.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@ class FetchFailedException extends ErrorWithSourceInnerException {
}
}

class FetchRedirectedException extends ErrorWithSourceInnerException {
constructor(source, innerException, message) {
if (!message) {
message = 'Fetch failed because redirection occurred.';
}
super(source, innerException, message);
this.name = 'FetchRedirected';
}
}

class ResponseUnexpectedStatusException extends ErrorWithSourceInnerException {
constructor(source, response, message) {
if (!message) {
Expand Down Expand Up @@ -87,3 +77,17 @@ class FetchTimeoutException extends ErrorWithSourceInnerException {
this.name = 'FetchTimeout';
}
}

class UserAgentInvalidException extends Error {
constructor(message) {
super(message);
this.name = 'UserAgentInvalid';
}
}

class NotRewardUserException extends Error {
constructor(message) {
super(message);
this.name = 'UserNotLoggedIn';
}
}
8 changes: 5 additions & 3 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@

"permissions": ["https://www.bing.com/search?q=*",
"https://trends.google.com/trends/*",
"https://account.microsoft.com/rewards/pointsbreakdown*",
"https://rewards.microsoft.com/pointsbreakdown*",
"https://account.microsoft.com/rewards/*",
"https://rewards.microsoft.com/*",
"https://rewards.bing.com/*",
"webRequest",
"webRequestBlocking",
"storage"
"storage",
"notifications"
]
}
99 changes: 83 additions & 16 deletions src/quest/searchQuest.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,36 @@ class SearchQuest {
this._status_ = status;
this._jobStatus_ = STATUS_BUSY;
try {
await getStableUA();
await this._googleTrend_.getGoogleTrendWords();
await this._startSearchQuests();
await this._doWorkClosedLoop(status);
await this._doWorkLoop();
} catch (ex) {
this._jobStatus_ = STATUS_ERROR;
if (ex instanceof UserAgentInvalidException) {
notifyUpdatedUAOutdated();
}
throw ex;
}
}

async _doWorkClosedLoop(status) {
await status.update();
if (status.isSearchCompleted) {
return;
}
async _doWorkLoop() {
while (true) {
if (this._status_.isSearchCompleted) {
return;
}

if (status.jobStatus==STATUS_ERROR || !status.summary.isValid) {
this._jobStatus_ = STATUS_ERROR;
return;
}
if (this._status_.jobStatus == STATUS_ERROR || !this._status_.summary.isValid) {
this._jobStatus_ = STATUS_ERROR;
return;
}

await this._startSearchQuests();

await this._startSearchQuests();
await this._doWorkClosedLoop(status);
const flag = await this.isSearchSuccessful();
if (flag > 0) {
await this._getAlternativeUA(flag);
}
}
}

async _startSearchQuests() {
Expand All @@ -54,6 +62,36 @@ class SearchQuest {
this._quitSearchCleanUp();
}

async isSearchSuccessful() {
// Return:
// 0 - successful; 1 - pc search failed; 2 - mb search failed; 3 - both failed
const pcSearchProgBefore = this._status_.pcSearchStatus.progress;
const mbSearchProgBefore = this._status_.mbSearchStatus.progress;
await this._status_.update();
const flag = (!this._status_.pcSearchStatus.isValidAndCompleted && (pcSearchProgBefore == this._status_.pcSearchStatus.progress));
return flag + 2 * (!this._status_.mbSearchStatus.isValidAndCompleted && (mbSearchProgBefore == this._status_.mbSearchStatus.progress));
}

async _getAlternativeUA(flag) {
if (flag == 3) {
if (userAgents.pcSource == 'updated' && userAgents.mbSource == 'updated') {
throw new UserAgentInvalidException('Cannot find working UAs for pc and mobile.');
}
await getUpdatedUA('both');
} else if (flag == 1) {
if (userAgents.pcSource == 'updated') {
throw new UserAgentInvalidException('Cannot find a working UA for pc.');
}
await getUpdatedUA('pc');
} else if (flag == 2) {
if (userAgents.mbSource == 'updated') {
throw new UserAgentInvalidException('Cannot find a working UA for mobile.');
}
await getUpdatedUA('mb');
}
notifyStableUAOutdated(flag);
}

async _doPcSearch() {
this._initiateSearch();
if (this._currentSearchType_ != SEARCH_TYPE_PC_SEARCH) {
Expand Down Expand Up @@ -133,13 +171,12 @@ class SearchQuest {
}

function removeUA() {
// remove user-agent
try {
chrome.webRequest.onBeforeSendHeaders.removeListener(toMobileUA);
} catch (ex) {}
} catch (ex) { }
try {
chrome.webRequest.onBeforeSendHeaders.removeListener(toMsEdgeUA);
} catch (ex) {}
} catch (ex) { }
}

function setMsEdgeUA() {
Expand Down Expand Up @@ -182,6 +219,36 @@ function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

function notifyStableUAOutdated(flag) {
if (developer && developer.notification_ua_stable_outdated) {
const message = 'Stable UA is outdated! Flag: ' + (flag == 3 ? 'pc and mobile' : flag == 1 ? 'pc' : 'mobile');
console.log(message);
chrome.notifications.clear('stable_ua_outdated');
chrome.notifications.create('stable_ua_outdated', {
type: 'basic',
iconUrl: 'img/warn@8x.png',
title: 'Developer notification',
message: message,
priority: 2,
});
}
}

function notifyUpdatedUAOutdated() {
if (developer && developer.notification_ua_updated_outdated) {
const message = 'Critical!! Updated UA is outdated!';
console.log(message);
chrome.notifications.clear('updated_ua_outdated');
chrome.notifications.create('updated_ua_outdated', {
type: 'basic',
iconUrl: 'img/err@8x.png',
title: 'Developer notification',
message: message,
priority: 2,
});
}
}

const SEARCH_TYPE_PC_SEARCH = 0;
const SEARCH_TYPE_MB_SEARCH = 1;
const STATUS_NONE = 0;
Expand Down

0 comments on commit 50899a1

Please sign in to comment.