mirror of
https://gitee.com/willfree/MINResolveV2.git
synced 2026-10-05 01:15:47 +08:00
增加了协议头:min@;规范了background.js的代码
This commit is contained in:
1 parent
33b998ccda
commit
850a6e6fe3
1 file changed
+278
-396
+278
-396
@@ -11,9 +11,39 @@ var misHost="121.15.171.92";
|
||||
var misPrefix="/mis/resolve";
|
||||
// 兴趣包前缀序号
|
||||
var misPrefixNum=0;
|
||||
// 兴趣包过期时间:默认两秒
|
||||
var interestLifetimeMilliseconds=2000;
|
||||
// 是否开启mis更新映射表
|
||||
var misEnableState=true;
|
||||
|
||||
// min协议的URI协议头
|
||||
var minProtocolHeader="min@";
|
||||
// 适配的其它浏览器默认协议:
|
||||
// 插件将自动拦截该头部,以做协议转换
|
||||
var httpProtocolHeader="http://";
|
||||
var httpsProtocolHeader="https://";
|
||||
// 适配的搜索引擎
|
||||
var searchLibs=[
|
||||
// 百度
|
||||
"https://www.baidu.com",
|
||||
"http://www.baidu.com",
|
||||
// 谷歌
|
||||
"https://www.google.com",
|
||||
"http://www.google.com",
|
||||
// 搜狗
|
||||
"https://www.sogou.com",
|
||||
"http://www.sogou.com",
|
||||
// 360
|
||||
"https://www.so.com",
|
||||
"http://www.so.com",
|
||||
// bing
|
||||
"https://cn.bing.com",
|
||||
"http://cn.bing.com",
|
||||
// bing
|
||||
"https://www.bing.com",
|
||||
"http://www.bing.com"
|
||||
];
|
||||
|
||||
// 获取兴趣包前缀
|
||||
function getInterestPrefix() {
|
||||
misPrefixNum++;
|
||||
@@ -73,10 +103,8 @@ function reload(result) {
|
||||
|
||||
/* Auto reload */
|
||||
browser.storage.onChanged.addListener(function (changes, area) {
|
||||
// if (area == "local") {
|
||||
load("storage", reload);
|
||||
loadMisHost();
|
||||
// }
|
||||
});
|
||||
|
||||
|
||||
@@ -97,15 +125,9 @@ function getJsonString(u_identifier){
|
||||
}
|
||||
|
||||
|
||||
function onData(interest, data)
|
||||
{
|
||||
// printLine("Got data packet with name " + data.getName().toUri());
|
||||
// printLine(data.getContent().buf().toString('binary'));
|
||||
}
|
||||
|
||||
function onTimeout(interest)
|
||||
{
|
||||
// printLine("Time out for interest " + interest.getName().toUri());
|
||||
console.log("time out...");
|
||||
}
|
||||
|
||||
// 增加映射表项时,判断该表项是否重复
|
||||
@@ -124,22 +146,21 @@ function addCustomRule(editingRule) {
|
||||
|
||||
function express(prefixUri) {
|
||||
// alert("成功加载mis的IP: "+misHost+"; misPrefix: "+misPrefix);
|
||||
var host = misHost;
|
||||
// Connect to the forwarder with a WebSocket.
|
||||
var face = new Face({host: host});
|
||||
var face = new Face({host: misHost});
|
||||
|
||||
var name = new Name(getInterestPrefix());
|
||||
var jsonPrefix=getJsonString(prefixUri);
|
||||
|
||||
var interest=new Interest(name);
|
||||
interest.setMustBeFresh(true);
|
||||
var jsonPrefix=getJsonString(prefixUri);
|
||||
interest.setApplicationParameters(new Blob(jsonPrefix));
|
||||
interest.setInterestLifetimeMilliseconds(2000);
|
||||
interest.setInterestLifetimeMilliseconds(interestLifetimeMilliseconds);
|
||||
// alert("express Interest: "+jsonPrefix);
|
||||
// printLine("Express interest " + interest.getName().toUri());
|
||||
// printLine("The json string is "+interest.getApplicationParameters().toString());
|
||||
|
||||
face.expressInterest(interest, function(interest,data){
|
||||
var targetJsonString=data.getContent().buf().toString('binary');
|
||||
// alert("targetJsonString: "+targetJsonString);
|
||||
alert("targetJsonString: "+targetJsonString);
|
||||
var targetJson=JSON.parse(targetJsonString);
|
||||
// 将targetJson解析成targetUri
|
||||
var StatusCode=targetJson.StatusCode;
|
||||
@@ -167,145 +188,135 @@ function express(prefixUri) {
|
||||
}, onTimeout);
|
||||
}
|
||||
|
||||
/* ini storage based on the MIS System */
|
||||
// function iniStorageBasedMIS() {
|
||||
// // alert("really begin");
|
||||
// var prefixUri="/mis/baidu";
|
||||
// express(prefixUri);
|
||||
// /* Download online rules */
|
||||
// function downloadOnlineURLs() {
|
||||
// /* Still downloading */
|
||||
// if (!storage.onlineURLs || storage.onlineURLs.length <=0 || downloading) {
|
||||
// return;
|
||||
// }
|
||||
// downloading = true;
|
||||
// var toDownload = []; // URLs that need to be downloaded
|
||||
// for (var i = 0; i < storage.onlineURLs.length; i++) {
|
||||
// var onlineURL = storage.onlineURLs[i];
|
||||
// if (onlineURL.auto && onlineURL.enable && onlineURL.url !== "") {
|
||||
// toDownload.push(onlineURL.url);
|
||||
// }
|
||||
// }
|
||||
// if (toDownload.length === 0) {
|
||||
// sendMessage("downloaded");
|
||||
// }
|
||||
// var downloadErrors = [];
|
||||
// var parseErrors = [];
|
||||
// for (var i = 0; i<toDownload.length; i++) {
|
||||
// download(toDownload[i], function (url, content) {
|
||||
// toDownload.remove(url);
|
||||
// if (content) {
|
||||
// try {
|
||||
// var json;
|
||||
// // content may be a string or json object
|
||||
// if (typeof content === "string") {
|
||||
// json = JSON.parse(content);
|
||||
// } else {
|
||||
// json = content;
|
||||
// }
|
||||
// json.url = url;
|
||||
// json.enable = true;
|
||||
// json.downloadAt = new Date();
|
||||
// /* To compact with gooreplacer */
|
||||
// /* version < 1.0, version >= 1.0 doesn't change */
|
||||
// if (!json.version || json.version < "1.0") {
|
||||
// var rules = [];
|
||||
// for (var key in json.rules) {
|
||||
// var rule = json.rules[key];
|
||||
// rules.push({
|
||||
// origin: key,
|
||||
// target: rule.dstURL,
|
||||
// enable: rule.enable === undefined ? true : rule.enable,
|
||||
// kind: rule.kind
|
||||
// });
|
||||
// }
|
||||
// json.rules = rules;
|
||||
// }
|
||||
// /* replace the onlineURL */
|
||||
// for (var j = 0; j < storage.onlineURLs.length; j++){
|
||||
// if (storage.onlineURLs[j].url == url){
|
||||
// storage.onlineURLs[j] = new OnlineURL();
|
||||
// storage.onlineURLs[j].fromObject(json);
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// } catch (err) {
|
||||
// parseErrors.push(url);
|
||||
// console.error(err);
|
||||
// }
|
||||
// }
|
||||
// else {
|
||||
// downloadErrors.push(url);
|
||||
// }
|
||||
// if (toDownload.length <= 0) {
|
||||
// downloading = false;
|
||||
// sendMessage("downloaded");
|
||||
// if (downloadErrors.length > 0) {
|
||||
// sendMessage("downloadError", downloadErrors);
|
||||
// }
|
||||
// if (parseErrors.length > 0) {
|
||||
// sendMessage("parseError", parseErrors);
|
||||
// }
|
||||
// storage.updatedAt = (new Date()).toISOString();
|
||||
// save({"storage": storage});
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /* Reset download timer to download online urls */
|
||||
// /* Setup a listener if alarm api supported */
|
||||
// if (browser.alarms) {
|
||||
// browser.alarms.onAlarm.addListener(function (alarm) {
|
||||
// if (alarm.name == "download") {
|
||||
// downloadOnlineURLs();
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// /* Reset the download timer */
|
||||
// function resetDownloadTimer() {
|
||||
// var interval = 900; // Default 15min
|
||||
// if (storage.updateInterval) {
|
||||
// interval = parseInt(storage.updateInterval);
|
||||
// }
|
||||
// /* If the browser support alarms api */
|
||||
// if (browser.alarms) {
|
||||
// browser.alarms.create("download", {
|
||||
// periodInMinutes: Math.ceil(interval / 60)
|
||||
// });
|
||||
// }
|
||||
// else {
|
||||
// /* With setInterval */
|
||||
// if (downloadTimer != null) {
|
||||
// clearInterval(downloadTimer);
|
||||
// }
|
||||
// interval = interval * 1000; // In milli-seconds
|
||||
// downloadTimer = setInterval(function () {
|
||||
// if (storage.enable) {
|
||||
// downloadOnlineURLs();
|
||||
// }
|
||||
// }, interval);
|
||||
// }
|
||||
// }
|
||||
|
||||
// alert("begin reset");
|
||||
// iniStorageBasedMIS();
|
||||
|
||||
/* Download online rules */
|
||||
function downloadOnlineURLs() {
|
||||
/* Still downloading */
|
||||
if (!storage.onlineURLs || storage.onlineURLs.length <=0 || downloading) {
|
||||
return;
|
||||
}
|
||||
downloading = true;
|
||||
var toDownload = []; // URLs that need to be downloaded
|
||||
for (var i = 0; i < storage.onlineURLs.length; i++) {
|
||||
var onlineURL = storage.onlineURLs[i];
|
||||
if (onlineURL.auto && onlineURL.enable && onlineURL.url !== "") {
|
||||
toDownload.push(onlineURL.url);
|
||||
}
|
||||
}
|
||||
if (toDownload.length === 0) {
|
||||
sendMessage("downloaded");
|
||||
}
|
||||
var downloadErrors = [];
|
||||
var parseErrors = [];
|
||||
for (var i = 0; i<toDownload.length; i++) {
|
||||
download(toDownload[i], function (url, content) {
|
||||
toDownload.remove(url);
|
||||
if (content) {
|
||||
try {
|
||||
var json;
|
||||
// content may be a string or json object
|
||||
if (typeof content === "string") {
|
||||
json = JSON.parse(content);
|
||||
} else {
|
||||
json = content;
|
||||
}
|
||||
json.url = url;
|
||||
json.enable = true;
|
||||
json.downloadAt = new Date();
|
||||
/* To compact with gooreplacer */
|
||||
/* version < 1.0, version >= 1.0 doesn't change */
|
||||
if (!json.version || json.version < "1.0") {
|
||||
var rules = [];
|
||||
for (var key in json.rules) {
|
||||
var rule = json.rules[key];
|
||||
rules.push({
|
||||
origin: key,
|
||||
target: rule.dstURL,
|
||||
enable: rule.enable === undefined ? true : rule.enable,
|
||||
kind: rule.kind
|
||||
});
|
||||
}
|
||||
json.rules = rules;
|
||||
}
|
||||
/* replace the onlineURL */
|
||||
for (var j = 0; j < storage.onlineURLs.length; j++){
|
||||
if (storage.onlineURLs[j].url == url){
|
||||
storage.onlineURLs[j] = new OnlineURL();
|
||||
storage.onlineURLs[j].fromObject(json);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
parseErrors.push(url);
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
else {
|
||||
downloadErrors.push(url);
|
||||
}
|
||||
if (toDownload.length <= 0) {
|
||||
downloading = false;
|
||||
sendMessage("downloaded");
|
||||
if (downloadErrors.length > 0) {
|
||||
sendMessage("downloadError", downloadErrors);
|
||||
}
|
||||
if (parseErrors.length > 0) {
|
||||
sendMessage("parseError", parseErrors);
|
||||
}
|
||||
storage.updatedAt = (new Date()).toISOString();
|
||||
save({"storage": storage});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/* Reset download timer to download online urls */
|
||||
/* Setup a listener if alarm api supported */
|
||||
if (browser.alarms) {
|
||||
browser.alarms.onAlarm.addListener(function (alarm) {
|
||||
if (alarm.name == "download") {
|
||||
downloadOnlineURLs();
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/* Reset the download timer */
|
||||
function resetDownloadTimer() {
|
||||
var interval = 900; // Default 15min
|
||||
if (storage.updateInterval) {
|
||||
interval = parseInt(storage.updateInterval);
|
||||
}
|
||||
/* If the browser support alarms api */
|
||||
if (browser.alarms) {
|
||||
browser.alarms.create("download", {
|
||||
periodInMinutes: Math.ceil(interval / 60)
|
||||
});
|
||||
}
|
||||
else {
|
||||
/* With setInterval */
|
||||
if (downloadTimer != null) {
|
||||
clearInterval(downloadTimer);
|
||||
}
|
||||
interval = interval * 1000; // In milli-seconds
|
||||
downloadTimer = setInterval(function () {
|
||||
if (storage.enable) {
|
||||
downloadOnlineURLs();
|
||||
}
|
||||
}, interval);
|
||||
}
|
||||
}
|
||||
|
||||
/* Handle runtime messages */
|
||||
browser.runtime.onMessage.addListener(function (message, sender, sendResponse) {
|
||||
if (message.method == "download" && !downloading) {
|
||||
downloadOnlineURLs();
|
||||
}
|
||||
else if (message.method == "isDownloading") {
|
||||
sendResponse(downloading);
|
||||
}
|
||||
else {
|
||||
console.error("Unknown method");
|
||||
}
|
||||
});
|
||||
// browser.runtime.onMessage.addListener(function (message, sender, sendResponse) {
|
||||
// if (message.method == "download" && !downloading) {
|
||||
// downloadOnlineURLs();
|
||||
// }
|
||||
// else if (message.method == "isDownloading") {
|
||||
// sendResponse(downloading);
|
||||
// }
|
||||
// else {
|
||||
// console.error("Unknown method");
|
||||
// }
|
||||
// });
|
||||
|
||||
function redirect(details) {
|
||||
if (storage.enable && details.url) {
|
||||
@@ -321,28 +332,17 @@ function redirect(details) {
|
||||
|
||||
var _redirect_promise_supported = false;
|
||||
|
||||
// 判断某个字符串是否是一个搜索url
|
||||
// 对小于0的数取0
|
||||
function getPositiveNum(num) {
|
||||
if(num>0){
|
||||
return num;
|
||||
}else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// 判断某个字符串是否是一个搜索url:搜索某个内容的URI
|
||||
function isSearchUrl(urlString){
|
||||
var searchLibs=[
|
||||
// 百度
|
||||
"https://www.baidu.com",
|
||||
"http://www.baidu.com",
|
||||
// 谷歌
|
||||
"https://www.google.com",
|
||||
"http://www.google.com",
|
||||
// 搜狗
|
||||
"https://www.sogou.com",
|
||||
"http://www.sogou.com",
|
||||
// 360
|
||||
"https://www.so.com",
|
||||
"http://www.so.com",
|
||||
// bing
|
||||
"https://cn.bing.com",
|
||||
"http://cn.bing.com",
|
||||
// bing
|
||||
"https://www.bing.com",
|
||||
"http://www.bing.com"
|
||||
];
|
||||
var flag=0;
|
||||
for(var i=0;i<searchLibs.length;i++){
|
||||
if(urlString.indexOf(searchLibs[i])>=0){
|
||||
@@ -364,16 +364,8 @@ function isSearchUrl(urlString){
|
||||
}
|
||||
}
|
||||
|
||||
// 对小于0的数取0
|
||||
function getPositiveNum(num) {
|
||||
if(num>0){
|
||||
return num;
|
||||
}else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// 传入一个搜索uri,获取其中的搜索数值
|
||||
// 传入一个搜索uri,获取其中的搜索数值;
|
||||
// 传入一个非搜索uri,返回空字符串
|
||||
function getSearchContent(uriString) {
|
||||
if(!isSearchUrl(uriString)){
|
||||
return "";
|
||||
@@ -447,133 +439,101 @@ function getSearchContent(uriString) {
|
||||
}
|
||||
}else{}
|
||||
return decodeURIComponent(content);
|
||||
// return content.replace(/%2F/g,'/');
|
||||
}
|
||||
|
||||
// 判断某个字符串是否包含映射表中的表项
|
||||
function isIncludedRules(urlString) {
|
||||
var flag=0;
|
||||
for (var i=0; i<storage.customRules.length; i++) {
|
||||
var rule = storage.customRules[i];
|
||||
var afterDecodeOrigin=rule.origin.replace('/','%2F');
|
||||
if ((urlString.indexOf(rule.origin)>=0)||(urlString.indexOf(afterDecodeOrigin)>=0)) {
|
||||
flag=1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(flag===0){
|
||||
// alert("it do not includes rules");
|
||||
return false;
|
||||
}else{
|
||||
// alert("it includes rules");
|
||||
// console.log("it includes rules");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// 模糊判断一个string是否是一个合法前缀
|
||||
function isRightfulPrefix(urlString){
|
||||
if(urlString===''){
|
||||
// 模糊判断一个string是否是一个合法前缀/合法标识
|
||||
function isRightfulPrefix(string){
|
||||
// 不能为空
|
||||
if(string===''){
|
||||
return false;
|
||||
}
|
||||
if(urlString.indexOf('.')>=0){
|
||||
// 不能包含.
|
||||
if(string.indexOf('.')>=0){
|
||||
return false;
|
||||
}
|
||||
if((urlString.indexOf('/')!==0)&&(urlString.indexOf('%2F')!==0)){
|
||||
// alert("没/");
|
||||
// 第一个字符必须是/
|
||||
var stringDecoded=decodeURIComponent(string);
|
||||
if(stringDecoded.indexOf('/')!==0){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// 判断某个字符串是否是映射表的一个前缀表项
|
||||
// 判断某个字符串是否包含映射表中的表项
|
||||
function isRulesPrefix(urlString) {
|
||||
if(!isRightfulPrefix(urlString)){
|
||||
// 判断一个url是否是一个min协议的合法URI
|
||||
function isRightfulMinURI(urlString){
|
||||
var urlStringDecoded=decodeURIComponent(urlString);
|
||||
// 长度要大于min协议头的长度
|
||||
if(urlStringDecoded.length<=minProtocolHeader.length){
|
||||
return false;
|
||||
}
|
||||
// 再遍历本地映射表
|
||||
// 前几个字符必须是min协议头
|
||||
if(urlStringDecoded.indexOf(minProtocolHeader)!==0){
|
||||
return false;
|
||||
}
|
||||
// 协议头后面跟的是一个合法前缀/合法标识
|
||||
var prefix=urlStringDecoded.substring(minProtocolHeader.length);
|
||||
if(!isRightfulPrefix(prefix)){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// 从一个合法minUri提取出前缀
|
||||
function getPrefixFromMinURI(minURI){
|
||||
if(isRightfulMinURI(minURI)){
|
||||
var urlStringDecoded=decodeURIComponent(minURI);
|
||||
return urlStringDecoded.substring(minProtocolHeader.length);
|
||||
}else{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
// 判断某个没使用url编码的string是否是映射表的一个表项
|
||||
function isCustomRulesItemString(string) {
|
||||
// 遍历本地映射表
|
||||
var flag=0;
|
||||
for (var i=0; i<storage.customRules.length; i++) {
|
||||
var rule = storage.customRules[i];
|
||||
var afterDecodeOrigin=rule.origin.replace('/','%2F');
|
||||
if ((urlString===rule.origin)||(urlString===afterDecodeOrigin)) {
|
||||
if (string===rule.origin) {
|
||||
flag=1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(flag===0){
|
||||
// alert("it do not includes rules");
|
||||
// alert("it is not rules: "+string);
|
||||
return false;
|
||||
}else{
|
||||
// alert("it includes rules");
|
||||
// alert("it is rules: "+string);
|
||||
// console.log("it includes rules");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// 判断某个字符串是在搜索映射表表项
|
||||
function isSearchForRules(urlString) {
|
||||
if(isSearchUrl(urlString)&&isIncludedRules(urlString)){
|
||||
// console.log("it is searching for rules!!!");
|
||||
return true;
|
||||
}else{
|
||||
// 判断某个minUrl中的前缀是否是映射表的一个表项
|
||||
function isCustomRulesItemMinURI(minURI) {
|
||||
// 如果不是min协议URI,返回false
|
||||
var prefix=getPrefixFromMinURI(minURI);
|
||||
if(prefix===""){
|
||||
return false;
|
||||
}
|
||||
// alert("是minuri");
|
||||
return isCustomRulesItemString(prefix);
|
||||
}
|
||||
|
||||
// 获取url中包括的rule.origin
|
||||
function getIncludedRules(urlString) {
|
||||
var flag=0;
|
||||
var originString="";
|
||||
for (var i=0; i<storage.customRules.length; i++) {
|
||||
var rule = storage.customRules[i];
|
||||
var afterDecodeOrigin=rule.origin.replace('/','%2F');
|
||||
if ((urlString.indexOf(rule.origin)>=0)||(urlString.indexOf(afterDecodeOrigin)>=0)) {
|
||||
flag=1;
|
||||
originString=rule.origin;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return originString;
|
||||
}
|
||||
|
||||
// 剪切不包含.这个字符的URI的http头
|
||||
function cutHttpHead(uri) {
|
||||
// 判断是http:还是https:
|
||||
var startIndex=0;
|
||||
if(uri.indexOf("https:")>=0){
|
||||
startIndex=8;
|
||||
}else if(uri.indexOf("http:")>=0){
|
||||
startIndex=7;
|
||||
}
|
||||
// 判断uri中是否含有.这个字符
|
||||
if(uri.indexOf('.')>=0){
|
||||
return "";
|
||||
}else{
|
||||
var uritmp=uri.substr(startIndex);
|
||||
if(uritmp.indexOf(0)==='/'){
|
||||
return decodeURIComponent(uritmp);
|
||||
}else{
|
||||
return decodeURIComponent('/'+uritmp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 根据传入的uri获取前缀,从而更新本地映射表
|
||||
function updateStorageBasedUri(uri) {
|
||||
// alert("begin upgrate");
|
||||
var uriCut=cutHttpHead(uri);
|
||||
// alert("uricut: "+uriCut);
|
||||
if(uriCut===""){
|
||||
}else{
|
||||
express(uriCut);
|
||||
}
|
||||
}
|
||||
|
||||
// 根据传入的前缀,更新本地映射表
|
||||
// 根据传入的前缀/标识,更新本地映射表
|
||||
function updateStorageBasedPrefix(prefix) {
|
||||
express(prefix);
|
||||
if(isRightfulPrefix(prefix)){
|
||||
express(prefix);
|
||||
}
|
||||
}
|
||||
|
||||
// 根据传入的MINuri获取前缀,从而更新本地映射表
|
||||
function updateStorageBasedMinURI(minURI) {
|
||||
// alert("begin upgrate");
|
||||
var prefix=getPrefixFromMinURI(minURI);
|
||||
if(!(prefix==="")){
|
||||
express(prefix);
|
||||
}
|
||||
}
|
||||
|
||||
function sleep(numberMillis) {
|
||||
@@ -588,115 +548,46 @@ function sleep(numberMillis) {
|
||||
}
|
||||
}
|
||||
|
||||
// 将一个http或https请求的协议头去掉
|
||||
function getCutHttpHeader(urlString){
|
||||
var urlString=decodeURIComponent(urlString);
|
||||
if(urlString.indexOf(httpProtocolHeader)===0){
|
||||
return urlString.substring(httpProtocolHeader.length);
|
||||
}else if(urlString.indexOf(httpsProtocolHeader)===0){
|
||||
return urlString.substring(httpsProtocolHeader.length);
|
||||
}else{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/* Handle redirect */
|
||||
function handleRedirect(details) {
|
||||
// alert(details.url);
|
||||
// 判断url是否是一个搜索url,并根据搜索内容,判断是否直接命中本地映射表
|
||||
var searchContent=getSearchContent(details.url);
|
||||
// alert("sc: "+searchContent);
|
||||
// 判断url去掉头部之后是否直接命中本地映射表
|
||||
var cutHeadString=cutHttpHead(details.url);
|
||||
// alert(cutHeadString);
|
||||
var searchContent=getSearchContent(details.url); // 已解码
|
||||
var minUri=getCutHttpHeader(details.url); //已解码
|
||||
// alert("?s: "+searchContent+" ; ?u: "+minUri);
|
||||
// 如果该搜索内容或网址命中了缓存中的映射表前缀,则重定向
|
||||
if(isRulesPrefix(searchContent)||isRulesPrefix(cutHeadString)){
|
||||
alert("sc: "+searchContent+" isRules: "+isRulesPrefix(searchContent));
|
||||
alert("cut: "+cutHeadString+" isRules: "+isRulesPrefix(cutHeadString));
|
||||
if(isCustomRulesItemMinURI(searchContent)||isCustomRulesItemMinURI(minUri)){
|
||||
// alert("s: "+searchContent+" ; u: "+minUri);
|
||||
var prefixTemp="";
|
||||
if(searchContent!==""){
|
||||
prefixTemp=searchContent;
|
||||
prefixTemp=getPrefixFromMinURI(searchContent);
|
||||
}else{
|
||||
prefixTemp=cutHeadString;
|
||||
}
|
||||
details.url = "http://" + prefixTemp;
|
||||
// alert("detail-url: "+details.url);
|
||||
if (_redirect_promise_supported) {
|
||||
var promise = new Promise(function (resolve, reject) {
|
||||
var blockingResponse = redirect(details);
|
||||
return resolve(blockingResponse);
|
||||
});
|
||||
return promise;
|
||||
} else {
|
||||
// alert("redirect: "+details.url);
|
||||
return redirect(details);
|
||||
prefixTemp=getPrefixFromMinURI(minUri);
|
||||
}
|
||||
details.url = prefixTemp; //映射表中加了http协议头,这里就不要加了!!!
|
||||
return redirect(details);
|
||||
}
|
||||
// 如果没有命中缓存映射表,但是它是一个检索内容的网址,
|
||||
// 则根据该内容更新本地缓存之后,进行重定向
|
||||
// 如果没有命中缓存映射表,但是它是一个检索内容的网络请求
|
||||
if(searchContent!==""){
|
||||
// alert("检索内容了呢:"+searchContent);
|
||||
if ((misEnableState === true)&&isRightfulPrefix(searchContent)) {
|
||||
details.url = "http://" + searchContent;
|
||||
if ((misEnableState === true)&&isRightfulMinURI(searchContent)) {
|
||||
details.url = httpProtocolHeader + searchContent;
|
||||
// 如果检索内容模糊判断是一个前缀,且mis同步是开着的
|
||||
// alert("成功加载mis的IP: "+misHost+"; misPrefix: "+misPrefix+", searchContent: "+searchContent);
|
||||
var host = misHost;
|
||||
// Connect to the forwarder with a WebSocket.
|
||||
var face = new Face({host: host});
|
||||
|
||||
var name = new Name(getInterestPrefix());
|
||||
var interest=new Interest(name);
|
||||
interest.setMustBeFresh(true);
|
||||
var jsonPrefix=getJsonString(searchContent);
|
||||
interest.setApplicationParameters(new Blob(jsonPrefix));
|
||||
interest.setInterestLifetimeMilliseconds(2000);
|
||||
// alert("express Interest");
|
||||
face.expressInterest(interest, function(interest,data){
|
||||
var targetJsonString=data.getContent().buf().toString('binary');
|
||||
// alert("targetJsonString: "+targetJsonString);
|
||||
var targetJson=JSON.parse(targetJsonString);
|
||||
// 将targetJson解析成targetUri
|
||||
var StatusCode=targetJson.StatusCode;
|
||||
var targetUri=targetJson.Message;
|
||||
// alert("code: "+StatusCode+", "+targetUri);
|
||||
if((StatusCode===200)&&(targetUri!=="")) {
|
||||
var editingRule = new Rule();
|
||||
editingRule.enable = true;
|
||||
editingRule.description = "initial based on MIS";
|
||||
editingRule.origin = searchContent;
|
||||
editingRule.target = targetUri;
|
||||
editingRule.exclude = "";
|
||||
editingRule.example = "";
|
||||
editingRule.process = "";
|
||||
editingRule.methods = [];
|
||||
editingRule.types = [];
|
||||
// storage.customRules.push(editingRule);
|
||||
addCustomRule(editingRule);
|
||||
/* save */
|
||||
save({"storage": storage});
|
||||
load("storage", reload);
|
||||
// alert("已经重新更新了本地映射表");
|
||||
// 缓存映射表更新之后,执行重定向操作
|
||||
// details.url = "http://" + searchContent;
|
||||
// window.location.replace("http://www.runoob.com");
|
||||
// window.location.href=details.url;
|
||||
// alert("刷新到:"+details.url);
|
||||
}
|
||||
// alert("根据本地缓存要重定向了哦");
|
||||
if (_redirect_promise_supported) {
|
||||
var promise = new Promise(function (resolve, reject) {
|
||||
var blockingResponse = redirect(details);
|
||||
return resolve(blockingResponse);
|
||||
});
|
||||
return promise;
|
||||
} else {
|
||||
// alert("deurl: "+details.url);
|
||||
return redirect(details);
|
||||
}
|
||||
}, function (interest) {
|
||||
// alert("time out...");
|
||||
// mis系统崩溃,没有给任何应答,直接执行重定向操作
|
||||
if (_redirect_promise_supported) {
|
||||
var promise = new Promise(function (resolve, reject) {
|
||||
var blockingResponse = redirect(details);
|
||||
return resolve(blockingResponse);
|
||||
});
|
||||
return promise;
|
||||
} else {
|
||||
return redirect(details);
|
||||
}
|
||||
});
|
||||
return {
|
||||
redirectUrl: details.url
|
||||
};
|
||||
updateStorageBasedMinURI(minUri);
|
||||
return redirect(details);
|
||||
}else{
|
||||
// 如果是搜索了一个内容,该内容没命中缓存,
|
||||
// 且mis没开,或该内容不符合前缀标识符表示规则,
|
||||
@@ -704,35 +595,26 @@ function handleRedirect(details) {
|
||||
return {};
|
||||
}
|
||||
}else{
|
||||
// 如果不是进行搜索,且本地没有命中映射表,则异步请求mis更新映射表
|
||||
if((misEnableState===true)&&isRightfulPrefix(cutHeadString)){
|
||||
updateStorageBasedUri(details.url);
|
||||
}
|
||||
// 并进行重定向
|
||||
if (_redirect_promise_supported) {
|
||||
var promise = new Promise(function (resolve, reject) {
|
||||
var blockingResponse = redirect(details);
|
||||
return resolve(blockingResponse);
|
||||
});
|
||||
return promise;
|
||||
} else {
|
||||
return redirect(details);
|
||||
// 如果本地没有命中映射表,且没有检索内容,是一个正常的网页
|
||||
if((misEnableState===true)&&isRightfulMinURI(minUri)){
|
||||
updateStorageBasedMinURI(minUri);
|
||||
}
|
||||
return redirect(details);
|
||||
}
|
||||
}
|
||||
|
||||
/* Since firefox 52, could return a promise to handles asynchronously */
|
||||
if (browser.runtime.getBrowserInfo) {
|
||||
function checkRedirectPromise(info) {
|
||||
if (info.name == "Firefox") {
|
||||
if (parseInt(info.version.split('.')[0]) >= 52) {
|
||||
_redirect_promise_supported = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
var gettingInfo = browser.runtime.getBrowserInfo();
|
||||
gettingInfo.then(checkRedirectPromise)
|
||||
}
|
||||
// if (browser.runtime.getBrowserInfo) {
|
||||
// function checkRedirectPromise(info) {
|
||||
// if (info.name == "Firefox") {
|
||||
// if (parseInt(info.version.split('.')[0]) >= 52) {
|
||||
// _redirect_promise_supported = true;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// var gettingInfo = browser.runtime.getBrowserInfo();
|
||||
// gettingInfo.then(checkRedirectPromise)
|
||||
// }
|
||||
|
||||
/* Add listener */
|
||||
browser.webRequest.onBeforeRequest.addListener(
|
||||
|
||||
Reference in new issue
Block a user