Skip to content

Commit

Permalink
Updating the JS to resolve some issues
Browse files Browse the repository at this point in the history
  • Loading branch information
aarongustafson committed Jan 25, 2017
1 parent c9cfb36 commit 68f6c63
Showing 1 changed file with 33 additions and 20 deletions.
53 changes: 33 additions & 20 deletions webmention_io.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@
'July', 'August', 'September', 'October', 'November', 'December'
],
json_webmentions,
targets = [
window.location.href.replace( 'localhost', 'www.aaron-gustafson.com' )
],
targets = [],
$none = false,
$redirects = document.querySelector('meta[property="webmention:redirected_from"]'),
redirects,
Expand All @@ -52,7 +50,8 @@
$existing_webmentions,
existing_webmentions = [],
e = 0;


// handle redirects
if ( $redirects )
{
redirects = $redirects.getAttribute('content').split(',');
Expand Down Expand Up @@ -102,16 +101,15 @@
while ( e-- )
{
existing_webmentions.push(
parseInt(
$existing_webmentions[e]
.getAttribute( 'id' )
.replace( 'webmention-', '' ),
10
)
$existing_webmentions[e]
.getAttribute( 'id' )
.replace( 'webmention-', '' )
);
}
//console.log(existing_webmentions);
$existing_webmentions = null;
}
window.AG.existing_webmentions = existing_webmentions;

// Set up the markup
elements.li.className = 'webmentions__item';
Expand Down Expand Up @@ -143,6 +141,9 @@
is_tweet = false,
is_gplus = false;

// make sure the id is a string
id = id.toString();

// Tweets gets handled differently
if ( data.url && data.url.indexOf( 'twitter.com/' ) > -1 )
{
Expand All @@ -154,18 +155,19 @@
}
}

// Google Plus gets handled differently
if ( data.url.indexOf( '/googleplus/' ) )
{
is_gplus = true;
}

// No need to replace
//console.log( existing_webmentions, id, existing_webmentions.indexOf( id ) );
if ( existing_webmentions.indexOf( id ) > -1 )
{
return;
}

// Google Plus gets handled differently
if ( data.url.indexOf( '/googleplus/' ) )
{
is_gplus = true;
}

var $item = elements.li.cloneNode( true ),
$mention = elements.article.cloneNode( true ),
$author = elements.author.cloneNode( true ),
Expand Down Expand Up @@ -425,12 +427,22 @@
}

window.AG.processWebmentions = function( data ){
if ( ! ( 'error' in data ) )
if ( data &&
! ( 'error' in data ) )
{
data.links.reverse();
data.links.forEach( addMention );
updateCount();
}
};

// Update the webmentions count
function updateCount() {
var $webmentions_link = document.querySelector( '.entry__jump--webmentions a' ),
webmentions_count = document.querySelectorAll( '.webmentions__item' ).length;

$webmentions_link.innerHTML = webmentions_count + ' webmentions';
}

// Synchromous XHR proxied through whateverorigin.org
function readWebPage( uri, callback )
Expand All @@ -440,7 +452,7 @@
var XHR = new XMLHttpRequest();
readWebPage = function( uri, callback ){
var done = false;
uri = 'http://whateverorigin.org/get?url=' + encodeURIComponent( uri );
uri = '//whateverorigin.org/get?url=' + encodeURIComponent( uri );
XHR.onreadystatechange = function() {
if ( this.readyState == 4 && ! done ) {
done = true;
Expand Down Expand Up @@ -514,8 +526,8 @@
// Load up any unpublished webmentions on load
json_webmentions = document.createElement('script');
json_webmentions.async = true;
json_webmentions.src = '//webmention.io/api/mentions?jsonp=window.AG.processWebmentions&target[]=' +
targets.join( '&target[]=' );
json_webmentions.src = '//webmention.io/api/mentions?jsonp=window.AG.processWebmentions&target[]=' +
targets.join( '&target[]=' );
document.getElementsByTagName('head')[0].appendChild( json_webmentions );

// Listen for new ones
Expand All @@ -530,6 +542,7 @@
};
ws.onmessage = function( event ){
addMention( JSON.parse( event.data ) );
updateCount();
};
}

Expand Down

0 comments on commit 68f6c63

Please sign in to comment.