Last active
May 13, 2016 17:37
-
-
Save satyr/104659 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
const Name = 'gatherer', Gatherer = 'http://gatherer.wizards.com/', | |
Base = '<style>\ | |
a img {border:none}\ | |
tr {background-color:inherit !important}\ | |
td {padding:0 .2em 1ex !important; width:auto !important}\ | |
.error {font-style:oblique; line-height:1.8}\ | |
.logo {display:inline-block}\ | |
.logo > img {max-width:100%; vertical-align:middle}\ | |
.loading {opacity:0.9}\ | |
.loading + .logo {opacity:0.4}\ | |
.error + .logo {opacity:0.7}\ | |
</style>'+ | |
'<div class='+ Name +'>'+ | |
'<div id='+ Name +'></div>'+ | |
'<a class=logo href='+ Gatherer +' accesskey="/">'+ | |
'<img src=http://gatherer.wizards.com/images/Redesign/Magic_Logo.png>'+ | |
'</a></div>' | |
CmdUtils.CreateCommand({ | |
name: Name, | |
description: 'Gatherer'.link(Gatherer), | |
author: 'satyr', license: 'MIT', | |
icon: Gatherer +'Images/favicon.ico', | |
argument: { | |
name: 'gatherer suggest', | |
label: 'card name', | |
rankLast: true, | |
suggest: function gtrr_suggest(txt, htm, cb, sx){ | |
var ss = [CmdUtils.makeSugg(txt, null, null, .3, sx)] | |
sx || ss.push($.ajax({ | |
url: Gatherer +'Handlers/InlineCardSearch.ashx', | |
data: {nameFragment: txt}, | |
dataType: 'json', | |
success: function nt_gtrrsg_success(o){ | |
var tl = txt.toLowerCase(), ss = [] | |
for each(let c in o.Results){ | |
let {Name} = c | |
if(Name.toLowerCase() === tl) continue | |
let nh = Utils.escapeHtml(Name) | |
ss.push({ | |
text: Name, html: nh, summary: nh +' '+ c.Snippet.small(), | |
score: .25, | |
}) | |
} | |
ss.length && cb(ss) | |
}, | |
})) | |
return ss | |
}, | |
}, | |
execute: function gtrr_execute({object: {text}}){ | |
Utils.openUrlInBrowser(this._req(text) || Gatherer) | |
}, | |
preview: function gtrr_preview(pb, {object: {text}}){ | |
var cn, u = this._req(text) | |
while(!(cn = pb.ownerDocument.getElementById(Name))) pb.innerHTML = Base | |
if(!u) return | |
cn.className += ' loading' | |
CmdUtils.previewAjax(pb, { | |
url: u, | |
success: function gtrr_success(htm){ | |
if(/<img\s[^>]*\bsrc="..\/..\/(Handlers\/Image.*?)"/.test(htm)){ | |
cn.innerHTML = | |
('<img class="card" src="'+ Gatherer + RegExp.$1 + '"/>') | |
.link(Utils.escapeHtml(this.url)) + | |
[/<div\s[^>]*\bid="[^\"]*rulingsContainer"[^]+?<\/table>/.exec(htm)] | |
cn.className = '' | |
return | |
} | |
cn.innerHTML = | |
/<div\s+id=".*?_DidYouMean">[^]+?<\/div>/.test(htm) | |
? CmdUtils.absUrl(RegExp.lastMatch, Gatherer) | |
: htm | |
cn.className = 'error' | |
}, | |
}) | |
}, | |
_req: function gtrr_req(x) { | |
return (x = x.trim()) | |
&& Gatherer +'Pages/Card/Details.aspx?name='+ encodeURIComponent(x) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment