bdwmTextBox

A new Text Box for PKU BBS

ของเมื่อวันที่ 27-08-2014 ดู เวอร์ชันล่าสุด

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

You will need to install an extension such as Tampermonkey to install this script.

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        bdwmTextBox
// @namespace   bdwmTextBox
// @description A new Text Box for PKU BBS
// @include     *bdwm.net/bbs/bbspst.php*
// @version     0.1alpha
// @grant       none
// ==/UserScript==

/**
 * cursorPosition Object
 *
 * Created by Blank Zheng on 2010/11/12.
 * Copyright (c) 2010 PlanABC.net. All rights reserved.
 * 
 * The copyrights embodied in the content of this file are licensed under the BSD (revised) open source license.
 */
 
var cursorPosition = {
	get: function (textarea) {
		var rangeData = {text: "", start: 0, end: 0 };
	
		if (textarea.setSelectionRange) { // W3C	
			textarea.focus();
			rangeData.start= textarea.selectionStart;
			rangeData.end = textarea.selectionEnd;
			rangeData.text = (rangeData.start != rangeData.end) ? textarea.value.substring(rangeData.start, rangeData.end): "";
		}
		
		return rangeData;
	},
	
	set: function (textarea, rangeData) {
		if(!rangeData) {
			alert("You must get cursor position first.")
		}
		textarea.focus();
		if (textarea.setSelectionRange) { // W3C
			textarea.setSelectionRange(rangeData.start, rangeData.end);
		} 
	},

	add: function (textarea, rangeData, text) {
		var oValue, nValue, nStart, nEnd, st;
		this.set(textarea, rangeData);
		
		if (textarea.setSelectionRange) { // W3C
			oValue = textarea.value;
			nValue = oValue.substring(0, rangeData.start) + text + oValue.substring(rangeData.end);
			nStart = nEnd = rangeData.start + text.length;
			st = textarea.scrollTop;
			textarea.value = nValue;
			// Fixbug:
			// After textarea.values = nValue, scrollTop value to 0
			if(textarea.scrollTop != st) {
				textarea.scrollTop = st;
			}
			textarea.setSelectionRange(nStart, nEnd);
		} 
	}
}

function setTextAttr(textarea, attr) {
	range = cursorPosition.get(textarea);
	nText = "\033[" + attr + "m" + range.text + "\033[m";
	cursorPosition.add(textarea, range, nText);
}

// 添加颜色下拉条
function addSelect(form, id, num, name) {
  var colors = [
    '黑色',
    '红色',
    '绿色',
    '黄色',
    '蓝色',
    '品红',
    '蓝绿',
    '白色'
  ];
  // 添加标题
  var title = document.createElement('span');
  title.innerHTML = name;
  form.appendChild(title);
  var mySelect = document.createElement('select');
  mySelect.id = id;
  for (var i = 0; i < colors.length; i++) {
    var opt = document.createElement('option');
    opt.value = num + i;
    opt.innerHTML = colors[i];
    mySelect.appendChild(opt);
  }
  form.appendChild(mySelect);
  return mySelect;
}

// 建立表格
function createForm(textarea) {
  var myform = document.createElement('form');
  var sfg = addSelect(myform, 'fgcolor', 30, '前景色');
  var sbg = addSelect(myform, 'bgcolor', 40, '背景色');
  
	// 确定按钮
	var btn = document.createElement('input');
  btn.type = 'button';
  btn.value = '确定';
  btn.onclick = function () {
    var attr = sbg.value + ';' + sfg.value;
		setTextAttr(textarea, attr);
  };
  myform.appendChild(btn);
	
	// 关闭按钮
	var closeBtn = document.createElement('input');
	closeBtn.type = 'button';
	closeBtn.value = '关闭';
	closeBtn.onclick = function() {
		this.parentNode.parentNode.style.display = "none";
	};
	myform.appendChild(closeBtn);
  
	return myform;
}

console.log('script begins');
// 获取文字窗口
var TextBoxes = document.getElementsByName('text');
if (TextBoxes.length == 0) {
  alert('No such text box!');
}
var TextBox = TextBoxes[0];
//alert(TextBox.rows);
TextBox.rows = '10';
//TextBox.value = navigator.userAgent;

// 建立浮动窗体
var mydiv = document.createElement('div');
mydiv.style.float = "left";
mydiv.style.height = "30px";
mydiv.style.width = "400px";
mydiv.style.position = "fixed";
mydiv.style.left = "30%";
mydiv.style.top = "40%";
mydiv.style.backgroundColor = "#89e842";
mydiv.style.display = "none";
mydiv.appendChild(createForm(TextBox));
document.getElementById('postfrm').appendChild(mydiv);

// 添加按钮
var Buttons = document.getElementById('postfrm').getElementsByTagName('input');
var postButton;
for (var i = 0; i<Buttons.length; i++){
	//alert(Buttons[i].value);
	if (Buttons[i].value == '发表'){
		postButton = Buttons[i];
		break;
	}
}
var attrBtn = document.createElement('input')
attrBtn.type='button';
attrBtn.onclick = function(){
	console.log("Button clicked");
	mydiv.style.display = "block";
};
attrBtn.value = '设置文字属性';
postButton.parentNode.insertBefore(attrBtn, postButton.nextSibling);