Skip to main content

Posts

Showing posts with the label javascript

Converting synced C/C++ into asynced JavaScript

Emscripten is a C/C++ -> LLVM -> JavaScript compiler. It's useful and interesting, but one of its biggest limitations is about the sync/async model: JavaScript is single-threaded event-driven languages, therefore you cannot do a sleep() in the hope of receiving and processing extra events. Demo First of all, please enjoy this demo , on which I really spent some time. tl;dr Motivation: to make `sleep` and other similar functions actually work in our Runtime, without lots of labor work porting the C/C++ code I actually made it worked  `sync->async` is doable and I think it's not too hard - there are already implementations for JS I think this is a feature that cannot be supported by merely an external library Does it fit emscripten? Or is it possible to write a plugin for emscripten? Demo explained(?) The demo is basically a stop-time animation: //draw something //wait for some while //draw something else //wait... You can verify this ...

dojo学习笔记 (一)

最近几天开始学习dojo 0.9.0, 主要是看它的源码和测试样例, 以下是这几天的研究成果 一、 dojo 概述 我用的是dojo 0.9.0, 下载压缩包解压后有四个文件夹, 依次为: dijit: 提供了很多ui控件, 被称为widget。 dojo: 提供了很多跨浏览器的实用函数, dojox: 似乎是提供了一些插件性质的东西, 我只对dojox.flash有点了解 util:里面只有个doh, 功能不明 二、 dojo库的使用 首要的是在html里导入dojo.js文件, dojo加载支持很多参数, 其中我目前用到的有parseOnLoad和isDebug. 设定参数的方法有两种, 一种是在导入dojo.js前通过javascript建立一个对象djConfig,包含要设定的键值对,如djConfig={isDebug:true}; 另一种是在<script>标签里加入djConfig属性,值为JSON语法的键值对, 如<script djConfig="isDebug:true" src="dojo.js"></script> 一开始我对于后一种方法感到很神奇, 起初我以为javascript可以获得自己所在标签的信息, 后来看dojo.js, 查找djConfig, 发现它一上来就判断djConfig是否存在, 否则就建立新值,这时我认为djConfig标签可以作为一个对象在javascript中可以访问, 但亲自尝试后发现并不行。于是我甚至认为这是dojo的一个bug, 但在随后的研究中我发现它确实起作用,于是不得不再认真得看dojo.js(当然是dojo.js.uncompressed.js), 发现原来它会遍历所有script标签,并对导入dojo的标签的djConfig进行处理, 实际上就是加上一对括号然后eval一下。 此时我才恍然大悟。 再说说那两个参数, isDebug不必多说, parseOnLoad是指定dojo.js是否在html加载完毕后进行扫描,识别通过标记方法声明的对象(比如dijit的widget就都支持这种声明, 十分方便)如<div dojoType=dijit.form.Menu> ...

Firefox 中 xmlhttprequest 跨域访问

今天要写一个小爬虫,用xmlhttprequest批量抓取一个图片集的链接,不过firefox有安全限制,对跨域访问有限制,需要权限提升。 记得在《Ajax in Action》曾看到过,现在忘了,在网上竟然查了半天才找到,(原以为Google第一个就是呢) 只需在xmlhttprequest作用域内加入 netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); 即可。 参考: http://www.captain.at/howto-ajax-permission-denied-xmlhttprequest.php

在Blogger首页显示文章摘要二解

在首页显示摘要是个不错的主意,可惜Blogger默认不支持. 网上倒是有一些解决方案,不过我都不大喜欢,因为需要修改文章模板,每次创建文章时还要指定哪里是摘要,哪里是全文,不爽. 我希望摘要的生成是自动的,可且应当便于修改. 网上好像有个这样的脚本,不过我网络不好,下不来... 经过两天的研究,我完成了两种方案. 方案一,根据行数定摘要. 修改模板, 加入CSS /* added by WangLu for summary display */ div.wl_summary{ height: 10em; max-height: 10em; overflow-y: hidden; } 另外在post-body的定义中,把<p><data:post.body/></p> 改为 <!-- added by WangLu --> <!-- display summary on the main page --> <b:if cond='data:blog.pageType == "item"'> <p><data:post.body/></p> <b:else/> <p> <div class='wl_summary'> <data:post.body/> </div> </p> ... <br/> <br/> <a expr:href='data:post.url'>Read more...</a> </b:if> 就行了. 此法优点是改动少,速度快,代码简单,且发文时无需添加额外信息 不过需要说明几点: 1. 在Firefox里指定height或max-height都可以达到效果,但是IE不支持max-height 2. 关于height的值可能需要根据实际字体进行修改, 设置不当容易出现半个字的情况. 3. 即使文章很短,也会出现'...'和Read more字样,逻辑上有点小问题. ...