Skip to content

Instantly share code, notes, and snippets.

@hinaloe
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hinaloe/3908ce1e21d4e9924080 to your computer and use it in GitHub Desktop.
Save hinaloe/3908ce1e21d4e9924080 to your computer and use it in GitHub Desktop.
Open TwOrig Image
var isCreatedContextMenu = false,
CONTEXT_ID = "OpenTwOrig";
chrome.runtime.onInstalled.addListener( Run );
chrome.runtime.onStartup.addListener( Run );
function Run()
{
if( isCreatedContextMenu == false)
{
chrome.contextMenus.create
(
{
"type" : "normal",
"id": CONTEXT_ID,
"title": "元画像を開く",
"contexts" : ["image"],
"targetUrlPatterns" : ["*://pbs.twimg.com/media/*"]
}
);
isCreatedContextMenu = true;
}
}
chrome.contextMenus.onClicked.addListener
(
function ( info, tab )
{
if( info.menuItemId == CONTEXT_ID )
{
Download( info.srcUrl );
}
}
);
function Download( src )
{
var a = document.createElement( 'a' );
a.href = ReplaceWithBiggest( src );
a.target = '_blank';
a.dispatchEvent( new CustomEvent( 'click' ) );
}
function ReplaceWithBiggest( src )
{
if( src.indexOf( ":orig" ) != -1 )
{
// none
}
else if( src.indexOf( ":" ) != -1 )
{
var srcSplitColons = src.split(':');
src = srcSplitColons[0] + ":" + srcSplitColons[1] + ":orig";
}
else
{
src = src + ":orig";
}
if( !GetSrcExist( src ) )
{
src = src.replace( ":orig", ":large" );
if( !GetSrcExist( src ) )
{
src = src.replace( ":large", "" );
}
}
return src;
}
function GetSrcExist( src )
{
var xmlHttpRequset = new XMLHttpRequest();
xmlHttpRequset.open( "GET", src, false );
xmlHttpRequset.send();
if( ( xmlHttpRequset.status == 200 ) || ( xmlHttpRequset.status == 500 ) )
{
return true;
}
return false;
}
// EOF
{
"background": {
"persistent": false,
"scripts": [ "event.js" ]
},
"description": "twitter.com のツイート内画像を右クリックした際に、「元画像を開く」メニューを表示します。",
"icons": {
"128": "icon/icon128.png",
"16": "icon/icon16.png",
"48": "icon/icon48.png"
},
"manifest_version": 2,
"name": "Twitter, Open Original Image",
"permissions": [ "contextMenus", "*://pbs.twimg.com/*" ],
"version": "1.0.0"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment