Last active
August 3, 2023 10:07
-
-
Save QQBoxy/6316548 to your computer and use it in GitHub Desktop.
PDF Copy Corrector Version2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header('content-type: application/json; charset=utf-8'); | |
function curl($url,$params = array(),$is_coockie_set = false) { | |
if(!$is_coockie_set) { | |
$ckfile = tempnam ("/tmp", "CURLCOOKIE"); | |
$ch = curl_init ($url); | |
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile); | |
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); | |
$output = curl_exec ($ch); | |
} | |
$str = ''; $str_arr= array(); | |
foreach($params as $key => $value) { | |
$str_arr[] = urlencode($key)."=".urlencode($value); | |
} | |
if(!empty($str_arr)) { | |
$str = '?'.implode('&',$str_arr); | |
} | |
$Url = $url.$str; | |
$ch = curl_init ($Url); | |
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile); | |
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); | |
$output = curl_exec ($ch); | |
return $output; | |
} | |
function Translate($word, $sl, $tl) { | |
if($word) { | |
$word = urlencode($word); | |
//hl: Header Interface Language ,sl: Sourse Language, tl: Target Language | |
$url = "http://translate.google.com/translate_a/t?client=t&text=".$word."&hl=en&sl=".$sl."&tl=".$tl."&ie=UTF-8&oe=UTF-8&multires=1&otf=1&ssel=3&tsel=3&sc=1"; | |
$name_en = curl($url); | |
$name_en = explode('"',$name_en); | |
return array("word"=>urlencode($name_en[1])); | |
} else { | |
return array("word"=>""); | |
} | |
} | |
function main() { | |
if(!empty($_GET['callback'])) { | |
if(!preg_match("/^[_\-\.0-9a-zA-Z]{1,50}$/", $_GET['callback'])) | |
return "Callback format illegal"; | |
$callback = $_GET['callback']; | |
} else { | |
$callback = ""; | |
} | |
$word = !empty($_GET['word']) ? $_GET['word'] : ""; | |
$sl = !empty($_GET['sl']) ? $_GET['sl'] : "en"; | |
if(!preg_match("/^[\-0-9a-zA-Z]{1,10}$/", $sl)) | |
return "Format illegal"; | |
$tl = !empty($_GET['tl']) ? $_GET['tl'] : "en"; | |
if(!preg_match("/^[\-0-9a-zA-Z]{1,10}$/", $tl)) | |
return "Format illegal"; | |
$contents = Translate($word, $sl, $tl); | |
if($contents) { | |
if (version_compare( phpversion() , '5.1.0', '>=')) { | |
$result = json_encode($contents); | |
} else { | |
include('json.php'); | |
$json = new Services_JSON(); | |
$result = $json->encode($contents); | |
} | |
$result = urldecode($result); | |
if($callback) { | |
return $callback.'('.$result.');'; | |
} else { | |
return $result; | |
} | |
} | |
} | |
echo main(); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-TW" xml:lang="zh-TW"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<title>Google Docs Absolute</title> | |
</head> | |
<body> | |
<!--[if gt IE 6]><!--> | |
<div align="center"> | |
<table> | |
</tr> | |
<td> | |
<input type="checkbox" id="translate" checked="checked" />啟用翻譯, <input type="checkbox" id="selected" checked="checked" />啟用全選 | |
</td> | |
<tr> | |
<tr> | |
<td> | |
Input:<br /> | |
<textarea id="inputcode" cols=100 rows=10></textarea> | |
</td> | |
<tr> | |
</tr> | |
<td> | |
Output:<br /> | |
<textarea id="outputcode" cols=100 rows=10></textarea> | |
</td> | |
</tr> | |
</tr> | |
<td> | |
Translate:<br /> | |
<textarea id="googletranslate" cols=100 rows=10></textarea> | |
</td> | |
</tr> | |
</table> | |
</div> | |
<script type="text/javascript"> | |
var scriptboxy = { | |
current : null, | |
queue : [], | |
request : function(q) { | |
var script = document.createElement('script'); | |
script.type = 'text/javascript'; | |
script.id = 'jsonScript'; | |
script.src = "http://titan.ccunix.ccu.edu.tw/~d01420005/googleTranslateAPI.php?sl=en&tl=zh-TW&word="+q.word+"&callback=scriptboxy.callback"; | |
this.queue.push({ | |
script : script, | |
success : q.success | |
}); | |
if (!this.current) this.action(); | |
}, | |
action : function() { | |
this.current = null; | |
if(this.queue.length) { | |
this.current = this.queue.shift(); | |
document.getElementsByTagName('head')[0].appendChild(this.current.script); | |
} | |
}, | |
callback : function(req) { | |
var removeScript = document.getElementById('jsonScript'); | |
if(removeScript) | |
removeScript.parentNode.removeChild(removeScript); | |
this.current.success(req); | |
this.action(); | |
}, | |
clear : function() { | |
var removeScript = document.getElementById('jsonScript'); | |
if(removeScript) | |
removeScript.parentNode.removeChild(removeScript); | |
this.queue = []; | |
} | |
}; | |
var inputLength = 0; | |
document.getElementById("inputcode").onkeyup = function(e) { | |
var len = document.getElementById("inputcode").value.length; | |
if(inputLength != len) { | |
inputLength = len; | |
codereplace(); | |
} | |
}; | |
var outputLength = 0; | |
document.getElementById("outputcode").onkeyup = function(e) { | |
var len = document.getElementById("outputcode").value.length; | |
if(outputLength != len) { | |
outputLength = len; | |
codetranslate(); | |
} | |
}; | |
document.getElementById("inputcode").onclick = function(e) { | |
if(document.getElementById("selected").checked) { | |
document.getElementById("inputcode").select(); | |
} | |
}; | |
document.getElementById("outputcode").onclick = function(e) { | |
if(document.getElementById("selected").checked) { | |
document.getElementById("outputcode").select(); | |
} | |
}; | |
document.getElementById("googletranslate").onclick = function(e) { | |
if(document.getElementById("selected").checked) { | |
document.getElementById("googletranslate").select(); | |
} | |
}; | |
function codereplace() { | |
var recode = document.getElementById("inputcode").value; | |
recode = recode.replace(/(ij)/gi,"ij"); //修正UTF-16相黏問題 | |
recode = recode.replace(/(IJ)/gi,"IJ"); | |
recode = recode.replace(/(ᵫ)/gi,"ue"); | |
recode = recode.replace(/(ff)/gi,"ff"); | |
recode = recode.replace(/(fi)/gi,"fi"); | |
recode = recode.replace(/(fl)/gi,"fl"); | |
recode = recode.replace(/(ffi)/gi,"ffi"); | |
recode = recode.replace(/(ffl)/gi,"ffl"); | |
recode = recode.replace(/(ſt)/gi,"ſt"); | |
recode = recode.replace(/(st)/gi,"st"); | |
recode = recode.replace(/(-\n)/gi,""); //合併單字換行 | |
recode = recode.replace(/[\n]/gi," "); //換行符號轉空白 | |
document.getElementById("outputcode").value = recode; | |
codetranslate(); | |
} | |
var translateTimer = null; | |
var sentence = new RegExp(/((?:(?![\.!?](?:\s|$)).)+(?:[\.!?](?:\s|$))?)/g); | |
function codetranslate() { | |
if(document.getElementById("translate").checked) { | |
if(translateTimer) clearTimeout(translateTimer); | |
translateTimer = setTimeout("translate();", 500); | |
} | |
} | |
function translate() { | |
scriptboxy.clear(); | |
document.getElementById('googletranslate').value = ""; | |
var words = document.getElementById("outputcode").value.match(sentence); | |
for(key in words) { | |
scriptboxy.request({ | |
word : words[key], | |
success : function(e) { | |
document.getElementById('googletranslate').value += e.word; | |
} | |
}); | |
} | |
} | |
</script> | |
<!--><![endif]--> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment