Last active
June 6, 2017 06:41
-
-
Save myfreeer/b94a260ff51a05ee78d4e6a1fd150225 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Youku Fix Cna | |
// @namespace myfreeer | |
// @version 0.1 | |
// @description 为Youku提供cna cookie | |
// @author myfreeer | |
// @match http://v.youku.com/* | |
// @match https://v.youku.com/* | |
// @license MIT | |
// @run-at document-start | |
// @grant GM_xmlhttpRequest | |
// ==/UserScript== | |
(function () { | |
'use strict'; | |
const cnaFake = (cna = '') => { | |
// this provides a fake cna | |
const BAES64_TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; | |
while (cna.length < 24) cna += BAES64_TABLE[(Math.random() * 64) | 0]; | |
let cookieDate = new Date(); | |
cookieDate.setTime(cookieDate.getTime() + 864000000000); // 1e4 * 24 * 60 * 60 * 1000 | |
let expires = '; expires=' + cookieDate.toGMTString() + "; path=/"; | |
document.cookie = 'cna=' + cna + expires; | |
}; | |
if (!document.cookie.match(/cna=([^;]+);/)) GM_xmlhttpRequest({ | |
method: 'GET', | |
url: 'https://log.mmstat.com/eg.js', | |
headers: { | |
referer: location.href | |
}, | |
timeout: 3000, | |
onload: e => { | |
if (!(e && e.responseText)) return cnaFake(); | |
let cna = e.responseText.match(/Etag="([^"]+)"/); | |
if (cna && cna[1]) cnaFake(cna[1]); | |
else cnaFake(); | |
}, | |
onerror: e => { | |
cnaFake(); | |
//console.warn(e); | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment