-
Notifications
You must be signed in to change notification settings - Fork 6
/
global.html
143 lines (134 loc) · 4.21 KB
/
global.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<!DOCTYPE HTML>
<html>
<head>
<meta charset='utf-8' />
<title>新同文堂</title>
<script>
var TongWenSafari = (function () {
var tongwen = {
"version" : "1.2",
"autoConvert": "none",
"iconAction" : "auto",
"symConvert" : true,
"fontCustom" : {
"enable" : false,
"trad" : "PMingLiU,MingLiU,新細明體,細明體",
"simp" : "MS Song,宋体,SimSun"
},
"urlFilter" : {
"enable" : false,
"list" : [
// { "url": "", "zhflag": "none, trad, simp" }
]
},
"userPhrase" : {
"enable" : false,
"trad" : {},
"simp" : {}
}
};
function urlFilterAction(uri) {
var lst = tongwen.urlFilter.list;
var zhflag = "";
for (var i = 0, c = lst.length; i < c; i++) {
if (lst[i].url.indexOf("*")) {
// var url = lst[i].url.replace(/(\W+)/ig, "\\$1").replace("*.", "*\\.").replace(/\*/ig, "\\w*"); // 較為嚴謹
var url = lst[i].url.replace(/(\W)/ig, "\\$1").replace(/\\\*/ig, "*").replace(/\*/ig, ".*"); // 寬鬆比對
var re = new RegExp("^" + url + "$","ig");
if (uri.match(re) !== null) {
return lst[i].zhflag;
}
} else if (uri == lst[i].url) {
return lst[i].zhflag;
}
}
return "";
}
function respondToMessage(e) {
switch (e.name) {
case "OptionsSave":
tongwen = JSON.parse(localStorage.getItem("tongwen"));
var bwrs = safari.application.browserWindows, tabs = null;
for (var i = 0, c = bwrs.length; i < c; i++) {
tabs = bwrs[i].tabs;
for (var j = 0, l = tabs.length; j < l; j++) {
tabs[j].page.dispatchMessage("CustomFontset" , tongwen);
tabs[j].page.dispatchMessage("LoadUserPhrase", tongwen);
}
}
break;
case "PageLoaded":
// 字型設定
e.target.page.dispatchMessage("CustomFontset", tongwen);
// 自定詞彙
e.target.page.dispatchMessage("LoadUserPhrase", tongwen);
// 轉換規則
var zhflag = "";
if (tongwen.urlFilter.enable) {
zhflag = urlFilterAction(e.message.baseURI);
}
// 自動轉換
if (zhflag == "") {
zhflag = tongwen.autoConvert;
}
switch (zhflag) {
case "trad": e.target.page.dispatchMessage("WebPage2Trad", tongwen); break;
case "simp": e.target.page.dispatchMessage("WebPage2Simp", tongwen); break;
}
break;
}
}
function performValidate(e) {
}
function performCommand(e) {
// tongwen = JSON.parse(localStorage.getItem("tongwen"));
switch (e.command) {
case "IconAction":
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("IconAction", tongwen);
break;
case "WebPage2Trad":
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("WebPage2Trad", tongwen);
break;
case "WebPage2Simp":
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("WebPage2Simp", tongwen);
break;
case "Options":
var newTab = safari.application.activeBrowserWindow.openTab();
newTab.url = safari.extension.baseURI + "options.html";
break;
}
}
function init(e) {
if (window.localStorage) {
var data = localStorage.getItem("tongwen");
if (data == null) {
localStorage.setItem("tongwen", JSON.stringify(tongwen));
} else {
// 合併新舊版本的設定值
var old_tongwen = JSON.parse(data);
for (var i in old_tongwen) {
if (i == "version") continue;
tongwen[i] = old_tongwen[i];
}
localStorage.setItem("tongwen", JSON.stringify(tongwen)); // 回存設定
}
}
// safari.application.activeBrowserWindow.activeTab.addEventListener("message", TongWenSafari.respondToMessage, false);
// safari.application.activeBrowserWindow.addEventListener("message", TongWenSafari.respondToMessage, false);
safari.application.addEventListener("message" , TongWenSafari.respondToMessage, false);
safari.application.addEventListener("validate", TongWenSafari.performValidate , false);
safari.application.addEventListener("command" , TongWenSafari.performCommand , false);
}
return {
respondToMessage : respondToMessage,
performValidate : performValidate,
performCommand : performCommand,
init : init
};
})();
TongWenSafari.init();
</script>
</head>
<body>
</body>
</html>