Skip to content

Instantly share code, notes, and snippets.

@myfreeer
Last active June 6, 2017 06:41
Show Gist options
  • Save myfreeer/b94a260ff51a05ee78d4e6a1fd150225 to your computer and use it in GitHub Desktop.
Save myfreeer/b94a260ff51a05ee78d4e6a1fd150225 to your computer and use it in GitHub Desktop.
// ==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