一、与前一个版本的不同
- 将网页中的button标签换成了a标签,方便后续美化
- 从网上找了关于限制页面缩放的代码,防止通过Ctrl+鼠标滚轮进行页面缩放
- 防止通过页面最大化或拖动窗口边缘改变窗口大小
二、代码
main.hta
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0, target-densitydpi=device-dpi" />
<title>String Convert Tool V1.0</title>
<script type="text/javascript" src="./JS/main.js"></script>
<link rel="stylesheet" type="text/css" href="main.css">
<!-- 下面这些HTA窗口样式的属性,需要把上面<meta http-equiv="X-UA-Compatible" content="ie=edge" />注释掉才能显现,这是兼容性问题 -->
<HTA:APPLICATION
APPLICATIONNAME="String Convert Tool"
BORDER="dialog"
BORDERSTYLE="complex"
CAPTION="yes"
CONTEXTMENU="no"
INNERBORDER="yes"
ID="oHTA"
ICON="#"
MAXIMIZEBUTTON="no"
MINIMIZEBUTTON="no"
NAVIGABLE="no"
SCROLL="no"
SCROLLFLAT="no"
SELECTION="no"
SYSMENU="yes"
SINGLEINSTANCE="yes"
SHOWINTASKBAR="yes"
VERSION="1.0"
WINDOWSTATE="normal"
/>
</head>
<body>
<center>
<div id="inputBox">
<textarea id="txtPaste" placeholder="粘贴待转换的英文到这里..."></textarea>
</div>
<div id="buttonArea1">
<a id="btnToUpper" onclick="funToUpper()" href="#">全部字母大写</a>
<a id="btnToLower" onclick="funToLower()" href="#">全部字母小写</a>
<a id="btnOnlyFirstCharUpper" onclick="funOnlyFirstCharUpper()" href="#">仅首字母大写</a>
<a id="btnEachWordUpper" onclick="funEachWordUpper()" href="#">每个单词大写</a>
</div>
<div id="buttonArea2">
<a id="btnClearPaste" onclick="funClearPaste()" href="#">清除所有内容</a>
<a id="btnLeftTrim" onclick="funLeftTrim()" href="#">清除左侧空格</a>
<a id="btnRightTrim" onclick="funRightTrim()" href="#">清除右侧空格</a>
<a id="btnTrim" onclick="funTrim()" href="#">清除两侧空格</a>
</div>
<div>
<textarea id="txtCopy" readonly placeholder="转换好的英文在这里..."></textarea>
</div>
<div>
<a id="btnCopyExit" onclick="funCopyExit()" href="#">拷贝并退出</a>
</div>
</center>
<!-- 当页面加载完毕后执行Paste,自动将剪切板中的内容Paste到文本框。 -->
<script type="text/javascript">
var obj = document.getElementById("txtPaste");
obj.focus();
document.execCommand("paste");
funToUpper();
</script>
</body>
</html>
main.js
//resize the window
window.resizeTo(600, 400);
// 判断pc浏览器是否缩放,若返回100则为默认无缩放,如果大于100则是放大,否则缩小
function detectZoom() {
var ratio = 0,
screen = window.screen,
ua = navigator.userAgent.toLowerCase();
if (window.devicePixelRatio !== undefined) {
ratio = window.devicePixelRatio;
}
else if (~ua.indexOf('msie')) {
if (screen.deviceXDPI && screen.logicalXDPI) {
ratio = screen.deviceXDPI / screen.logicalXDPI;
}
}
else if (window.outerWidth !== undefined && window.innerWidth !== undefined) {
ratio = window.outerWidth / window.innerWidth;
}
if (ratio) {
ratio = Math.round(ratio * 100);
}
console.log(window.devicePixelRatio, 123)
return ratio;
};
// window.onresize 事件可用于检测页面是否触发了放大或缩小。
window.addEventListener('resize', function () {
isScale();
})
isScale();
//判断PC端浏览器缩放比例不是100%时的情况
function isScale() {
var rate = detectZoom(),
isMac = /macintosh|mac os x/i.test(navigator.userAgent); // Mac默认缩放值为200,windows默认为100,需要分开判断
if ((isMac && rate !== 200) || (!isMac && rate !== 100)) {
//如何让页面的缩放比例自动为100,'transform':'scale(1,1)'没有用,又无法自动条用键盘事件,目前只能提示让用户如果想使用100%的比例手动去触发按ctrl+0
alert('当前页面不是100%显示,请按键盘ctrl/command + 0恢复100%显示标准,以防页面显示错乱!')
}
}
// 阻止pc端浏览器缩放js代码
// 由于浏览器菜单栏属于系统软件权限,没发控制,我们着手解决ctrl/cammond + +/- 或 Windows下ctrl + 滚轮 缩放页面的情况,只能通过js来控制了
document.addEventListener('DOMContentLoaded', function (event) {
// chrome 浏览器直接加上下面这个样式就行了,但是ff不识别
document.body.style.zoom = 'reset';
document.addEventListener('keydown', function (event) {
if ((event.ctrlKey === true || event.metaKey === true)
&& (event.which === 61 || event.which === 107
|| event.which === 173 || event.which === 109
|| event.which === 187 || event.which === 189)) {
event.preventDefault();
}
}, false);
var scrollFunc = function (event) {
event = event || window.event;
if (event.wheelDelta) { //判断浏览器IE,谷歌滑轮事件
if (event.ctrlKey === true || event.metaKey) {
event.preventDefault();
}
}
};
//给页面绑定滑轮滚动事件
if (document.addEventListener) {
document.addEventListener('DOMMouseScroll', scrollFunc, false);
}
//滚动滑轮触发scrollFunc方法
window.onmousewheel = document.onmousewheel = scrollFunc;
}, false);
//禁止window窗口变化
window.onresize=function(){
window.resizeTo(600,400);
}
//自定义function方法,浏览器不支持trim
function myTrim(x) {
return x.replace(/^\s+|\s+$/gm, '');
}
function myLeftTrim(x){
return x.replace(/(^\s*)/g,'');
}
function myRightTrim(x){
return x.replace(/(\s*$)/g,'');
}
//与画面相关的function
function funLeftTrim(){
document.getElementById("txtCopy").value=myLeftTrim(document.getElementById("txtCopy").value);
}
function funRightTrim() {
document.getElementById("txtCopy").value=myRightTrim(document.getElementById("txtCopy").value);
}
function funTrim() {
document.getElementById("txtCopy").value=myTrim(document.getElementById("txtCopy").value);
}
function funClearPaste(){
document.getElementById("txtPaste").value="";
document.getElementById("txtCopy").value="";
}
function funToUpper() {
var str=document.getElementById("txtPaste").value;
document.getElementById("txtCopy").value=String(str).toUpperCase();
}
function funToLower() {
var str=document.getElementById("txtPaste").value;
document.getElementById("txtCopy").value=String(str).toLowerCase();
}
function funOnlyFirstCharUpper() {
var str=myTrim(document.getElementById("txtPaste").value);
if (String(str).length<1) {
document.getElementById("txtCopy").value="";
}
else{
document.getElementById("txtCopy").value=String(str).charAt(0).toUpperCase()+String(str).substring(1).toLowerCase();
}
}
function funEachWordUpper() {
var str = myTrim(document.getElementById("txtPaste").value);
if (String(str).length<1) {
document.getElementById("txtCopy").value="";
return;
}
var res=String(str).charAt(0).toUpperCase();
for (var index = 1; index < String(str).length; index++) {
if (String(str).charAt(index-1)==' ' || String(str).charAt(index-1)=='\n') {
res+=String(str).charAt(index).toUpperCase();
}else{
res+=String(str).charAt(index);
}
}
document.getElementById("txtCopy").value= res;
}
function funCopyExit() {
var obj=document.getElementById("txtCopy");
obj.select();
document.execCommand("copy");
window.close();
}
main.css
* {
padding: 0;
margin: 0;
}
#buttonArea1 {
margin: 5px;
}
#buttonArea2 {
margin-bottom: 5px;
}
#btnCopyExit {
display: block;
height: 40px;
margin-top: 10px;
}
a {
display:inline-block;
background-color:palevioletred;
height: 35px;
width: 100px;
color: white;
text-decoration: none;
line-height: 40px;
}
a:hover {
background-color: deepskyblue;
}
textarea {
width:550px;
height: 100px;
}