Skip to content

Commit 3df5102

Browse files
committedSep 8, 2018
Code rewriting and change to feed summary version
1 parent 1ae5c76 commit 3df5102

File tree

5 files changed

+121
-135
lines changed

5 files changed

+121
-135
lines changed
 

‎README.md

+24-32
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,45 @@
1-
<p align="center"><a href="#" target="_blank"><img width="100" src="/resources/logo.svg"></a></p>
1+
<p align="center"><a href="#" target="_blank"><img width="100" src="/img/logo.svg"></a></p>
22
<h1>BloggerJS</h1>
33
<p>
44
<a href="./LICENSE"><img src="https://img.shields.io/badge/license-MIT-brightgreen.svg"></a>
5-
<a href="https://github.com/jokenox/bloggerjs/tree/2ea81a1"><img src="https://img.shields.io/badge/latest%20ver-v0.3.1-orange.svg"></a>
5+
<a href="https://github.com/jokenox/bloggerjs/tree/2ea81a1"><img src="https://img.shields.io/badge/latest%20ver-v0.4.0-orange.svg"></a>
66
</p>
77

88
**BloggerJS** es un script para modificar el formato de las URL en un blog de Blogger. Creando visualmente una mejor navegación.<br/><br/>
99
El script se integra a tu blog y "limpia" las URLs en todo el sitio durante la navegación, eliminando de ellas la fecha ```"/YYYY/MM"``` o el ```"/p"```, según sea el caso, así como también el ```".html"```.
1010
También de esta manera resulta más cómodo compartir una URL, pues luce mucho mejor.<br/>
11-
<p><img src="/resources/url_demo.png"></p>
11+
<p><img src="/img/url_demo.png"></p>
1212

1313
## Implementación
1414

1515
Para implementar **BloggerJS** en tu blog, copia todo el siguiente código:
1616
```javascript
1717
<script type="text/javascript">
18-
// BloggerJS v0.3.1
19-
// Copyright (c) 2017-2018 Kenny Cruz
18+
// BloggerJS v0.4.0
2019
// Licensed under the MIT License
20+
// Copyright (c) 2017-2018 Kenny Cruz
21+
// github.com/jokenox
2122

22-
// Configuration -----------
23-
var postsDatePrefix = false;
24-
var accessOnly = false;
23+
// Configuration
24+
var config = {
25+
postsDatePrefix: false,
26+
accessOnly: false,
2527

26-
var useApiV3 = false;
27-
var apiKey = "";
28-
var blogId = "";
29-
// -------------------------
30-
var postsOrPages=["pages","posts"],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}
31-
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)}
32-
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)}}
28+
useApiV3: false,
29+
apiKey: "YOUR-API-KEY-HERE"
30+
}
31+
var postsOrPages=["pages","posts"],blogId="<data:blog.blogId/>",urlTotal,fetchIndex=1,ampChar="&amp;"[0],secondRequest=!0,feedPriority=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}
32+
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(!config.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)}}
3333
function urlSearch(url,database){var pathname=url+".html";database.forEach(function(element){var search=element.search(pathname);if(search!==-1)window.location=element})}
34-
function urlManager(){var validation=urlVal();if(validation===0){if(!accessOnly)urlMod()}
35-
else if(validation===1){getJSON(postsOrPages[feedPriority],1)}
36-
else if(validation===2){if(!accessOnly)history.replaceState(null,null,"/")}}
37-
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}
38-
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)}
39-
function bloggerJSON(json){var database=[];if(!useApiV3)if(urlTotal===undefined)urlTotal=parseInt(json.feed.openSearch$totalResults.$t);if(!useApiV3){try{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)})})}
40-
catch(e){}}
41-
else{try{json.items.forEach(function(element,index){database.push(element.url)})}
42-
catch(e){}
34+
function urlManager(){var validation=urlVal();if(validation===0){if(!config.accessOnly)urlMod()}else if(validation===1){fetchData(postsOrPages[feedPriority],1)}else if(validation===2){if(!config.accessOnly)history.replaceState(null,null,"/")}}
35+
function fetchData(postsOrPages,index){var script=document.createElement("script");if(config.useApiV3){var jsonUrl="https://www.googleapis.com/blogger/v3/blogs/"+blogId+"/"+postsOrPages+"?key="+config.apiKey+"#maxResults=500#fields=nextPageToken%2Citems(url)#callback=parseData";if(nextPageToken)jsonUrl+="#pageToken="+nextPageToken;nextPageToken=undefined}else{var jsonUrl=window.location.protocol+"//"+window.location.hostname+"/feeds/"+postsOrPages+"/summary?start-index="+index+"#max-results=150#orderby=published#alt=json-in-script#callback=parseData"}
36+
jsonUrl=jsonUrl.replace(/#/g,ampChar);script.type="text/javascript";script.src=jsonUrl;document.getElementsByTagName("head")[0].appendChild(script)}
37+
function parseData(json){var database=[];if(!config.useApiV3){if(!urlTotal){urlTotal=parseInt(json.feed.openSearch$totalResults.$t)}
38+
try{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)})})}catch(e){}}else{try{json.items.forEach(function(element,index){database.push(element.url)})}catch(e){}
4339
nextPageToken=json.nextPageToken}
44-
urlSearch(window.location.pathname,database);if(urlTotal>150){jsonIndex+=150;urlTotal-=150;getJSON(postsOrPages[feedPriority],jsonIndex)}
45-
else if(nextPageToken){getJSON(postsOrPages[feedPriority])}
46-
else if(secondRequest){nextPageToken=undefined;urlTotal=undefined;jsonIndex=1;secondRequest=!1;if(feedPriority===0){feedPriority=1;getJSON("posts",1)}
47-
else if(feedPriority===1){feedPriority=0;getJSON("pages",1)}}}
40+
urlSearch(window.location.pathname,database);if(urlTotal>150){fetchIndex+=150;urlTotal-=150;fetchData(postsOrPages[feedPriority],fetchIndex)}else if(nextPageToken){fetchData(postsOrPages[feedPriority])}else if(secondRequest){nextPageToken=undefined;urlTotal=0;fetchIndex=1;secondRequest=!1;if(feedPriority===0){feedPriority=1;fetchData("posts",1)}else if(feedPriority===1){feedPriority=0;fetchData("pages",1)}}}
4841
function bloggerJS(priority){if(priority)feedPriority=priority;urlManager()}
49-
bloggerJS();
42+
bloggerJS()
5043
</script>
5144
```
5245

@@ -60,7 +53,7 @@ Ya que copiaste completamente el código anterior, dirígete al código HTML de
6053
Una vez hecho esto, sólo guarda los cambios hechos a tu plantilla. Después de ello, **BloggerJS** estará funcionando.
6154

6255
### Nota:
63-
Aunque con sólo copiar y pegar el código, **BloggerJS** estará funcionando, es recomendable (sobre todo para blogs con mucho contenido) configurar el script para funcionar con el API v3 de Blogger, pues funcionará más rápido.
56+
Aunque con sólo copiar y pegar el código, **BloggerJS** estará funcionando, es recomendable (sobre todo para blogs con mucho contenido) configurar el script para funcionar con el API v3 de Blogger, puesto que esta es muy superior en rendimiento y velocidad.
6457

6558
## Configuración
6659
Al principio del script encontrarás variables de configuración, modificar estas propiedades es opcional. La descripción de cada una está en la siguiente tabla:
@@ -70,8 +63,7 @@ Al principio del script encontrarás variables de configuración, modificar esta
7063
| postsDatePrefix | false | Permitir la fecha en las URLs de las entradas/posts. |
7164
| accessOnly | false | Las URLs cortas sólo sirven para acceder al sitio, mas no en su funcionamiento general. |
7265
| useApiV3 | false | Usar API v3 de Blogger. |
73-
| apiKey | vacío | API Key para el uso del API v3 de Blogger. |
74-
| blogId | vacío | ID del blog (para uso del API v3 de Blogger). |
66+
| apiKey | - | API Key para el uso del API v3 de Blogger. |
7567

7668
## Licencia
7769
Licensed under the [MIT License](./LICENSE).<br/>

‎blogger.js

+83-86
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,52 @@
1-
// BloggerJS v0.3.1
2-
// Copyright (c) 2017-2018 Kenny Cruz
1+
// BloggerJS v0.4.0
32
// Licensed under the MIT License
3+
// Copyright (c) 2017-2018 Kenny Cruz
4+
// github.com/jokenox
45

5-
// Configuración -----------
6-
7-
// Permite las fechas en las URL de entradas.
8-
var postsDatePrefix = false;
9-
10-
// URL cortas sólo para entrar al sitio,
11-
// mas no en su funcionamiento general.
12-
var accessOnly = false;
6+
// Configuración
7+
var config = {
8+
// Permite las fechas en las URL de entradas.
9+
postsDatePrefix: false,
1310

14-
// Usar API v3 de Blogger
15-
var useApiV3 = false;
16-
var apiKey = "";
17-
var blogId = "";
11+
// URL cortas sólo para entrar al sitio,
12+
// mas no en su funcionamiento general.
13+
accessOnly: false,
1814

19-
// -------------------------
15+
// Usar API v3 de Blogger.
16+
useApiV3: false,
17+
apiKey: "YOUR-API-KEY-HERE"
18+
}
2019

2120
var postsOrPages = ["pages", "posts"],
22-
urlTotal, jsonIndex = 1,
21+
blogId = "<data:blog.blogId/>",
22+
urlTotal, fetchIndex = 1,
23+
ampChar = "&amp;"[0],
2324
secondRequest = true,
2425
feedPriority = 0,
25-
amp = "&amp;"[0],
2626
nextPageToken;
2727

2828
// urlVal();
29-
// Valida si la URL corresponde a un post/página, si no,
30-
// o si corresponde al index.
31-
function urlVal(){
29+
// Valida si la URL corresponde a un post/página,
30+
// si no, o si corresponde al index.
31+
function urlVal() {
3232
var url = window.location.pathname;
3333
var length = url.length;
3434
var urlEnd = url.substring(length - 5);
35-
if(urlEnd === ".html") return 0;
36-
else if(length > 1) return 1;
35+
if (urlEnd === ".html") return 0;
36+
else if (length > 1) return 1;
3737
else return 2;
3838
}
3939

4040
// urlMod();
41-
// Analiza la URL para identificar si se trata de un post o una página,
42-
// para después modificarla eliminando la fecha o el "/p/", así como el ".html".
43-
function urlMod(){
41+
// Modifica la URL eliminando la fecha o el "/p/", así como el ".html".
42+
function urlMod() {
4443
var url = window.location.pathname;
45-
if(url.substring(1, 2) === "p"){
44+
if (url.substring(1, 2) === "p") {
4645
url = url.substring(url.indexOf("/",1) + 1);
4746
url = url.substr(0, url.indexOf(".html"));
4847
history.replaceState(null, null, "../" + url);
49-
}
50-
else{
51-
if(!postsDatePrefix) url = url.substring(url.indexOf("/",7) + 1);
48+
} else {
49+
if (!config.postsDatePrefix) url = url.substring(url.indexOf("/",7) + 1);
5250
else url = url.substring(1);
5351
url = url.substr(0, url.indexOf(".html"));
5452
history.replaceState(null, null, "../../" + url);
@@ -58,97 +56,94 @@ function urlMod(){
5856
// urlSearch(url, database);
5957
// Busca una url específica en la base de datos, si la encuentra,
6058
// entonces dirigirá a ella.
61-
function urlSearch(url, database){
59+
function urlSearch(url, database) {
6260
var pathname = url + ".html";
63-
database.forEach(function(element){
61+
database.forEach(function(element) {
6462
var search = element.search(pathname);
65-
if(search !== -1) window.location = element;
63+
if (search !== -1) window.location = element;
6664
});
6765
}
6866

6967
// urlManager(database, id);
7068
// Ejecuta una validación de URL, para determinar con el resultado
7169
// la acción a realizar (modificarla o buscarla en el feed del blog).
72-
function urlManager(){
70+
function urlManager() {
7371
var validation = urlVal();
74-
if(validation === 0){
75-
if(!accessOnly) urlMod();
76-
}
77-
else if(validation === 1){
78-
getJSON(postsOrPages[feedPriority], 1);
79-
}
80-
else if(validation === 2){
81-
if(!accessOnly) history.replaceState(null, null, "/");
72+
if (validation === 0) {
73+
if (!config.accessOnly) urlMod();
74+
} else if (validation === 1) {
75+
fetchData(postsOrPages[feedPriority], 1);
76+
} else if (validation === 2) {
77+
if (!config.accessOnly) history.replaceState(null, null, "/");
8278
}
8379
}
8480

85-
// getJSON();
86-
// Realiza una petición de datos donde vienen las URLs
87-
// y los pasa mediante un callback.
88-
function getJSON(postsOrPages, index){
89-
var script = document.createElement('script');
90-
if(useApiV3){
91-
var jsonUrl = "https://www.googleapis.com/blogger/v3/blogs/" + blogId + "/" + postsOrPages + "?key=" + apiKey + "#maxResults=500#fields=nextPageToken%2Citems(url)#callback=bloggerJSON";
92-
if(nextPageToken) jsonUrl += "#pageToken=" + nextPageToken;
81+
// fetchData();
82+
// Realiza una petición de datos del blog.
83+
function fetchData(postsOrPages, index) {
84+
var script = document.createElement("script");
85+
if (config.useApiV3) {
86+
var jsonUrl = "https://www.googleapis.com/blogger/v3/blogs/" + blogId + "/" + postsOrPages +
87+
"?key=" + config.apiKey + "#maxResults=500#fields=nextPageToken%2Citems(url)#callback=parseData";
88+
if (nextPageToken) jsonUrl += "#pageToken=" + nextPageToken;
9389
nextPageToken = undefined;
90+
} else {
91+
var jsonUrl = window.location.protocol + "//" + window.location.hostname + "/feeds/" + postsOrPages +
92+
"/summary?start-index=" + index + "#max-results=150#orderby=published#alt=json-in-script#callback=parseData";
9493
}
95-
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";
96-
jsonUrl = jsonUrl.replace(/#/g, amp);
97-
script.type = 'text/javascript';
94+
jsonUrl = jsonUrl.replace(/#/g, ampChar);
95+
script.type = "text/javascript";
9896
script.src = jsonUrl;
99-
document.getElementsByTagName('head')[0].appendChild(script);
97+
document.getElementsByTagName("head")[0].appendChild(script);
10098
}
10199

102-
// bloggerJSON();
100+
// parseData();
103101
// Obtiene datos en formato JSON, los clasifica
104102
// y los envía para comparar la URL actual.
105-
function bloggerJSON(json){
103+
function parseData(json) {
106104
var database = [];
107105

108-
if(!useApiV3) if(urlTotal === undefined) urlTotal = parseInt(json.feed.openSearch$totalResults.$t);
109-
if(!useApiV3){
110-
try{
111-
json.feed.entry.forEach(function(element, index){
106+
if (!config.useApiV3) {
107+
if (!urlTotal) {
108+
urlTotal = parseInt(json.feed.openSearch$totalResults.$t);
109+
}
110+
111+
try {
112+
json.feed.entry.forEach(function(element, index) {
112113
var entry = json.feed.entry[index];
113-
entry.link.forEach(function(element, index){
114-
if(entry.link[index].rel === "alternate") database.push(entry.link[index].href);
114+
entry.link.forEach(function(element, index) {
115+
if (entry.link[index].rel === "alternate") database.push(entry.link[index].href);
115116
});
116117
});
117-
}
118-
catch(e){}
119-
}
120-
else{
121-
try{
122-
json.items.forEach(function(element, index){
118+
} catch(e) {}
119+
} else {
120+
try {
121+
json.items.forEach(function(element, index) {
123122
database.push(element.url);
124123
});
125-
}
126-
catch(e){}
124+
} catch(e) {}
127125
nextPageToken = json.nextPageToken;
128126
}
129127

130128
urlSearch(window.location.pathname, database);
131129

132-
if(urlTotal > 150){
133-
jsonIndex += 150;
130+
if (urlTotal > 150) {
131+
fetchIndex += 150;
134132
urlTotal -= 150;
135-
getJSON(postsOrPages[feedPriority], jsonIndex);
136-
}
137-
else if(nextPageToken){
138-
getJSON(postsOrPages[feedPriority]);
139-
}
140-
else if(secondRequest){
133+
fetchData(postsOrPages[feedPriority], fetchIndex);
134+
} else if (nextPageToken) {
135+
fetchData(postsOrPages[feedPriority]);
136+
} else if(secondRequest) {
141137
nextPageToken = undefined;
142-
urlTotal = undefined;
143-
jsonIndex = 1;
138+
urlTotal = 0;
139+
fetchIndex = 1;
144140
secondRequest = false;
145-
if(feedPriority === 0){
141+
if (feedPriority === 0) {
146142
feedPriority = 1;
147-
getJSON("posts", 1);
148-
}
149-
else if(feedPriority === 1){
143+
fetchData("posts", 1);
144+
} else if(feedPriority === 1) {
150145
feedPriority = 0;
151-
getJSON("pages", 1);
146+
fetchData("pages", 1);
152147
}
153148
}
154149
}
@@ -158,7 +153,9 @@ function bloggerJSON(json){
158153
// Puede recibir como parámetro el orden de búsqueda para las URL,
159154
// es decir, si iniciará a comparar contra las páginas o las entradas.
160155
// 0 ó vacío = Páginas, 1 = Entradas.
161-
function bloggerJS(priority){
162-
if(priority) feedPriority = priority;
156+
function bloggerJS(priority) {
157+
if (priority) feedPriority = priority;
163158
urlManager();
164159
}
160+
161+
bloggerJS();

‎blogger.min.js

+14-17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎resources/logo.svg ‎img/logo.svg

File renamed without changes.
File renamed without changes.

1 commit comments

Comments
 (1)

adicore commented on Jun 11, 2021

@adicore

Hello, your script above can be run on cloudflare workers?

Please sign in to comment.