Created
October 17, 2013 17:16
-
-
Save motin/7028770 to your computer and use it in GitHub Desktop.
StartHQ provider test code + mailchimp provider. Run this code in the developer tools console while logged in to your mailchimp account.
This file contains 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
// include jquery - http://stackoverflow.com/questions/7474354/include-jquery-in-the-javascript-console | |
var jq = document.createElement('script'); | |
jq.src = "//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"; | |
document.getElementsByTagName('head')[0].appendChild(jq); | |
// ... give time for script to load, then type: | |
jQuery.noConflict(); | |
// define testing function | |
var testProvider = function (provider, term) { | |
// define the parameters that you want to test | |
var query = provider.query.replace('{{term}}', term); | |
var translate = provider.translate; | |
console.log('loading query: ', query); | |
jQuery.get(query, {}, function (response, textStatus, jqXHR) { | |
console.log('parsing response'); | |
// comment to hide response | |
console.log('response: ', response); | |
var parsed = eval('jQuery.' + translate); | |
jQuery.each('name,description,link'.split(','), function (i, val) { | |
console.log('running selector/expression test for: ', val); | |
var selector = provider[val].selector; | |
var expression = provider[val].expression; | |
var results = jQuery(parsed).find(selector); | |
console.log('num results: ', results.length); | |
results.each(function (i, element) { | |
console.log('result ' + i + ':', eval(expression)); | |
}); | |
}); | |
}, 'text'); | |
}; | |
var provider = { | |
type: 'listmember', | |
query: 'https://us7.admin.mailchimp.com/search/all?query={{term}}&offset=0&entities=listmembers', | |
translate: 'parseHTML(JSON.parse(response).html)', | |
icon: { | |
selector: '', | |
expression: 'app.url|icons' | |
}, | |
name: { | |
selector: 'div.member-summary', | |
expression: 'element.childNodes[1].childNodes[1].childNodes[1].childNodes[0].textContent' | |
}, | |
description: { | |
selector: 'div.member-summary', | |
expression: '"E-mail: " + element.childNodes[1].childNodes[1].childNodes[1].childNodes[2].textContent.replace("·","").trim() + ". Campaign: " + element.childNodes[1].childNodes[3].textContent.trim() + ". Details: " + element.childNodes[1].childNodes[5].childNodes[1].textContent.substr(0,element.childNodes[1].childNodes[5].childNodes[1].textContent.indexOf("·") - 2).trim()' | |
}, | |
link: { | |
selector: 'div.member-summary', | |
expression: '"https://us7.admin.mailchimp.com" + element.childNodes[1].childNodes[1].childNodes[1].childNodes[0].getAttribute("href")' | |
} | |
}; | |
// run test | |
testProvider(provider, 'foo'); | |
// sample output: | |
/* | |
loading query: https://us7.admin.mailchimp.com/search/all?query=foo&offset=0&entities=listmembers | |
undefined | |
parsing response | |
response: ..... | |
running selector/expression test for: name | |
num results: 2 | |
result 0: foo bar | |
result 1: bar foo | |
running selector/expression test for: description | |
num results: 2 | |
result 0: E-mail: foo@neam.se. Campaign: Foo. Details: Subscribed on Oct 17, 2013 04:46 pm | |
result 1: E-mail: foobar@neam.se. Campaign: Foo. Details: Subscribed on Oct 17, 2013 04:46 pm | |
running selector/expression test for: link | |
num results: 2 | |
result 0: https://us7.admin.mailchimp.com/lists/members/view?id=87727917 | |
result 1: https://us7.admin.mailchimp.com/lists/members/view?id=87727921 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment