Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4358952

Browse filesBrowse files
committed
Added some documentation, added real support for createComment and better dynamic scripting support. More of the Bilibili API implemented
1 parent e258bf0 commit 4358952
Copy full SHA for 4358952

12 files changed

+56,620-68Lines changed: 56620 additions & 68 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎docs/scripting/TransmissionAPI.md‎

Copy file name to clipboard
+21Lines changed: 21 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Transmission API
2+
=====================================
3+
这里定义了传输 API
4+
5+
Worker → CCLScriptingContext
6+
-------------------------------------
7+
- CleanUp
8+
Requests the cleanup of a worker. This signals that the worker is finished
9+
all tasks and should not be used again. We assume all resources are freed.
10+
请求清理一个高级弹幕运行沙箱。此信号表示沙箱将不会在被使用并且取消CCL执行空间对沙箱的管理。
11+
我们默认在这时所有此沙箱的变量都已经无效化并且被释放。
12+
13+
- Trace
14+
Sends a request for Trace output to the CCLContext. 发送调试信息到 CCLContext。
15+
一个外部的监听器可以接受到所有此类信号。主要由 Trace 函数发送,用户也可以重载让别的函数发送。
16+
17+
- AssignObject
18+
Registers an object id, class and abstraction on the CCLContext. This is used
19+
to actually initialize DOM elements and the such. IDs correspond to the same
20+
IDs in the worker.
21+
注册一个对象
Collapse file

‎docs/scripting/dynamic_comment.md‎

Copy file name to clipboardExpand all lines: docs/scripting/dynamic_comment.md
  • Display the source diff
  • Display the rich diff
Whitespace-only changes.
Collapse file

‎docs/scripting/globals.md‎

Copy file name to clipboard
+64Lines changed: 64 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
Globals 库
2+
================================
3+
Globals库用于保存全局变量,这样就能实现跨空间读取。但是由于我们的沙箱架构,跨空间读取信息本身就有
4+
一定的限制。同时由于 Globals 支持可能带来一些开销,一些实现可能会禁止 Globals API。我们强烈建议
5+
开发者们 **不要依赖Globals库**
6+
7+
._set (key, value)
8+
---------------------------------
9+
设定一个 Key 为 Value。
10+
11+
._get (key)
12+
---------------------------------
13+
获取一个 Key 下存储的 Value
14+
15+
不兼容细节
16+
---------------------------------
17+
首先,由于通讯协议的限制,每次更改变量必须通过 ._set 重新更改全局变量,因为全局的变量和 Worker 实例
18+
里面 Globals 库缓存的变量是不一样的,同样,期待产生变化的话必须通过 ._get 重新载入。举例如下:
19+
WorkerA
20+
21+
var x = Globals._set("a", {b:10, c:11});
22+
trace(x.b); // = 10
23+
x.b = 20;
24+
trace(x.b); // = 20
25+
trace(Globals._get("a").b) // = 10
26+
Globals._set("a",x);
27+
trace(Globals._get("a").b) // = 20
28+
29+
我们再考虑在 A 同时运行的 B 代码:
30+
WorkerB
31+
32+
var x = Globals._get("a");
33+
// 在 A 的第二个 _set 之前
34+
trace(x.b) // = 10
35+
// 假如这时 WorkerA 更改了 Globals 中的 a
36+
trace(x.b) // = 10
37+
trace(Globals._get("a").b) // = 20
38+
39+
换句话说,每次对 _set 或者 _get 的调用就是同步变量。如果希望保险的话,可以不去把结果赋值,而是
40+
每次懂 _get 一遍。由于变量是缓存的,这并不会非常影响效率。
41+
42+
其次,由于通讯必须通过 JSON 化,所以无法传递函数或者状态信息到对方。于是 Globals 库会采取如下措施:
43+
对于
44+
45+
w = {
46+
doSomething:function(){
47+
// some code
48+
}
49+
}
50+
51+
我们会首先打包 `doSomething` 函数为一个对象,然后更改变量名为 "__f:doSomething":
52+
53+
w = {
54+
"__f:doSomething":"function(){ /* some code */ }"
55+
}
56+
57+
并且在对 get 时侧解包,并且 eval 出函数,赋值。这里需要注意两点,由于解包时所在的环境不同,函数
58+
可能会失去一些本可以访问的对象,所以在编写时请不要依赖任何变量存在,因为我们不能引入别的变量。如果
59+
你希望链接 `w` 本体(如使用 `w.otherParameter = 'something'` ),则需要在所有提到 `w` 的时候
60+
使用 `__unpack_self` 来引用(所以创建变量的时候也最好采取同样名称,如果还需引入自己的话)。
61+
62+
63+
64+
Collapse file

‎docs/scripting/sandbox.md‎

Copy file name to clipboardExpand all lines: docs/scripting/sandbox.md
+10-3Lines changed: 10 additions & 3 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,20 @@ Supported Objects 支持的对象
1515
建造一个按钮,可以挂载 onclick 事件。
1616
- $.createComment
1717
制造一条弹幕,通过参数模式动态插入 CommentManager。注意此弹幕不会出现在timeline里面,而是
18-
通过 send() 派发出的。
18+
通过 send() 派发出的。注意:有时此弹幕不会被派发出,是否通过 CommentManager 渲染会根据弹幕
19+
的具体参数情况决定(请参考 [动态弹幕](dynamic_comment.md))。
1920
- $.createShape
20-
制造一个多边形(渲染层是一个新的canvas)。
21+
制造一个SVG图形
2122

2223
Support for Bili Library 兼容性库
2324
--------------------------------
24-
- 还原 Util
25+
- 还原 Utils
2526
(基本都是原生方法的套用)
2627
- 原生支持 Math 库
2728
- 原生支持 String 库
29+
- 支持部分 Display 库(Shape,Comment,Button等)
30+
- 支持部分 Player 库(基本操控)
31+
- 支持部分 Bitmap 库
32+
- 支持部分 Storage 库
33+
- 有限支持 Global 库(参考 [Global库](globals.md) 中的『不兼容细节』)
34+
- 有限支持 Tween 库(参考 [Tween库](tween.md) 中的『不兼容细节』)
Collapse file

‎docs/scripting/tween.md‎

Copy file name to clipboardExpand all lines: docs/scripting/tween.md
  • Display the source diff
  • Display the rich diff
Whitespace-only changes.
Collapse file

‎experimental/scripting/bscript.js‎

Copy file name to clipboardExpand all lines: experimental/scripting/bscript.js
+76-11Lines changed: 76 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ CCLScripting = {
145145
case "RequestObject":
146146
ctx.get(resp.name);
147147
break;
148+
case "UpdateObject":
149+
ctx.updateObjectField(resp.name, resp.field, resp.value);
150+
break;
148151
case "CallObjectMethod":
149152
ctx.callObjectMethod(resp.id, resp.method, resp.params);
150153
break;
@@ -212,7 +215,35 @@ CCLScripting = {
212215

213216
CCLScripting.ScriptingContext = function(stage){
214217
var boundObjects = {};
215-
218+
219+
this.updateObjectField = function(id, field, value){
220+
var obj = boundObjects[id];
221+
if(!obj)
222+
return;
223+
switch(obj.className){
224+
case "CommentObject":
225+
if(field === "text"){
226+
if(obj.domParent){
227+
obj.domParent.innerText = value;
228+
stage.appendChild(obj.domParent);
229+
}
230+
obj.text = value;
231+
}
232+
break
233+
default:
234+
if(field === "x"){
235+
if(obj.domParent){
236+
obj.domParent.style.left = value + "px";
237+
}
238+
}else if(field === "y"){
239+
if(obj.domParent){
240+
obj.domParent.style.top = value + "px";
241+
}
242+
}
243+
break;
244+
}
245+
};
246+
216247
this.callMethod = function (method, params){
217248
if(method === "alert"){
218249
if(params)
@@ -238,8 +269,7 @@ CCLScripting = {
238269
};
239270

240271
this.set = function(objname, objclass, serialized){
241-
boundObjects[objname] = new CCLScripting.CommonObject();
242-
console.log(objclass);
272+
boundObjects[objname] = new CCLScripting.CommonObject(objclass);
243273
switch(objclass){
244274
case "ButtonObject":{
245275
// Do some setup.
@@ -271,15 +301,26 @@ CCLScripting = {
271301
"height":stage.offsetHeight,
272302
"style":{
273303
"position":"absolute",
274-
"top":"0px",
275-
"left":"0px"
304+
"top": (serialized.y ? serialized.y : 0) + "px",
305+
"left":(serialized.x ? serialized.x : 0) + "px"
276306
}});
277307
setTimeout(function(){
278308
stage.appendChild(svg.domParent);
279309
},1000);
280310
}break;
281311
case "CommentObject":{
282-
312+
var cmt = boundObjects[objname];
313+
cmt.domParent = _("div",{
314+
"className":"cmt",
315+
"style":{
316+
"fontSize":(serialized.fontsize ? serialized.fontsize : 16) + "px",
317+
"position":"absolute",
318+
"top":(serialized.y ? serialized.y : 0) + "px",
319+
"left":(serialized.x ? serialized.x : 0) + "px",
320+
"color":serialized.color
321+
}
322+
}, [_("text",serialized.text ? serialized.text : "")]);
323+
stage.appendChild(cmt.domParent);
283324
}break;
284325
default:break;
285326
}
@@ -294,8 +335,9 @@ CCLScripting = {
294335
};
295336
};
296337

297-
CCLScripting.CommonObject = function(){
338+
CCLScripting.CommonObject = function(className){
298339
this.data = {};
340+
this.className = className;
299341
this.deserialize = function(data){
300342
for(var field in data){
301343
this.data[field] = data[field];
@@ -366,9 +408,21 @@ CCLScripting = {
366408
this.domParent.appendChild(state.lastPath);
367409
};
368410
this.lineTo = function(params){
411+
if(!state.lastPath){
412+
state.lastPath = document.createElementNS("http://www.w3.org/2000/svg", "path");
413+
state.lastPath.setAttribute("d", "M0 0");
414+
applyFill(state.lastPath, this);
415+
applyStroke(state.lastPath, this);
416+
}
369417
state.lastPath.setAttribute("d", state.lastPath.getAttribute("d") + " L" + params.join(" "));
370418
};
371419
this.curveTo = function(params){
420+
if(!state.lastPath){
421+
state.lastPath = document.createElementNS("http://www.w3.org/2000/svg", "path");
422+
state.lastPath.setAttribute("d", "M0 0");
423+
applyFill(state.lastPath, this);
424+
applyStroke(state.lastPath, this);
425+
}
372426
state.lastPath.setAttribute("d", state.lastPath.getAttribute("d") + " Q" + params.join(" "));
373427
};
374428
this.lineStyle = function(params){
@@ -402,12 +456,23 @@ CCLScripting = {
402456
applyStroke(r, this);
403457
this.domParent.appendChild(r);
404458
};
459+
this.drawRoundRect = function(params){
460+
var r = document.createElementNS("http://www.w3.org/2000/svg", "rect");
461+
r.setAttribute("x", params[0]);
462+
r.setAttribute("y", params[1]);
463+
r.setAttribute("width", params[2]);
464+
r.setAttribute("height", params[3]);
465+
r.setAttribute("rx", params[4]);
466+
r.setAttribute("ry", params[5]);
467+
applyFill(r, this);
468+
applyStroke(r, this);
469+
this.domParent.appendChild(r);
470+
};
405471
this.drawCircle = function(params){
406472
var c = document.createElementNS("http://www.w3.org/2000/svg", "circle");
407-
c.setAttribute("x", params[0]);
408-
c.setAttribute("y", params[1]);
409-
c.setAttribute("width", params[2]);
410-
c.setAttribute("height", params[3]);
473+
c.setAttribute("cx", params[0]);
474+
c.setAttribute("cy", params[1]);
475+
c.setAttribute("r", params[2]);
411476
applyFill(c, this);
412477
applyStroke(c, this);
413478
this.domParent.appendChild(c);
Collapse file

‎experimental/scripting/index.htm‎

Copy file name to clipboardExpand all lines: experimental/scripting/index.htm
+7-4Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
position:fixed; bottom:0; left:0; right:0; height:150px; overflow:auto; }
1212
#player{border:1px solid #f88; width:100%;background-color:#100;position:relative;}
1313
#code-input{width:100%;height:340px;display:block; border:1px solid #f88; padding:10px;background:#000;color:#f88;font-size:20px;}
14-
.s-button, .button{display:block; border:1px solid #f88; padding:10px; background:#000; color:#f88; float:left;}
14+
.s-button, .button{display:block; border:1px solid #f88; padding:10px; background:#000; color:#f88; float:left;-moz-user-select: none; -khtml-user-select: none; -webkit-user-select: none; -o-user-select: none; user-select: none; cursor:default;}
1515
.s-button:hover,.button:hover{background:#f88; color:#000;}
16-
.button{font-size:12px;}
16+
.button{font-size:12px;z-index:99;}
1717
</style>
1818
<script type="text/javascript">var $ = function(e){return window.document.getElementById(e);}</script>
1919
<script src="bscript.js" type="text/javascript"></script>
@@ -27,9 +27,9 @@ <h2 style="color:#fff">CCL Scripting Demo</h2>
2727
<textarea id="code-input"></textarea>
2828
</div>
2929
<div id="playerdiv">
30-
<div id="player" style="height:400px;clear:both;"></div>
30+
<div id="player" style="height:400px;clear:both;overflow:hidden;"></div>
3131
</div>
32-
<div id="output" style="color:#fff;"> </div>
32+
<div id="output" style="color:#fff;">[Msg] 有关具体API信息请阅读 docs/scripting</div>
3333
<script type="text/javascript">
3434
bscripter = new CCLScripting.BridgedSandbox({}, $("player"));
3535
bscripter.addEventListener("trace", function(data){
@@ -59,6 +59,9 @@ <h2 style="color:#fff">CCL Scripting Demo</h2>
5959
xhr.open("GET", filename, true);
6060
xhr.send();
6161
}
62+
$("debug-basic").addEventListener("click", function(){
63+
fetchFile("manzoku.biliscript");
64+
});
6265
$("debug-svg-madoka").addEventListener("click", function(){
6366
fetchFile("madoka.biliscript");
6467
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.