Skip to content

Commit

Permalink
Blogger API v3 Compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jokenox committed Jun 8, 2018
1 parent 89c810f commit 184ddd2
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 137 deletions.
123 changes: 22 additions & 101 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,112 +9,35 @@ El script "limpia" la URL, eliminando de ella la fecha ```"/YYYY/MM"``` o el ```

Para implementar **BloggerJS** en tu blog, copia todo el siguiente código:
```javascript
<script>
// BloggerJS v0.2.1
<script type="text/javascript">
// BloggerJS v0.3.0
// Copyright (c) 2017-2018 Kenny Cruz
// Licensed under the MIT License

// Configuration -----------
var postsDatePrefix = false;
var accessOnly = false;
// -------------------------

var postsOrPages = ["posts", "pages"],
amp = "&amp;".substring(0, 1),
urlTotal, jsonIndex = 1,
secondRequest = true,
feedPriority = 0;

function urlVal(){
var url = window.location.pathname;
var length = url.length;
var urlEnd = url.substring(length - 5);
if(urlEnd === ".html") return 0;
else if(length > 1) return 1;
else return 2;
}

function urlMod(){
var url = window.location.pathname;
if(url.substring(1, 2) === "p"){
url = url.substring(url.indexOf("/",1) + 1);
url = url.substr(0, url.indexOf(".html"));
history.replaceState(null, null, "../" + url);
}
else{
if(!postsDatePrefix) url = url.substring(url.indexOf("/",7) + 1);
else url = url.substring(1);
url = url.substr(0, url.indexOf(".html"));
history.replaceState(null, null, "../../" + url);
}
}

function urlSearch(url, database){
var pathname = url + ".html";
database.forEach(function(element){
var search = element.search(pathname);
if(search !== -1) window.location = element;
});
}

function urlManager(){
var validation = urlVal();
if(validation === 0){
if(!accessOnly) urlMod();
}
else if(validation === 1){
if(!postsDatePrefix) getJSON(postsOrPages[feedPriority], 1);
else getJSON("posts", 1);
}
else if(validation === 2){
if(!accessOnly) history.replaceState(null, null, "/");
}
}

function getJSON(postsOrPages, index){
var script = document.createElement('script');
var jsonUrl = window.location.protocol + "//" + window.location.hostname + "/feeds/" + postsOrPages + "/default?start-index=" + index + "#max-results=150#orderby=published#alt=json-in-script#callback=bloggerJSON";
jsonUrl = jsonUrl.replace(/#/g, amp);
script.type = 'text/javascript';
script.src = jsonUrl;
document.getElementsByTagName('head')[0].appendChild(script);
}

function bloggerJSON(json){
var database = [];
if(urlTotal === undefined) urlTotal = parseInt(json.feed.openSearch$totalResults.$t);
json.feed.entry.forEach(function(element, index){
var entry = json.feed.entry[index];
entry.link.forEach(function(element, index){
if(entry.link[index].rel === "alternate") database.push(entry.link[index].href);
});
});
urlSearch(window.location.pathname, database);
if(urlTotal > 150){
jsonIndex += 150;
urlTotal -= 150;
getJSON(postsOrPages[feedPriority], jsonIndex);
}
else if(secondRequest){
urlTotal = undefined;
jsonIndex = 1;
secondRequest = false;
if(feedPriority === 0){
feedPriority = 1;
getJSON("pages", 1);
}
else if(feedPriority === 1){
feedPriority = 0;
getJSON("posts", 1);
}
}
}

function bloggerJS(priority){
if(priority) feedPriority = priority;
urlManager();
}

var useApiV3 = false;
var apiKey = "";
var blogId = "";
// -------------------------
var postsOrPages=["posts","pages"],urlTotal,jsonIndex=1,secondRequest=!0,feedPriority=0,amp="&amp;"[0],nextPageToken;function urlVal(){var url=window.location.pathname;var length=url.length;var urlEnd=url.substring(length-5);if(urlEnd===".html")return 0;else if(length>1)return 1;else return 2}
function urlMod(){var url=window.location.pathname;if(url.substring(1,2)==="p"){url=url.substring(url.indexOf("/",1)+1);url=url.substr(0,url.indexOf(".html"));history.replaceState(null,null,"../"+url)}
else{if(!postsDatePrefix)url=url.substring(url.indexOf("/",7)+1);else url=url.substring(1);url=url.substr(0,url.indexOf(".html"));history.replaceState(null,null,"../../"+url)}}
function urlSearch(url,database){var pathname=url+".html";database.forEach(function(element){var search=element.search(pathname);if(search!==-1)window.location=element})}
function urlManager(){var validation=urlVal();if(validation===0){if(!accessOnly)urlMod()}
else if(validation===1){if(!postsDatePrefix)getJSON(postsOrPages[feedPriority],1);else getJSON("posts",1)}
else if(validation===2){if(!accessOnly)history.replaceState(null,null,"/")}}
function getJSON(postsOrPages,index){var script=document.createElement('script');if(useApiV3){var jsonUrl="https://www.googleapis.com/blogger/v3/blogs/"+blogId+"/"+postsOrPages+"?key="+apiKey+"#maxResults=500#fields=nextPageToken%2Citems(url)#callback=bloggerJSON";if(nextPageToken)jsonUrl+="#pageToken="+nextPageToken;nextPageToken=undefined}
else var jsonUrl=window.location.protocol+"//"+window.location.hostname+"/feeds/"+postsOrPages+"/default?start-index="+index+"#max-results=150#orderby=published#alt=json-in-script#callback=bloggerJSON";jsonUrl=jsonUrl.replace(/#/g,amp);script.type='text/javascript';script.src=jsonUrl;document.getElementsByTagName('head')[0].appendChild(script)}
function bloggerJSON(json){var database=[];if(!useApiV3)if(urlTotal===undefined)urlTotal=parseInt(json.feed.openSearch$totalResults.$t);if(!useApiV3){json.feed.entry.forEach(function(element,index){var entry=json.feed.entry[index];entry.link.forEach(function(element,index){if(entry.link[index].rel==="alternate")database.push(entry.link[index].href)})})}
else{json.items.forEach(function(element,index){database.push(element.url)});nextPageToken=json.nextPageToken}
urlSearch(window.location.pathname,database);if(urlTotal>150){jsonIndex+=150;urlTotal-=150;getJSON(postsOrPages[feedPriority],jsonIndex)}
else if(nextPageToken){getJSON(postsOrPages[feedPriority])}
else if(secondRequest){nextPageToken=undefined;urlTotal=undefined;jsonIndex=1;secondRequest=!1;if(feedPriority===0){feedPriority=1;getJSON("pages",1)}
else if(feedPriority===1){feedPriority=0;getJSON("posts",1)}}}
function bloggerJS(priority){if(priority)feedPriority=priority;urlManager()}
bloggerJS();
</script>
```
Expand All @@ -128,8 +51,6 @@ Ya que copiaste completamente el código anterior, dirígete al código HTML de
```
Una vez hecho esto, sólo guarda los cambios hechos a tu plantilla. Después de ello, **BloggerJS** estará funcionando.

Recuerda que también en su lugar, puedes usar la versión minificada de BloggerJS, que se encuentra en el archivo ```blogger.min.js``` de este repositorio.

## Licencia
Licensed under the [MIT License](./LICENSE).<br/>
Copyright (c) 2017-2018 [Kenny Cruz](https://github.com/jokenox).
72 changes: 37 additions & 35 deletions blogger.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// BloggerJS v0.2.1
// BloggerJS v0.3.0
// Copyright (c) 2017-2018 Kenny Cruz
// Licensed under the MIT License

Expand All @@ -11,36 +11,37 @@ var postsDatePrefix = false;
// mas no en su funcionamiento general.
var accessOnly = false;

// Usar API v3 de Blogger
var useApiV3 = false;
var apiKey = "";
var blogId = "";

// -------------------------

var postsOrPages = ["posts", "pages"],
amp = "&amp;".substring(0, 1),
urlTotal, jsonIndex = 1,
secondRequest = true,
feedPriority = 0;
feedPriority = 0,
amp = "&amp;"[0],
nextPageToken;

// urlVal();
// Valida si la URL corresponde a un post/página, si no,
// o si corresponde al index.
function urlVal(){

var url = window.location.pathname;
var length = url.length;
var urlEnd = url.substring(length - 5);

if(urlEnd === ".html") return 0;
else if(length > 1) return 1;
else return 2;

}

// urlMod();
// Analiza la URL para identificar si se trata de un post o una página,
// para después modificarla eliminando la fecha o el "/p/", así como el ".html".
function urlMod(){

var url = window.location.pathname;

if(url.substring(1, 2) === "p"){
url = url.substring(url.indexOf("/",1) + 1);
url = url.substr(0, url.indexOf(".html"));
Expand All @@ -52,30 +53,24 @@ function urlMod(){
url = url.substr(0, url.indexOf(".html"));
history.replaceState(null, null, "../../" + url);
}

}

// urlSearch(url, database);
// Busca una url específica en la base de datos, si la encuentra,
// entonces dirigirá a ella.
function urlSearch(url, database){

var pathname = url + ".html";

database.forEach(function(element){
var search = element.search(pathname);
if(search !== -1) window.location = element;
});

}

// urlManager(database, id);
// Ejecuta una validación de URL, para determinar con el resultado
// la acción a realizar (modificarla o buscarla en el feed del blog).
function urlManager(){

var validation = urlVal();

if(validation === 0){
if(!accessOnly) urlMod();
}
Expand All @@ -86,39 +81,46 @@ function urlManager(){
else if(validation === 2){
if(!accessOnly) history.replaceState(null, null, "/");
}

}

// getJSON();
// Realiza una petición al feed y obtiene los datos en formato JSON,
// y los envía mediante un callback.
// Realiza una petición de datos donde vienen las URLs
// y los pasa mediante un callback.
function getJSON(postsOrPages, index){

var script = document.createElement('script');
var jsonUrl = window.location.protocol + "//" + window.location.hostname + "/feeds/" + postsOrPages + "/default?start-index=" + index + "#max-results=150#orderby=published#alt=json-in-script#callback=bloggerJSON";
if(useApiV3){
var jsonUrl = "https://www.googleapis.com/blogger/v3/blogs/" + blogId + "/" + postsOrPages + "?key=" + apiKey + "#maxResults=500#fields=nextPageToken%2Citems(url)#callback=bloggerJSON";
if(nextPageToken) jsonUrl += "#pageToken=" + nextPageToken;
nextPageToken = undefined;
}
else var jsonUrl = window.location.protocol + "//" + window.location.hostname + "/feeds/" + postsOrPages + "/default?start-index=" + index + "#max-results=150#orderby=published#alt=json-in-script#callback=bloggerJSON";
jsonUrl = jsonUrl.replace(/#/g, amp);

script.type = 'text/javascript';
script.src = jsonUrl;
document.getElementsByTagName('head')[0].appendChild(script);

}

// bloggerJSON();
// Obtiene las URL del feed en formato JSON
// y las envía para comparar la URL actual.
// Obtiene datos en formato JSON, los clasifica
// y los envía para comparar la URL actual.
function bloggerJSON(json){

var database = [];

if(urlTotal === undefined) urlTotal = parseInt(json.feed.openSearch$totalResults.$t);

json.feed.entry.forEach(function(element, index){
var entry = json.feed.entry[index];
entry.link.forEach(function(element, index){
if(entry.link[index].rel === "alternate") database.push(entry.link[index].href);
if(!useApiV3) if(urlTotal === undefined) urlTotal = parseInt(json.feed.openSearch$totalResults.$t);
if(!useApiV3){
json.feed.entry.forEach(function(element, index){
var entry = json.feed.entry[index];
entry.link.forEach(function(element, index){
if(entry.link[index].rel === "alternate") database.push(entry.link[index].href);
});
});
});
}
else{
json.items.forEach(function(element, index){
database.push(element.url);
});
nextPageToken = json.nextPageToken;
}

urlSearch(window.location.pathname, database);

Expand All @@ -127,7 +129,11 @@ function bloggerJSON(json){
urlTotal -= 150;
getJSON(postsOrPages[feedPriority], jsonIndex);
}
else if(nextPageToken){
getJSON(postsOrPages[feedPriority]);
}
else if(secondRequest){
nextPageToken = undefined;
urlTotal = undefined;
jsonIndex = 1;
secondRequest = false;
Expand All @@ -140,7 +146,6 @@ function bloggerJSON(json){
getJSON("posts", 1);
}
}

}

// bloggerJS();
Expand All @@ -149,9 +154,6 @@ function bloggerJSON(json){
// es decir, si iniciará a comparar contra las entradas o las páginas.
// 0 ó vacío = Entradas, 1 = Páginas.
function bloggerJS(priority){

if(priority) feedPriority = priority;

urlManager();

}
18 changes: 17 additions & 1 deletion blogger.min.js

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

0 comments on commit 184ddd2

Please sign in to comment.