Greasy Fork

Greasy Fork is available in English.

仓库用度盘投稿助手

简易功能增强, 方便仓库投稿用

当前为 2016-02-26 提交的版本,查看 最新版本

// ==UserScript==
// @name         仓库用度盘投稿助手
// @namespace    moe.jixun.dupan.galacg
// @version      1.1.0
// @description  简易功能增强, 方便仓库投稿用
// @author       Jixun
// @include      http://pan.baidu.com/disk/home*
// @include      http://yun.baidu.com/disk/home*

// 因为需要抢在页面脚本执行之前, 因此必须申请一个权限。
// TamperMonkey 测试通过, GreaseMonkey 未知.
// @grant        unsafeWindow
// @run-at       document-start
// ==/UserScript==

var script = document.createElement('script');
script.innerHTML = ';(' + (function () {
	var oRequire;
	Object.defineProperty(window, 'require', {
		set: function (require) {
			oRequire = require;
		},
		get: function () {
			return fakeRequire;
		}
	});

	var __AppId = 250528;

	function _ (fn) {
		return fn.toString().match(/\/\*([\s\S]+)\*\//)[1];
	}
	
	function sprintf () {
		var arg = arguments,
			len = arg.length - 1,
			inp = arg[0];
		
		if (len < 1) return;
		
		for (var i=len; i>0; i--) {
			inp = inp.replace (new RegExp('(\\$|%)'+i,'g'), arg[i])
		}
		return inp;
	}

	function fakeRequire (module) {
		console.info('Load module: %s', module);
		if (module == 'disk-system:widget/pageModule/list/listMenu.js') {
			if (!_G.injected) {
				initScript();
				_G.injected = true;
			}
		}
		return oRequire.apply(this, arguments);
	}

	function appendAfter (list, name, item, noPush) {
		for (var i = 0; i < list.length; i++) {
			if (list[i] instanceof Array) {
				if (appendAfter(list[i], name, item, true)) {
					return ;
				}
			} else if (list[i].title === name) {
				i++;
				list.splice(i, i, item);
				return true;
			}
		}

		if (!noPush) list.push(item);
	}

	var _G = {};
	function initScript () {
		// Load module
		_G.FaceData = oRequire('disk-system:widget/data/faceData.js');
		_G.ListInit = oRequire("disk-system:widget/pageModule/list/listInit.js");

		var fileCtxMenu = _G.FaceData.getData().contextMenu.file;
		appendAfter(fileCtxMenu, '分享', {
			title: '自定义分享',
			index: 8,
			keyboard: 'u',
			action: lazyDialog('loadCustomShareDialog')
		});

		fileCtxMenu.push({
			title: '批量重命名',
			keyboard: 'r',
			position: 'bottom',
			index: 2,
			action: lazyDialog('loadBatchRenameDialog')
		});

		setTimeout(delayLoad, 200);
	}

	function lazyDialog (name) {
		return function () {
			_G[name]();
		};
	}

	function nextId () {
		if (!nextId.id) nextId.id = 0;
		return nextId.id++;
	}

	function confirmDialog (data) {
		loadDependencies();

		var dialog;
		var dialogData = {
			id: "confirm-" + nextId(),
			show: true,
			title: data.title,
			body: $('<div style="text-align:center; padding:22px;">').append(data.body),
			buttons: [{
				name: "confirm",
				title: data.sureText || "确定",
				type: "big",
				color: "blue",
				padding: ["50px", "50px"],
				click: function() {
					if ($.isFunction(data.onSure)) {
						data.onSure.apply(this, arguments);
					} else {
						dialog.hide();
					}
				}
			}, {
				name: "cancel",
				title: data.cancelText || "取消",
				type: "big",
				padding: ["50px", "50px"],
				click: function() {
					if ($.isFunction(data.onCancel)) {
						data.onCancel.apply(this, arguments);
					} else {
						dialog.hide();
					}
				}
			}]
		};
		dialog = new _G.Dialog(dialogData);
		return dialog;
	}

	function delayLoad () {
		_G.loadCustomShareDialog = (function () {
			var _tplCustomShare = _(function () {/*
				<div>
					<p>请输入提取码:  <input id="jx_shareKey" style="width: 6em" /></p>
					<p id="jx_errmsg" class="jx_c_warn jx_hide">无效的提取码, 脚本将随机生成一个分享代码 :&lt;</p>
				</div>
				<div class="jx_hide">
					<p>分享地址: <input id="jx_shortUrl" style="width: 20em" readonly /></p>
					<p>提取码: <input id="jx_shareCode" style="width: 5em; text-align: center" readonly /></p>

					<p style="text-align: left">投稿代码:<br /><textarea readonly id="jx_dlboxCode" class="jx"></textarea></p>
				</div>
			*/});

			var dialog, $footer, $key, $tplCustomShare;
			function showDialog () {
				$tplCustomShare = $(_tplCustomShare);
				dialog = confirmDialog({
					title: '自定义分享',
					body: $tplCustomShare,
					onSure: function () {
						handleConfirm();
					},
					onCancel: function () {
						dialog.hide();
					},
				});

				$footer = dialog.$dialog.find(_G.Dialog.QUERY.dialogFooter);

				$key = $('#jx_shareKey', $tplCustomShare)
					.blur (function () {
						if (!isCodeValid(this.value = this.value.trim())) {
							$('#jx_errmsg', $tplCustomShare)
								.removeClass ('jx_hide');
						}
					})
					.focus (function () {
						$('#jx_errmsg', $tplCustomShare)
							.addClass ('jx_hide');
					})
					.val(genKey (4));
			}

			function handleConfirm () {
				var key = $key.val();
				if (!isCodeValid(key)) {
					key = genKey(4);
					$key.val(key);
				}

				_G.Tip.show({
					mode: 'loading',
					msg: '正在分享',
					autoClose: false
				});

				var sharedItems = _G.ListInit.getCheckedItems();
				$.ajax ({
					url: '/share/set?' + $.param({
						channel: 'chunlei',
						bdstoken: this.token,
						app_id: __AppId
					}),
					type: 'POST',
					data:  {
						fid_list: JSON.stringify(sharedItems.map(getFileId)),
						schannel: 4,
						channel_list: '[]',
						pwd: key
					},
					dataType: 'json'
				}).success(function (r) {
					_G.Tip.hide();

					_G.Tip.show({
						mode: 'success',
						msg: '分享成功!'
					});

					$footer.children('.g-button-blue-large').hide();
					$footer.children('.g-button-large').find('.text').text('关闭');

					$('#jx_shortUrl',  $tplCustomShare).val (r.shorturl || '很抱歉, 分享失败 :<');
					$('#jx_shareCode', $tplCustomShare).val (key);

					$tplCustomShare.toggleClass ('jx_hide');

					var title = fixCode(sharedItems[0].server_filename) + (sharedItems.length == 1 ? '' : ' 等文件');

					$('#jx_dlboxCode', $tplCustomShare).val ('[dlbox title="' + 
						title + '" from="浩瀚的宇宙" time="' +
						makeDate(new Date()) + '" info="提取:' + key +
						'" link1="度娘|' + r.shorturl + '#' + key + '"][/dlbox]'
					);
				}).fail(function () {
					_G.Tip.hide();

					_G.Tip.show({
						mode: 'failure',
						msg: '网络错误,请稍后重试。'
					});
				});
			}

			/* 依赖函数表 */
			function isCodeValid(code) {
				return encodeURIComponent(code).replace(/%[A-F\d]{2}/g, '-').length == 4;
			}

			function fixCode (code) {
				return code.replace(/"/g, '&#x22;').replace(/\]/g, '&#x5D;');
			}

			function fixWidthDigits (d) {
				return ('0' + d.toString()).slice(-2);
			}

			function makeDate (d) {
				return sprintf('%1.%2.%3', d.getFullYear (),
					fixWidthDigits(d.getMonth () + 1),
					fixWidthDigits(d.getDate ())
				);
			}

			function genKey (size) {
				// length => 26 + 10, 36
				var keySet = 'abcdefghijklmnopqrstuvwxyz0123456789';
				for (var i = (size || 4) | 0, r = ''; i--; )
					r += keySet[0 | (Math.random() * 36)];
				
				return r;
			}

			function getFileId (item) {
				return item.fs_id;
			}

			/* 收尾 */
			return showDialog;
		})();

		function loadBatchRenameDialog () {
			alert('敬请期待!');
		}

	}

	function loadDependencies () {
		_G.Tip = oRequire("disk-system:widget/system/uiService/tip/tip.js");
		_G.Dialog = oRequire("disk-system:widget/system/uiService/dialog/dialog.js");
		_G.Context = oRequire("disk-system:widget/context/context.js").instanceForSystem;
		_G.ui = _G.Context.ui;
	}

	// Inject style
	var style = document.createElement('style');
	style.textContent = _(function () {/*
.jx_btn {
	background: #fefefe;
	background: linear-gradient(to bottom,  #fefefe 0%,#f2f2f2 88%);

	display: inline-block;
	line-height: 25px;
	vertical-align: middle;
	margin: 0 0 0 10px;
	text-decoration: none;
	border: 1px solid #AAA;
	padding: 0px 20px;
	height: 26px;
	border-radius: 2px;

	min-width: 3em;
	text-align: center;
}
.jx_btn, .jx_btn:hover, .jx_btn:focus {
	color: #666;
}
.jx_btn:active {
	color: #06C;
	background: #e3e3e3;
	background: -moz-linear-gradient(top,  #e3e3e3 0%, #f7f7f7 12%);
	background: -webkit-linear-gradient(top,  #e3e3e3 0%,#f7f7f7 12%);
	background: linear-gradient(to bottom,  #e3e3e3 0%,#f7f7f7 12%);
}

.jx_hide   { display: none }
.jx_c_warn { color: red }

.dialog-panel .jx_list {
	text-align: left;
	max-height: 3em;
	overflow-y: scroll;
	overflow-x: hidden;
	line-height: 1;
	padding: .2em;
	margin-bottom: .5em;
}

.jx_list .name {
	color: black;
}
.jx_list .size {
	color: #777;
}

textarea.jx{
	width: 100%;
}
	*/});
	document.head.appendChild(style);
}).toString () + ')();';
document.head.appendChild(script);