Skip to content

Instantly share code, notes, and snippets.

@arabcoders
Last active August 29, 2015 14:13
Show Gist options
  • Save arabcoders/eec0597dc659ea232103 to your computer and use it in GitHub Desktop.
Save arabcoders/eec0597dc659ea232103 to your computer and use it in GitHub Desktop.
This script is orginally written by Chris Bradbury and i took it and fixed some bugs such as CSP protection in chome since instagram still serve images over HTTP while the CSP in twitter disallow embeding any HTTP content., there is few things i want to do at long term such as using MutationObserver instead of looping and using twitter internal …
// ==UserScript==
// @name Twitter Instagram Cards
// @namespace http://arabcoders.co
// @author ArabCoders, Orginally Written By: Chris Bradbury
// @version 2.0.1
// @description Now that Instagram have pulled their twitter support, this script adds back inline instagram photos, fixed CSP in Chrome 29+
// @include https://twitter.com/*
// @require https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.js
// @grant setTimeout
// @grant GM_xmlhttpRequest
// ==/UserScript==
$documentHeight = '';
debug = false;
function height() {
if (debug) {
console.log('Checking Tweets at Height ' + $(document).height() );
}
if ( $documentHeight != $(document).height() )
{
loadInstagram();
}
if (typeof timeout != 'undefined')
{
window.clearTimeout(timeout);
}
var timeout = window.setTimeout(height, 3000);
}
function loadInstagram(){
$documentHeight = $(document).height();
$('body').find('.js-tweet-text').each( function( key, value ) {
var container = this;
if ( value.innerHTML.indexOf("instagr.am") >= 0 || value.innerHTML.indexOf("instagram.com/p/") >= 0 ) {
if ( value.innerHTML.indexOf('div class="instagram"') >= 0 )
{
}
else
{
if ( value.innerHTML.indexOf("instagr.am") >= 0 )
{
var instaId = value.innerHTML.split('instagr.am/p/');
}
else
{
var instaId = value.innerHTML.split('instagram.com/p/');
}
instaId = instaId[1].split('/"');
instaId = instaId[0];
var imgUrl = 'http://instagram.com/p/'+instaId+'/media/';
GM_xmlhttpRequest ( {
method: 'GET',
url: imgUrl,
onload: function (respDetails) {
var binResp = customBase64Encode(respDetails.responseText);
if (debug) {
console.log('loading Instagram Image ID:' + instaId, imgUrl, binResp);
}
$(container).append('<br clear="both">'
+ '<a onClick=\'$("#' + instaId +'").toggle();\' href="javaScript:void(0);">Open/Close</a><br/>'
+ '<div class="instagram" id="'+ instaId +'" style="margin-top: 10px; margin-bottom: 5px;">'
+ '<img alt="image" width="435" src="data:image/png;base64,' + binResp +'" /></div>'
);
},
overrideMimeType: 'text/plain; charset=x-user-defined'
});
}
}
});
}
function customBase64Encode (inputStr) {
var
bbLen = 3,
enCharLen = 4,
inpLen = inputStr.length,
inx = 0,
jnx,
keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
+ "0123456789+/=",
output = "",
paddingBytes = 0;
var
bytebuffer = new Array (bbLen),
encodedCharIndexes = new Array (enCharLen);
while (inx < inpLen) {
for (jnx = 0; jnx < bbLen; ++jnx) {
if (inx < inpLen)
bytebuffer[jnx] = inputStr.charCodeAt (inx++) & 0xff;
else
bytebuffer[jnx] = 0;
}
encodedCharIndexes[0] = bytebuffer[0] >> 2;
encodedCharIndexes[1] = ( (bytebuffer[0] & 0x3) << 4) | (bytebuffer[1] >> 4);
encodedCharIndexes[2] = ( (bytebuffer[1] & 0x0f) << 2) | (bytebuffer[2] >> 6);
encodedCharIndexes[3] = bytebuffer[2] & 0x3f;
paddingBytes = inx - (inpLen - 1);
switch (paddingBytes) {
case 1:
encodedCharIndexes[3] = 64;
break;
case 2:
encodedCharIndexes[3] = 64;
encodedCharIndexes[2] = 64;
break;
default:
break;
}
for (jnx = 0; jnx < enCharLen; ++jnx)
{
output += keyStr.charAt ( encodedCharIndexes[jnx] );
}
}
return output;
}
$(document).ready(function(){
loadInstagram();
height();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment