Skip to content

Commit

Permalink
添加ajax 优化代码 添加safari插件
Browse files Browse the repository at this point in the history
  • Loading branch information
zythum committed Mar 18, 2015
1 parent 1792119 commit 28a6bf4
Show file tree
Hide file tree
Showing 28 changed files with 318 additions and 74 deletions.
4 changes: 2 additions & 2 deletions dest/index.js

Large diffs are not rendered by default.

77 changes: 77 additions & 0 deletions extension/mama2.safariextension/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Author</key>
<string>zythum_朱一</string>
<key>Builder Version</key>
<string>10600.3.18</string>
<key>CFBundleDisplayName</key>
<string>mama2</string>
<key>CFBundleIdentifier</key>
<string>com.zythum.mama2</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>Chrome</key>
<dict>
<key>Global Page</key>
<string>bg.html</string>
<key>Toolbar Items</key>
<array>
<dict>
<key>Command</key>
<string>MAMA2</string>
<key>Identifier</key>
<string>MAMA2</string>
<key>Image</key>
<string>mama2.png</string>
<key>Include By Default</key>
<true/>
<key>Label</key>
<string>MAMA2</string>
<key>Palette Label</key>
<string>MAMA2</string>
<key>Tool Tip</key>
<string>MAMA2</string>
</dict>
</array>
</dict>
<key>Content</key>
<dict>
<key>Scripts</key>
<dict>
<key>Start</key>
<array>
<string>script.js</string>
</array>
</dict>
<key>Whitelist</key>
<array>
<string>http://*/*</string>
<string>https://*/*</string>
</array>
</dict>
<key>Description</key>
<string>妈妈再也不用担心我的macbook发热计划</string>
<key>DeveloperIdentifier</key>
<string>28M925B2TX</string>
<key>ExtensionInfoDictionaryVersion</key>
<string>1.0</string>
<key>Permissions</key>
<dict>
<key>Website Access</key>
<dict>
<key>Include Secure Pages</key>
<true/>
<key>Level</key>
<string>All</string>
</dict>
</dict>
<key>Website</key>
<string>http://zythum.github.io/mama2/</string>
</dict>
</plist>
16 changes: 16 additions & 0 deletions extension/mama2.safariextension/bg.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>MAMA2</title>
</head>
<body>
<script type="text/javascript">
safari.application.addEventListener("command", function (e) {
if(e.command == 'MAMA2'){
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage('MAMA2');
}
}, false);
</script>
</body>
</html>
Binary file added extension/mama2.safariextension/mama2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions extension/mama2.safariextension/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var url = 'http://localhost:8000/dest/index.js'
safari.self.addEventListener("message", function(theMessageEvent){
if(theMessageEvent.name == 'MAMA2'){
if(window === window.top){
(function(s){s=document.body.appendChild(document.createElement('script'));s.src=url+'?ts='+Date.now();s.charset='UTF-8';}())
}
}
}, false);
Binary file removed images/dev_ls.png
Binary file not shown.
Binary file added images/ll.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/mama2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/mama2.psd
Binary file not shown.
Binary file removed images/mama2.sketch/Data
Binary file not shown.
Binary file added images/new.sketch/Data
Binary file not shown.
7 changes: 2 additions & 5 deletions images/mama2.sketch/metadata → images/new.sketch/metadata
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@
<key>commit</key>
<string>b2079fe10151ad0eef6cc553b7369ec78d67b9b5</string>
<key>fonts</key>
<array>
<string>AvenirNext-DemiBold</string>
<string>AvenirNext-Heavy</string>
</array>
<array/>
<key>length</key>
<integer>7420</integer>
<integer>23781</integer>
<key>version</key>
<integer>37</integer>
</dict>
Expand Down
File renamed without changes.
22 changes: 12 additions & 10 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
<title>MAMA2-妈妈再也不用担心我的macbook发热了计划2</title>
<style>
*{margin:0; padding:0;}
body {background: #FDF9F9; font-family: "PingHei","Lucida Grande", "Lucida Sans Unicode", "STHeiti", "Helvetica","Arial","Verdana","sans-serif"; font-size:16px;}
body {background: #FDF9F9; font-family: "PingHei","Lucida Grande", "Lucida Sans Unicode", "STHeiti", "Helvetica","Arial","Verdana","sans-serif"; font-size:16px; color: #333333;}
.frame{width: 720px; margin:0 auto 30px;}
.main {text-align:center;padding-top: 50px;}
.main h1 {padding: 20px 0 40px;letter-spacing:-2px;}
.main h1 {padding: 40px 0 40px;letter-spacing:-2px;}
.card { border-radius: 3px;box-shadow: 0 0 2px rgba(0,0,0,0.4); background: #ffffff;margin:0 100px;padding:20px;}
p {line-height: 28px; text-align: left; }
p .first-letter {font-size: 60px;float:left;vertical-align:top;margin:10px;font-weight: bold;}
.main ul {margin-top: 20px;}
.main ul li{list-style-type: none; color: #DF6558; text-align: left; width: 300px; margin: 10px auto; font-weight: bold;}
.main ul li span {float:right;}
.main ul li span:before {content: "@";}
.main ul li a { text-decoration: none; color: #333333; }
hr {height:0; border:0; border-top: 1px solid rgba(0,0,0,0.1); margin: 10px 5px;}
.text-left { text-align: left; }
.text-center { text-align: center; }
Expand All @@ -33,7 +35,7 @@
</head>
<body>
<div class="frame main">
<img src="./images/mama2.png" alt="">
<img src="./images/mama2.png" width="300" style="opacity:0.8">
<h1 class="text-gray">
妈妈再也不用担心我的macbook发热了计划2
</h1>
Expand All @@ -42,11 +44,11 @@ <h1 class="text-gray">
<span class="first-letter"></span>妈计划用于解决在看视频网站时macbook发热严重的问题。使用video来替换原来的flash播放器。
</p>
<hr>
<p class="text-center">目前网站支持列表:</p>
<p class="text-center text-title">目前网站支持列表:</p>
<ul>
<li><span>朱一</span>哔哩哔哩弹幕视频网</li>
<li><span>朱一</span>优酷-中国领先视频网站</li>
<li><span>朱一</span>土豆-每个人都是生活的导演</li>
<li><span>朱一</span><a href="http://www.bilibili.com">哔哩哔哩弹幕视频网</a></li>
<li><span>朱一</span><a href="http://www.youku.com">优酷-中国领先视频网站</a></li>
<li><span>朱一</span><a href="http://www.tudou.com">土豆-每个人都是生活的导演</a></li>
</ul>
</div>
</div>
Expand All @@ -63,8 +65,8 @@ <h1 class="text-gray">
<p class="text-center text-title">使用 浏览器插件</p>
<hr>
<p class="text-center">
<a href="" class="text-margin disable"><img src="./images/safari.jpg" alt=""></a>
<a href="" class="disable"><img src="./images/chrome.jpg" alt=""></a>
<a href="" class="text-margin disable"><img src="./images/safari.jpg"></a>
<a href="" class="disable"><img src="./images/chrome.jpg"></a>
</p>
</div>
</div>
Expand Down Expand Up @@ -121,7 +123,7 @@ <h1 class="text-gray">
<hr/>
<p class="text-center">
现在的目录结构是这样的
<img class="image" src="./images/dev_ls.png" alt="">
<img class="image" src="./images/ll.png">
</p>
<hr/>
<p>
Expand Down
77 changes: 77 additions & 0 deletions src/ajax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* #function ajax#
* < {
* url: String 请求地址
* param: Object 请求参数.可缺省
* method: String 请求方法GET,POST,etc. 可缺省,默认是GET
* callback: Function 请求的callback, 如果失败返回-1, 成功返回内容
* contentType: String 返回内容的格式。如果是JOSN会做JSON Parse, 可缺省,默认是json
* context: Any callback回调函数的this指向。可缺省
* }
* 用于发起ajax或者jsonp请求
*/

var jsonp = require('./jsonp')
var noop = require('./noop')
var queryString = require('./queryString')

function defalutOption (option, defalutValue) {
return option === undefined ? defalutValue : option
}

function queryString (obj) {
var query = []
for (one in obj) {
if (obj.hasOwnProperty(one)) {
query.push([one, obj[one]].join('='))
}
}
return query.join('&')
}

function joinUrl (url, queryString) {
return url + (url.indexOf('?') ? '?' : '&') + queryString
}

function ajax (options) {
var url = defalutOption(options.url, '')
var query = queryString( defalutOption(options.param, {}) )
var method = defalutOption(options.method, 'GET')
var callback = defalutOption(options.callback, noop)
var contentType = defalutOption(options.contentType, 'json')
var context = defalutOption(options.context, null)

if (options.jsonp) {
return jsonp(
joinUrl(url, query),
callback.bind(context),
typeof options.jsonp === 'string' ? options.jsonp : undefined
)
}

var xhr = new XMLHttpRequest()
if (method === 'get') {
url = joinUrl(url, query)
query = ''
}
xhr.open(method, url, true)
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
xhr.send(query)
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 ) {
if (xhr.status === 200) {
var data = request.responseText
if (contentType.toLowerCase() === 'json') {
try {
data = JSON.parse(data)
} catch(e) {
data = -1
}
}
return callback.call(context, data)
} else {
return callback.call(context, -1)
}
}
}
}
module.exports = ajax;
4 changes: 4 additions & 0 deletions src/canPlayM3U8.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
/* #Bool canPlayM3U8#
* 返回浏览器是否支持m3u8格式的视频播放。
* 目前chrome,firefox只支持mp4
*/
module.exports = !!document.createElement('video').canPlayType('application/x-mpegURL')
3 changes: 3 additions & 0 deletions src/createElement.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/*
* 用于简单创建html节点
*/
function createElement (tagName, attributes) {
var element = document.createElement(tagName)
if ( typeof attributes === 'function' ) {
Expand Down
4 changes: 3 additions & 1 deletion src/flashBlocker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

/*
* 用于屏蔽页面上的所有flash
*/
var flashText = '<div style="text-shadow:0 0 2px #eee;letter-spacing:-1px;background:#eee;font-weight:bold;padding:0;font-family:arial,sans-serif;font-size:30px;color:#ccc;width:152px;height:52px;border:4px solid #ccc;border-radius:12px;position:absolute;top:50%;left:50%;margin:-30px 0 0 -80px;text-align:center;line-height:52px;">Flash</div>';

var count = 0;
Expand Down
64 changes: 26 additions & 38 deletions src/httpProxy.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,38 @@
/* #function httpProxy#
* < String 请求地址
* < String 请求方法GET,POST,etc.
* < Object 请求参数
* < Function 请求的callback, 如果失败返回-1, 成功返回内容
* < {
* xml: Bool 是否需要做xml2json 可缺省, 默认fasle
* gzinflate: Bool 是否需要做gzinflate 可缺省, 默认fasle
* context: Any callback回调的this指向 可缺省
* }
* }
* 用于发起跨域的ajax请求。既接口返回跨域又不支持jsonp协议的
*/

var createElement = require('./createElement')
var ajax = require('./ajax')
var queryString = require('./queryString')

var proxyUrl = 'http://zythum.sinaapp.com/mama2/proxy.php'

var callbackPrefix = 'MAMA2_HTTP_PROXY_CALLBACK'
var callbackCount = 0
var timeoutDelay = 10000

function noop () {}

function callbackHandle () {
return callbackPrefix + callbackCount++
}

function queryString (obj) {
var query = []
for (one in obj) {
if (obj.hasOwnProperty(one)) {
query.push([one, obj[one]].join('='))
}
}
return query.join('&')
}

function httpProxy (url, type, params, callback, opts) {
opts = opts || {}
var _callbackHandle = callbackHandle()
window[_callbackHandle] = function (rs) {
clearTimeout(timeoutTimer)
window[_callbackHandle] = noop
callback(rs)
document.body.removeChild(script)
}
var timeoutTimer = setTimeout(function () {
window[_callbackHandle](-1)
}, timeoutDelay)

var script = createElement('script', {
appendTo: document.body,
src: proxyUrl + '?' + queryString({
ajax({
url: proxyUrl,
param : {
params: encodeURIComponent(queryString(params)),//上行参数

url: encodeURIComponent(url),
post: type === 'post' ? 1 : 0,
params: encodeURIComponent(queryString(params)),
callback: _callbackHandle,
post: type === 'post' ? 1 : 0,
xml: opts.xml ? 1 : 0,
gzinflate: opts.gzinflate ? 1 : 0
})
},
jsonp: true,
callback: callback,
context: opts.context
})
}

Expand Down
5 changes: 2 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
var flashBlocker = require('./flashBlocker')

var createElement = require('./createElement')
var player = require('./player')
var log = require('./log')

var mamaKey = 'MAMAKEY_田琴是这个世界上最可爱的女孩子呵呵呵呵,我要让全世界都在知道'
var mamaKey = require('./mamaKey')
var matched

if (window[mamaKey] != true) {
Expand Down Expand Up @@ -82,6 +80,7 @@ function seeked (source, comments) {
delete window[mamaKey]
}
var player = new MAMAPlayer('MAMA2_video_placeHolder', '800x400', source, comments)
player.iframe.contentWindow.focus();
flashBlocker()
player.iframe.style.display = 'block'
}
Expand Down
6 changes: 4 additions & 2 deletions src/jsonp.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* #function jsonp#
* jsonp方法。推荐使用ajax方法。ajax包含了jsonp
*/
var createElement = require('./createElement')
var noop = require('./noop')

var callbackPrefix = 'MAMA2_HTTP_JSONP_CALLBACK'
var callbackCount = 0
var timeoutDelay = 10000

function noop () {}

function callbackHandle () {
return callbackPrefix + callbackCount++
}
Expand Down
Loading

0 comments on commit 28a6bf4

Please sign in to comment.