Skip to content

Commit 89c810f

Browse files
committed
accessOnly and postsDatePrefix options
1 parent 8cf39fe commit 89c810f

File tree

4 files changed

+81
-49
lines changed

4 files changed

+81
-49
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2017 Kenny Cruz
3+
Copyright (c) 2017-2018 Kenny Cruz
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,20 @@ El script "limpia" la URL, eliminando de ella la fecha ```"/YYYY/MM"``` o el ```
1010
Para implementar **BloggerJS** en tu blog, copia todo el siguiente código:
1111
```javascript
1212
<script>
13-
// BloggerJS v0.1.0
14-
// Copyright (c) 2017 Kenny Cruz
13+
// BloggerJS v0.2.1
14+
// Copyright (c) 2017-2018 Kenny Cruz
1515
// Licensed under the MIT License
1616

17-
var postsOrPages = ["pages", "posts"],
18-
urlAmp = "&amp;".substring(0, 1),
17+
// Configuration -----------
18+
var postsDatePrefix = false;
19+
var accessOnly = false;
20+
// -------------------------
21+
22+
var postsOrPages = ["posts", "pages"],
23+
amp = "&amp;".substring(0, 1),
24+
urlTotal, jsonIndex = 1,
1925
secondRequest = true,
20-
feedPriority = 0,
21-
jsonIndex = 1,
22-
urlTotal;
26+
feedPriority = 0;
2327

2428
function urlVal(){
2529
var url = window.location.pathname;
@@ -38,7 +42,8 @@ function urlMod(){
3842
history.replaceState(null, null, "../" + url);
3943
}
4044
else{
41-
url = url.substring(url.indexOf("/",7) + 1);
45+
if(!postsDatePrefix) url = url.substring(url.indexOf("/",7) + 1);
46+
else url = url.substring(1);
4247
url = url.substr(0, url.indexOf(".html"));
4348
history.replaceState(null, null, "../../" + url);
4449
}
@@ -54,23 +59,30 @@ function urlSearch(url, database){
5459

5560
function urlManager(){
5661
var validation = urlVal();
57-
if(validation === 0) urlMod();
58-
else if(validation === 1) getJSON(postsOrPages[feedPriority], 1);
59-
else if(validation === 2) history.replaceState(null, null, "/");
62+
if(validation === 0){
63+
if(!accessOnly) urlMod();
64+
}
65+
else if(validation === 1){
66+
if(!postsDatePrefix) getJSON(postsOrPages[feedPriority], 1);
67+
else getJSON("posts", 1);
68+
}
69+
else if(validation === 2){
70+
if(!accessOnly) history.replaceState(null, null, "/");
71+
}
6072
}
6173

6274
function getJSON(postsOrPages, index){
63-
var script = document.createElement("script");
75+
var script = document.createElement('script');
6476
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";
65-
jsonUrl = jsonUrl.replace(/#/g, urlAmp);
66-
script.type = "text/javascript";
77+
jsonUrl = jsonUrl.replace(/#/g, amp);
78+
script.type = 'text/javascript';
6779
script.src = jsonUrl;
68-
document.getElementsByTagName("head")[0].appendChild(script);
80+
document.getElementsByTagName('head')[0].appendChild(script);
6981
}
7082

7183
function bloggerJSON(json){
7284
var database = [];
73-
if(urlTotal == undefined) urlTotal = parseInt(json.feed.openSearch$totalResults.$t);
85+
if(urlTotal === undefined) urlTotal = parseInt(json.feed.openSearch$totalResults.$t);
7486
json.feed.entry.forEach(function(element, index){
7587
var entry = json.feed.entry[index];
7688
entry.link.forEach(function(element, index){
@@ -89,17 +101,17 @@ function bloggerJSON(json){
89101
secondRequest = false;
90102
if(feedPriority === 0){
91103
feedPriority = 1;
92-
getJSON(postsOrPages[feedPriority], 1);
104+
getJSON("pages", 1);
93105
}
94106
else if(feedPriority === 1){
95107
feedPriority = 0;
96-
getJSON(postsOrPages[feedPriority], 1);
108+
getJSON("posts", 1);
97109
}
98110
}
99111
}
100112

101113
function bloggerJS(priority){
102-
if(priority != undefined) feedPriority = priority;
114+
if(priority) feedPriority = priority;
103115
urlManager();
104116
}
105117

@@ -116,6 +128,8 @@ Ya que copiaste completamente el código anterior, dirígete al código HTML de
116128
```
117129
Una vez hecho esto, sólo guarda los cambios hechos a tu plantilla. Después de ello, **BloggerJS** estará funcionando.
118130

131+
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.
132+
119133
## Licencia
120134
Licensed under the [MIT License](./LICENSE).<br/>
121-
Copyright (c) 2017 [Kenny Cruz](https://github.com/jokenox).
135+
Copyright (c) 2017-2018 [Kenny Cruz](https://github.com/jokenox).

blogger.js

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
1-
// BloggerJS v0.1.0
2-
// Copyright (c) 2017 Kenny Cruz
1+
// BloggerJS v0.2.1
2+
// Copyright (c) 2017-2018 Kenny Cruz
33
// Licensed under the MIT License
44

5-
var postsOrPages = ["pages", "posts"],
6-
urlAmp = "&amp;".substring(0, 1),
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;
13+
14+
// -------------------------
15+
16+
var postsOrPages = ["posts", "pages"],
17+
amp = "&amp;".substring(0, 1),
18+
urlTotal, jsonIndex = 1,
719
secondRequest = true,
8-
feedPriority = 0,
9-
jsonIndex = 1,
10-
urlTotal;
20+
feedPriority = 0;
1121

1222
// urlVal();
13-
// Valida si la URL corresponde a un post/pagina, si no,
23+
// Valida si la URL corresponde a un post/página, si no,
1424
// o si corresponde al index.
1525
function urlVal(){
1626

@@ -25,8 +35,8 @@ function urlVal(){
2535
}
2636

2737
// urlMod();
28-
// Analiza la URL para identificar si se trata de un post o una pagina,
29-
// para despues modificarla eliminando la fecha o el "/p/", así como el ".html".
38+
// Analiza la URL para identificar si se trata de un post o una página,
39+
// para después modificarla eliminando la fecha o el "/p/", así como el ".html".
3040
function urlMod(){
3141

3242
var url = window.location.pathname;
@@ -37,15 +47,16 @@ function urlMod(){
3747
history.replaceState(null, null, "../" + url);
3848
}
3949
else{
40-
url = url.substring(url.indexOf("/",7) + 1);
50+
if(!postsDatePrefix) url = url.substring(url.indexOf("/",7) + 1);
51+
else url = url.substring(1);
4152
url = url.substr(0, url.indexOf(".html"));
4253
history.replaceState(null, null, "../../" + url);
4354
}
4455

4556
}
4657

4758
// urlSearch(url, database);
48-
// Busca una url especifica en la base de datos, si la encuentra,
59+
// Busca una url específica en la base de datos, si la encuentra,
4960
// entonces dirigirá a ella.
5061
function urlSearch(url, database){
5162

@@ -65,9 +76,16 @@ function urlManager(){
6576

6677
var validation = urlVal();
6778

68-
if(validation === 0) urlMod();
69-
else if(validation === 1) getJSON(postsOrPages[feedPriority], 1);
70-
else if(validation === 2) history.replaceState(null, null, "/");
79+
if(validation === 0){
80+
if(!accessOnly) urlMod();
81+
}
82+
else if(validation === 1){
83+
if(!postsDatePrefix) getJSON(postsOrPages[feedPriority], 1);
84+
else getJSON("posts", 1);
85+
}
86+
else if(validation === 2){
87+
if(!accessOnly) history.replaceState(null, null, "/");
88+
}
7189

7290
}
7391

@@ -76,12 +94,13 @@ function urlManager(){
7694
// y los envía mediante un callback.
7795
function getJSON(postsOrPages, index){
7896

79-
var script = document.createElement("script");
97+
var script = document.createElement('script');
8098
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";
81-
jsonUrl = jsonUrl.replace(/#/g, urlAmp);
82-
script.type = "text/javascript";
99+
jsonUrl = jsonUrl.replace(/#/g, amp);
100+
101+
script.type = 'text/javascript';
83102
script.src = jsonUrl;
84-
document.getElementsByTagName("head")[0].appendChild(script);
103+
document.getElementsByTagName('head')[0].appendChild(script);
85104

86105
}
87106

@@ -92,7 +111,7 @@ function bloggerJSON(json){
92111

93112
var database = [];
94113

95-
if(urlTotal == undefined) urlTotal = parseInt(json.feed.openSearch$totalResults.$t);
114+
if(urlTotal === undefined) urlTotal = parseInt(json.feed.openSearch$totalResults.$t);
96115

97116
json.feed.entry.forEach(function(element, index){
98117
var entry = json.feed.entry[index];
@@ -109,32 +128,30 @@ function bloggerJSON(json){
109128
getJSON(postsOrPages[feedPriority], jsonIndex);
110129
}
111130
else if(secondRequest){
112-
113131
urlTotal = undefined;
114132
jsonIndex = 1;
115133
secondRequest = false;
116-
117134
if(feedPriority === 0){
118135
feedPriority = 1;
119-
getJSON(postsOrPages[feedPriority], 1);
136+
getJSON("pages", 1);
120137
}
121138
else if(feedPriority === 1){
122139
feedPriority = 0;
123-
getJSON(postsOrPages[feedPriority], 1);
140+
getJSON("posts", 1);
124141
}
125-
126142
}
127143

128144
}
129145

130146
// bloggerJS();
131147
// Incializa BloggerJS.
132-
// Puede recibir como parametro el orden de busqueda para las URL,
148+
// Puede recibir como parámetro el orden de búsqueda para las URL,
133149
// es decir, si iniciará a comparar contra las entradas o las páginas.
134-
// 0 ó vacío = Páginas, 1 = Entradas.
150+
// 0 ó vacío = Entradas, 1 = Páginas.
135151
function bloggerJS(priority){
136152

137-
if(priority != undefined) feedPriority = priority;
153+
if(priority) feedPriority = priority;
154+
138155
urlManager();
139156

140157
}

blogger.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)