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
var base_url = 'https://api.github.com'; | |
var title = document.title; | |
var owner = 'yydai'; | |
var repo = 'yydai.github.io'; | |
var search_issues = base_url + '/search/issues?q=' + title + '+user:' + owner; | |
function test() { | |
jQuery.ajax({ | |
type: 'GET', | |
async: false, | |
dataType:'json', | |
url: search_issues, | |
success:function(data) { | |
result = data; | |
} | |
}); | |
return result; | |
} | |
var result = test(); | |
var items = result.items[0]; | |
if(jQuery.isEmptyObject(items)) { | |
create(title); | |
} else { | |
html_url = items.html_url; | |
document.body.innerHTML += | |
'<div id="comments"><h2>Comments</h2><div id="header">Want to leave a comment? Visit <a href="'+ html_url + '"> this issue page on GitHub</a> (you will need a GitHub account).</div></div>' | |
} | |
function create(title) { | |
var creare = base_url + '/repos/' + owner + '/' + repo + '/issues'; | |
var payload = { | |
'title': title, | |
'body': 'You can write comments in this issues.', | |
'labels': ['blog'], | |
} | |
$.ajax({ | |
type: 'POST', | |
url: create, | |
crossDomain: true, | |
// The key needs to match your method's input parameter (case-sensitive). | |
data: JSON.stringify(payload), | |
headers: { | |
'Authorization': 'Basic eXlkYWk6ZGVpc3Q5MjgxNw==', | |
'Access-Control-Allow-Origin' : '*' | |
}, | |
contentType: 'application/json; charset=utf-8', | |
dataType: 'json', | |
success: function(data){alert('create issue success');}, | |
failure: function(errMsg) { | |
alert('create issue failed'); | |
} | |
}); | |
} | |
if(result.total_count == 1) { | |
var comments_url = result.items[0].comments_url; | |
} else if (result.total_count == 0) { | |
// create a new issue | |
create(title); | |
} else { | |
// result not only | |
alert('Cannot load the comments.'); | |
} | |
function loadComments(data) { | |
for (var i=0; i<data.length; i++) { | |
var cuser = data[i].user.login; | |
var cuserlink = 'https://' + repo + '/' + data[i].user.login; | |
var clink = comments_url + '#issuecomment-' + data[i].url.substring(data[i].url.lastIndexOf("/")+1); | |
var cbody = data[i].body_html; | |
var cavatarlink = data[i].user.avatar_url; | |
var cdate = Date.parse(data[i].created_at).toString('yyyy-MM-dd HH:mm:ss'); | |
var code = '<div class="comment"><div class="commentheader"><div class="commentgravatar">' + '<img src="' + cavatarlink + '" alt="" width="20" height="20">' + '</div><a class="commentuser" href=\""+ cuserlink + "\">' + cuser + '</a><a class="commentdate" href=\"" + clink + "\">' + cdate + '</a></div><div class="commentbody">' + cbody + '</div></div>'; | |
$('#comments').append(code); | |
} | |
} | |
var comments_api = comments_url + '?per_page=100'; | |
$.ajax(comments_api, { | |
headers: {Accept: 'application/vnd.github.full+json'}, | |
dataType: 'json', | |
success: function(msg){ | |
loadComments(msg); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment