Skip to content

Commit

Permalink
Fix for Facebook API change
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Potts committed Oct 15, 2017
1 parent 4894edf commit 474ba38
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 135 deletions.
32 changes: 15 additions & 17 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
{
"extends": ["eslint:recommended"],
"env": {
"browser": true
},
"globals": {},
"rules": {
"no-const-assign": "warn",
"no-this-before-super": "warn",
"no-undef": "warn",
"no-unreachable": "warn",
"no-unused-vars": "warn",
"constructor-super": "warn",
"valid-typeof": "warn",
"indent": [
"error",
4,
{
"SwitchCase": 1
}
],
"quotes": ["error", "single"],
"semi": ["error", "always"],
"eqeqeq": ["error", "always"]
"no-const-assign": 1,
"no-this-before-super": 1,
"no-undef": 1,
"no-unreachable": 1,
"no-unused-vars": 1,
"constructor-super": 1,
"valid-typeof": 1,
"indent": [2, 4, { "SwitchCase": 1 }],
"quotes": [2, "single", "avoid-escape"],
"semi": [2, "always"],
"eqeqeq": [2, "always"],
"one-var": [2, "never"],
"comma-dangle": [2, "always-multiline"]
}
}
32 changes: 0 additions & 32 deletions .jsbeautifyrc

This file was deleted.

Empty file removed .jshintignore
Empty file.
56 changes: 0 additions & 56 deletions .jshintrc

This file was deleted.

25 changes: 25 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
// Exclude from the editor
"files.exclude": {
"**/node_modules": true
},

// Exclude from search
"search.exclude": {
"dist/": true
},

// Formatting
"editor.tabSize": 4,
"editor.insertSpaces": true,
"editor.formatOnSave": true,

// Trim on save
"files.trimTrailingWhitespace": true,

// Prettier settings
"prettier.tabWidth": 4,
"prettier.eslintIntegration": true,
"prettier.printWidth": 120,
"prettier.cssEnable": ["css", "less", "scss"]
}
2 changes: 1 addition & 1 deletion dist/shr.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 29 additions & 29 deletions src/js/shr.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
var config;
var storage = {
data: {},
ttl: 0
ttl: 0,
};

// Default config
Expand All @@ -28,12 +28,12 @@
return '<span class="' + classname + ' ' + classname + '--' + position + '">' + count + '</span>';
},
value: {
facebook: 'shares',
github: 'stargazers_count'
facebook: 'share_count',
github: 'stargazers_count',
},
prefix: {
github: 'data'
}
github: 'data',
},
},
urls: {
facebook: function(url) {
Expand All @@ -46,50 +46,42 @@
return (
'https://api.github.com/repos' + repo + (typeof token === 'string' ? '?access_token=' + token : '')
);
}
},
},
popup: {
google: {
width: 500,
height: 500
height: 500,
},
facebook: {
width: 640,
height: 270
height: 270,
},
twitter: {
width: 640,
height: 240
height: 240,
},
pinterest: {
width: 750,
height: 550
}
height: 550,
},
},
storage: {
key: 'shr',
enabled: (function() {
// Try to use local storage (it might be disabled, e.g. user is in private/porn mode)
// see: https://github.com/Selz/plyr/issues/131
// Try to use local storage (it might be disabled, e.g. user is in private mode)
try {
// Add test item
window.localStorage.setItem('___test', 'OK');

// Get the test item
var result = window.localStorage.getItem('___test');

// Clean up
window.localStorage.removeItem('___test');

// Check if value matches
return result === 'OK';
var key = '___test';
window.localStorage.setItem(key, key);
window.localStorage.removeItem(key);
return true;
} catch (e) {
return false;
}
})(),
ttl: 300000 // 5 minutes in milliseconds
ttl: 300000, // 5 minutes in milliseconds
},
tokens: {}
tokens: {},
};

// Debugging
Expand Down Expand Up @@ -203,7 +195,7 @@

// Get URL parameter by name
function getParameterByName(query, name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
name = name.replace(/[[]/, '\\[').replace(/[\]]/, '\\]');

var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(query);
Expand Down Expand Up @@ -237,7 +229,7 @@
if (config.storage.enabled && config.storage.key in window.localStorage) {
storage = {
data: JSON.parse(window.localStorage[config.storage.key]),
ttl: window.localStorage[config.storage.key + '_ttl']
ttl: window.localStorage[config.storage.key + '_ttl'],
};
}
}
Expand Down Expand Up @@ -346,9 +338,17 @@
// eg. GitHub uses data.data.forks, vs facebooks data.shares
data = prefixData(shr.network, data);

var count;
var count = 0;
var custom = shr.link.getAttribute('data-shr-display');

// Facebook changed the schema of their data
switch (shr.network) {
case 'facebook':
data = data.share;
break;
}

// Get value based on config
if (!isNullOrEmpty(custom)) {
count = data[custom];
} else if (shr.network in config.count.value) {
Expand Down

0 comments on commit 474ba38

Please sign in to comment.