// ==UserScript==
// @name 仓库用度盘投稿助手
// @namespace org.jixun.dupan.galacg
// @description 简易功能增强, 方便仓库投稿用
// @include http://pan.baidu.com/disk/home#*
// @include http://pan.baidu.com/disk/home
// @version 1.0.1
// @grant none
// @run-at document-end
// ==/UserScript==
var script = document.createElement('script');
script.innerHTML = ';(' + (function () {
var __AppId = 250528;
var _ = function (foo) {
return foo.toString().match(/\/\*([\s\S]+)\*\//)[1];
};
// 第三方百度盘用户脚本 SDK
var $fileAPI = require("clouddisk-ui:widget/list-view/disk-home.js");
var $dataCenter = require("common:widget/data-center/data-center.js");
var $dialog = require ('common:widget/dialog/dialog.js');
// 刷新当前视图
var _reload = function () { $fileAPI.render(); };
// 消息提示
var Toast = require("common:widget/toast/toast.js");
var $panel = require ('common:widget/panel/panel.js');
/*
Toast.obtain.useToast ({
toastMode:
Toast.obtain.MODE_LOADING
| Toast.obtain.MODE_CAUTION
| Toast.obtain.MODE_SUCCESS
| Toast.obtain.MODE_FAILURE
| Toast.obtain.MODE_NONE,
msg: '消息',
sticky: true, // 是否保留显示不消失 (除非用户交互)
position: $panel.TOP, // 如果不是这个则为 0,0 (内置)
closeType: true, // True 直接显示; False 单击后隐藏
relativeDOM: 定位用元素 DOM
});
*/
/**
* @namespace $dataCenter
appVersion: Object
currentDir: "/目录"
is-timeline: true
isDesc: 1
isOnlyRenderTypeChange: false
orderKey: "time"
page: "disk-home"
quota: Object
selectedItemList: [ 选中的项目的元素列表 ]
selectedList: [ 选中的项目的 id 列表 ]
tmpl-selected-list: <回调, 生成选中列表文字>
*/
// require('clouddisk-ui:widget/context-menu/context-menu.js');
$('<style>').text (_(function () {/*
.jx_btn {
background: #fefefe;
background: -moz-linear-gradient(top, #fefefe 0%, #f2f2f2 88%);
background: -webkit-linear-gradient(top, #fefefe 0%,#f2f2f2 88%);
background: linear-gradient(to bottom, #fefefe 0%,#f2f2f2 88%);
display: inline-block;
line-height: 26px;
color: #666;
vertical-align: middle;
margin: 0px 0px 0px 5px;
text-decoration: none;
border: 1px solid #AAA;
padding: 0px 20px;
height: 26px;
border-radius: 2px;
}
.jx_btn:hover {
color: #666;
}
.jx_btn:active {
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_red { color: red }
*/})).appendTo (document.body);
(function (hookVer) {
console.info ('Dupan Hooker ready: %s', hookVer);
// var hookVer = '1.0';
var customHookers = {};
var msgSystem = window.mdev.message;
// Check if is already hooked;
if (msgSystem.hookVer) {
if (hookVer !== msgSystem.hookVer) {
throw new Error('Custom Hooker version does not match!\nCurrent: ' + hookVer + ', On-page: ' + msgSystem.hookVer);
} else {
console.info('Already hooked with same version %s, skip...', hookVer);
}
return;
}
msgSystem.hookVer = hookVer;
msgSystem.oldTrigger = msgSystem.trigger;
msgSystem.hookEve = function (eve, fooCb) {
if (!customHookers.hasOwnProperty(eve))
customHookers[eve] = [];
customHookers[eve].push(fooCb);
return msgSystem;
};
msgSystem.trigger = function (eve) {
console.debug ('Trigger event: %s', eve);
var args = [].slice.call(arguments, 1);
if (customHookers.hasOwnProperty(eve)) {
for (var i = 0; i < customHookers[eve].length; i++) {
try {
customHookers[eve][i].apply(undefined, args);
} catch (e) {
// 抛出消息,表示这个事件不应该继续了。
if (e.exit)
return;
}
}
}
msgSystem.oldTrigger.apply(msgSystem, [eve].concat(args));
};
})('1.0');
var $menuBase = $('div.module-context-menu > ul.menu-main');
$menuBase.find('a[data-key="rename"]').parent().after ($('<li>').append($('<a>').attr({
hidefocus: true,
href: "javascript: ;",
class: "list-node",
'node-type': "item"
}).data('key', 'batch-rename').text('批量重命名')));
$menuBase.find('a[data-key="share"]').parent().after ($('<li>').append($('<a>').attr({
hidefocus: true,
href: "javascript: ;",
class: "list-node",
'node-type': "item"
}).data('key', 'custom-share').text('自定义分享')));
$(_(function () {/*
<a node-type="btn-reload" href="javascript: ;" class="jx_btn jx_btn_reload">刷新</a>
*/})).insertAfter ($('.icon-btn-createfile')).click(function () {
_reload ();
});
var _batchRename = function () {
var $wndConfirmRename = new $dialog ();
var $tplConfirmRename = $(_(function () {/*
<p>请输入新的命名规则 (自动储存): <input id="jx_nameRule" style="width:20em" /></p>
<p style="line-height: 1; padding-top: 1em;"><code>:e</code> 表示扩展名; <code>:E</code> 表示 .扩展名; <code>:d</code> 表示一位随机数字;
<br /><code>:c</code> 表示一位随机字符; <code>:t</code> 表示当前时间戳</p>
*/}));
$('#jx_nameRule', $tplConfirmRename).val(localStorage.jxRenameRule || '[GalACG] :d:d:d:d:d:d:d:d:d:d:E');
$wndConfirmRename.Confirm('确认批量重命名', $tplConfirmRename, function () {
// 确认重命名
var _nameRule = $('#jx_nameRule', $tplConfirmRename).val();
// 储存命名规则
localStorage.jxRenameRule = _nameRule;
var _curDir = $dataCenter.get('currentDir');
if (_curDir.substr(-1) !== '/')
_curDir += '/';
var _fixName = function (z, code) {
switch (code) {
case 'c':
return String.fromCharCode(97 + Math.random() * 26);
case 'd':
return Math.random().toString().slice(3,4);
case 't':
return +new Date();
case 'e':
var ext = this.match(/\.([^.]+)$/);
return ext ? ext[1] : '';
case 'E':
return this.match(/\.[^.]+$/) || '';
}
};
var _list = $dataCenter.get ('selectedList');
var _flist = [].slice.call($dataCenter.get ('selectedItemList').map(function (e) {
// [{"path":"/1.rar","newname":"2.rar"}]
var file = e.find('.name-text').text();
return {
path: _curDir + file,
newname: _nameRule.replace(/:([cdeE])/g, _fixName.bind(file))
};
}));
Toast.obtain.useToast ({
toastMode: Toast.obtain.MODE_LOADING,
msg: '正在重命名…',
sticky: true
});
$.ajax ({
url: '/api/filemanager?' + $.param({
channel: 'chunlei',
bdstoken: yunData.MYBDSTOKEN,
app_id: __AppId,
opera: 'rename'
}),
type: 'POST',
data: {
filelist: JSON.stringify(_flist)
}
}).success(function () {
Toast.obtain.useCloseToast();
_reload ();
});
this.setVisible (0);
}, function () {
// 取消重命名
this.setVisible (0);
});
};
var _genKey = function (size) {
// length => 26 + 10, 36
var keySet = 'abcdefghijklmnopqrstuvwxyz0123456789';
for (var i = (size || 4) | 0, r = ''; i--; )
r += keySet[0 | (Math.random() * 36)];
return r;
};
var _customShare = function () {
console.debug ('customShare');
var $wndCustomShare = new $dialog ();
var $tplCustomShare = $(_(function () {/*
<div>
<p>请输入提取码: <input id="jx_shareKey" style="width: 6em" /></p>
<p id="jx_errmsg" class="jx_red jx_hide">无效的提取码, 脚本将随机生成一个分享代码 :<</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" style="width: 100%;"></textarea></p>
</div>
*/}));
var _isCodeValid = function (t) {
return encodeURIComponent(t).replace(/%[A-F\d]{2}/g, '-').length == 4;
};
var _fixCode = function (s) {
return s.replace(/"/g, '"').replace(/\]/g, ']');
};
var _date = function (d) {
return d.getFullYear () + '-' + d.getMonth() + '-' + d.getDate();
};
var _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));
$wndCustomShare.Confirm('自定义分享', $tplCustomShare, function () {
var key = _isCodeValid(_key.val()) ? _key.val() : _genKey (4);
Toast.obtain.useToast ({
toastMode: Toast.obtain.MODE_LOADING,
msg: '正在分享…',
sticky: true
});
var $pane = $(this._mUI.pane);
$.ajax ({
url: '/share/set?' + $.param({
channel: 'chunlei',
bdstoken: this.token,
app_id: __AppId
}),
type: 'POST',
data: {
fid_list: JSON.stringify($dataCenter.get ('selectedList')),
schannel: 4,
channel_list: '[]',
pwd: key
},
dataType: 'json'
}).success(function (r) {
$pane.find ('a.okay').hide ();
$pane.find ('a.cancel>b').text ('关闭');
$tplCustomShare.toggleClass ('jx_hide');
$('#jx_shortUrl', $tplCustomShare).val (r.shorturl || '很抱歉, 分享失败 :<');
$('#jx_shareCode', $tplCustomShare).val (key);
var selList = $dataCenter.get ('selectedItemList');
$('#jx_dlboxCode', $tplCustomShare).val ('[dlbox title="' + _fixCode($('.name-text', selList[0]).text())
+ (selList.length == 1 ? '' : ' 等文件') + '" from="浩瀚的宇宙" time="'
+ _date(new Date()) + '" info="提取:' + key + '" link1="度娘|'
+ r.shorturl + '#' + key + '"][/dlbox]');
Toast.obtain.useCloseToast();
_reload ();
});
// this.setVisible (0);
}, function () {
this.setVisible (0);
});
};
// 通过 Hook 确保菜单项被显示、处理后续事件
window.mdev.message.hookEve ('context-menu-render', function (e, menus) {
menus['batch-rename'] = menus['custom-share'] = 'enable';
}).hookEve ('file-operate', function (op) {
switch (op) {
case 'batch-rename':
_batchRename ();
break;
case 'custom-share':
_customShare ();
break;
default:
return ;
}
throw {exit: true};
});
}).toString () + ')();';
document.body.appendChild(script);