Skip to content

Commit 6ee7c10

Browse files
committed
ffix(grade.website): Refactor cloudopt.grade.website()
1. Take apart icon/notify logics from the function 2. Change classify rule of scores 3. Fix the icon color updating problem
1 parent 003b596 commit 6ee7c10

File tree

4 files changed

+89
-65
lines changed

4 files changed

+89
-65
lines changed

js/background/filter/download.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ if (chrome.downloads != undefined) {
77
cloudopt.config.refresh(function() {
88
cloudopt.grade.website(url, function(result) {
99
var config = cloudopt.config.get();
10-
if (result.safe < 0 && config.safeDownload) {
10+
if (result.safe === false && config.safeDownload) {
1111
cloudopt.notification.error(cloudopt.i18n.get("dangDownTitle"));
1212
chrome.downloads.cancel(downloadItem.id, function (url) { });
1313
};

js/cloudopt-core.js

Lines changed: 86 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ cloudopt.utils = (function (cloudopt) {
349349
function getRawUa() {
350350
return navigator.userAgent;
351351
}
352-
352+
353353

354354
return {
355355
getQueryString: getQueryString,
@@ -594,7 +594,7 @@ cloudopt.config = (function (cloudopt) {
594594
/** Local update time */
595595
var update_time = new Date().getTime();
596596

597-
/**
597+
/**
598598
* async save config
599599
*/
600600
function asyncSaveConfig(config) {
@@ -942,19 +942,57 @@ cloudopt.grade = (function (cloudopt) {
942942
isCreateNoty = true
943943
}
944944

945+
/**
946+
* Decide the secure level of a website
947+
*
948+
* @param result result of website()
949+
*/
950+
function classify(result) {
951+
var level = 0; /* no level */
952+
if (result.score >= 65) {
953+
level = 1; /* safe */
954+
} else if (result.score >= 60) {
955+
level = 2; /* normal */
956+
} else if (result.score >= 40) {
957+
level = 3; /* potential threat */
958+
} else {
959+
level = 4; /* dangerous */
960+
}
961+
return level;
962+
}
963+
964+
function isSafe(result) {
965+
var level = classify(result);
966+
var config = cloudopt.config.get();
967+
var safe = false;
968+
if (level <= 2) {
969+
safe = true;
970+
}
971+
if (level === 3 && config.safePotential) {
972+
safe = false;
973+
}
974+
if (config.whiteList.indexOf(result.host) > -1 || config.whiteListAds.indexOf(result.host) > -1) {
975+
safe = true;
976+
}
977+
if (config.blackList.some(e => e === result.host)) {
978+
safe == false;
979+
}
980+
981+
return safe;
982+
}
983+
945984
/**
946985
* Check the url and get the web data. Such as score ,type, host
947986
*
948987
* @param website url
949988
* @param callback callback function
950989
*/
951990
function website(website, callback) {
952-
cloudopt.browserIconChange.normal();
953991
var cacheSuffix = "_grade_1.0";
954992
website = cloudopt.utils.getHost(website);
955993
website = website + cacheSuffix;
956994
var result = {
957-
safe: 0,
995+
safe: true,
958996
type: "",
959997
date: new Date(),
960998
score: 0,
@@ -964,75 +1002,40 @@ cloudopt.grade = (function (cloudopt) {
9641002
return result;
9651003
}
9661004
result.host = website.replace(cacheSuffix, "");
1005+
9671006
cloudopt.store.get(website, function (item) {
9681007
if (item != undefined && JSON.stringify(item) != "{}" && cloudopt.utils.comparisonDate(item.date, defaultExpireTime.safeWebsite)) {
9691008
cloudopt.config.refresh(function () {
970-
var config = cloudopt.config.get();
971-
if (item.score < 60 && config.safePotential == true) {
972-
item.safe = -1;
973-
}
974-
if (item.score > 30 && config.safePotential == false) {
975-
item.safe = 0;
976-
}
977-
/*if in white list*/
978-
if (config.whiteList.indexOf(item.host) > -1 && item.safe < 0) {
979-
item.safe = 0;
980-
}
981-
if (config.whiteListAds.indexOf(item.host) > -1 && item.safe < 0) {
982-
item.safe = 0;
983-
}
984-
/*if in black list*/
985-
if (config.blackList.some(e => e === item.host)) {
986-
item.safe = -1;
987-
}
1009+
item.safe = isSafe(item);
9881010
callback(item);
989-
if (item.score < 60) cloudopt.browserIconChange.danger();
990-
labSafeTipsNoty(item.type)
9911011
});
9921012
return;
9931013
} else {
9941014
cloudopt.store.remove(website);
9951015
cloudopt.http.get(cloudopt.host + 'grade/website/' + website.replace(cacheSuffix, ''),{
9961016
timeout: 30000
9971017
}).carryApiKey().then(data=>{
998-
var config = cloudopt.config.get();
999-
if (data.result.score >= 60) {
1000-
result.safe = 1;
1001-
} else if (data.result.score < 60 && data.result.score >= 40 && config.safePotential == true) {
1002-
result.safe = -1;
1003-
} else if (data.result.score < 40) {
1004-
result.safe = -2;
1005-
} else {
1006-
result.safe = 0;
1007-
}
1018+
result.safe = isSafe(data.result);
10081019
result.type = data.result.type;
10091020
result.score = data.result.score;
1010-
/*if in white list*/
1011-
if (config.whiteList.indexOf(result.host) > -1 && result.safe == -1) {
1012-
result.safe = 0;
1013-
}
1014-
if (config.whiteListAds.indexOf(result.host) > -1 && result.safe == -1) {
1015-
result.safe = 0;
1016-
}
1021+
10171022
if (data.result.host != "") {
10181023
result.date = new Date().getTime();
10191024
cloudopt.store.set(website, result);
10201025
}
10211026
callback(result);
1022-
if (result.score < 60) cloudopt.browserIconChange.danger();
1023-
labSafeTipsNoty(result.type)
10241027
},error=>{
1025-
result.safe = 0;
1028+
result.safe = true;
10261029
callback(result);
1027-
labSafeTipsNoty(result.type)
10281030
})
10291031
}
10301032
});
1031-
10321033
}
10331034

10341035
return {
1035-
website: website
1036+
website: website,
1037+
classify: classify,
1038+
labSafeTipsNoty: labSafeTipsNoty
10361039
};
10371040

10381041
})(cloudopt);
@@ -1063,23 +1066,21 @@ cloudopt.init = (function (cloudopt) {
10631066

10641067
chrome.tabs.onActivated.addListener(function (activeInfo) {
10651068
chrome.tabs.query({ active: true, currentWindow: true }, function(tabArray) {
1066-
url = tabArray[tabArray.length - 1].url;
1067-
host = cloudopt.utils.getHost(url);
1068-
// cloudopt.browserIconChange.normal();
1069-
if (tabArray[tabArray.length - 1].url.indexOf("file://") == 0 || tabArray[tabArray.length - 1].url.indexOf("chrome-extension://") == 0 || tabArray[tabArray.length - 1].url.indexOf("chrome://") == 0 || cloudopt.config.get().whiteList.indexOf(host) > -1 || cloudopt.config.get().whiteListAds.indexOf(host) > -1) {
1070-
cloudopt.browserIconChange.gray();
1071-
return;
1072-
}
1073-
cloudopt.grade.website(url, function(result) {
1074-
if (result.score == 0 && result.safe == 0) {
1075-
cloudopt.browserIconChange.normal()
1076-
} else if (result.score < 60) {
1077-
cloudopt.browserIconChange.danger()
1078-
}
1079-
});
1069+
cloudopt.browserIconChange.auto(tabArray[tabArray.length - 1].url);
10801070
});
10811071
});
10821072

1073+
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
1074+
if (changeInfo.status != "loading") {
1075+
chrome.tabs.query({ active: true, currentWindow: true }, function(tabArray) {
1076+
cloudopt.browserIconChange.auto(tabArray[tabArray.length - 1].url);
1077+
cloudopt.grade.website(tabArray[tabArray.length - 1].url, function(result) {
1078+
cloudopt.grade.labSafeTipsNoty(result.type);
1079+
});
1080+
});
1081+
}
1082+
});
1083+
10831084
cloudopt.config.asyncAcquireConfig()
10841085

10851086
cloudopt.store.get("location", function (value) {
@@ -1155,11 +1156,34 @@ cloudopt.browserIconChange = (function (cloudopt) {
11551156
})
11561157
}
11571158

1159+
function auto(url) {
1160+
cloudopt.grade.website(url, function(result) {
1161+
if (url.indexOf("file://") == 0
1162+
|| url.indexOf("chrome-extension://") == 0
1163+
|| url.indexOf("chrome://") == 0) {
1164+
gray();
1165+
return;
1166+
}
1167+
1168+
var level = cloudopt.grade.classify(result);
1169+
if (result.safe === false) {
1170+
danger();
1171+
} else if (level == 3) {
1172+
gray();
1173+
} else if (level == 2) {
1174+
normal();
1175+
} else {
1176+
green();
1177+
}
1178+
});
1179+
}
1180+
11581181
return {
11591182
normal: normal,
11601183
gray: gray,
11611184
green: green,
1162-
danger: danger
1185+
danger: danger,
1186+
auto: auto
11631187
}
11641188
})(cloudopt);
11651189

js/cloudopt-popup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ cloudopt.onFinish(function() {
4444

4545
$(".score h1").text(result.score);
4646

47-
if (result.score == 0 && result.safe == 0) {
47+
if (result.score == 0 && result.safe) {
4848

4949
$(".score h1").text("?");
5050
$('#popupScoreButton').text(cloudopt.i18n.get("popupEvaluateButton"));

js/filter/website.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cloudopt.message.send("grade-website", window.location.href, function (result) {
22
var config = cloudopt.config.get();
3-
if (result.safe <= -1 && config.safeCloud == true) {
3+
if (result.safe === false && config.safeCloud == true) {
44
cloudopt.message.send("countEvent", "site-block");
55
cloudopt.message.send("countEvent", "site-block-today");
66
window.location.href = "https://www.cloudopt.net/block/" + encodeURIComponent(cloudopt.utils.getHost(window.location.href));

0 commit comments

Comments
 (0)