Skip to content

Commit

Permalink
v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jokenox committed Oct 24, 2017
0 parents commit 8cf39fe
Show file tree
Hide file tree
Showing 6 changed files with 285 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dev
wildcard.js
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Kenny Cruz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
121 changes: 121 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<p align="center"><a href="#" target="_blank"><img width="100" src="/resources/logo.svg"></a></p>
<h1>BloggerJS</h1>

**BloggerJS** es un script para modificar el formato de las URL en un blog de Blogger. Creando visualmente una mejor navegación.<br/><br/>
El script "limpia" la URL, eliminando de ella la fecha ```"/YYYY/MM"``` o el ```"/p"```, según sea el caso, así como también el ```".html"```. De esta manera resulta más cómodo compartir una URL, pues luce mucho mejor.<br/>
<p><img src="/resources/url_demo.png"></p>

## Implementación

Para implementar **BloggerJS** en tu blog, copia todo el siguiente código:
```javascript
<script>
// BloggerJS v0.1.0
// Copyright (c) 2017 Kenny Cruz
// Licensed under the MIT License

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

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{
url = url.substring(url.indexOf("/",7) + 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) urlMod();
else if(validation === 1) getJSON(postsOrPages[feedPriority], 1);
else if(validation === 2) 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, urlAmp);
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(postsOrPages[feedPriority], 1);
}
else if(feedPriority === 1){
feedPriority = 0;
getJSON(postsOrPages[feedPriority], 1);
}
}
}

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

bloggerJS();
</script>
```

Ya que copiaste completamente el código anterior, dirígete al código HTML de tu plantilla y busca la etiqueta ```<head>```, pega el código justo debajo de ella:

```html
<head>
<!-- Aquí va el código -->
...
```
Una vez hecho esto, sólo guarda los cambios hechos a tu plantilla. Después de ello, **BloggerJS** estará funcionando.

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

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

// urlVal();
// Valida si la URL corresponde a un post/pagina, 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 pagina,
// para despues 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"));
history.replaceState(null, null, "../" + url);
}
else{
url = url.substring(url.indexOf("/",7) + 1);
url = url.substr(0, url.indexOf(".html"));
history.replaceState(null, null, "../../" + url);
}

}

// urlSearch(url, database);
// Busca una url especifica 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) urlMod();
else if(validation === 1) getJSON(postsOrPages[feedPriority], 1);
else if(validation === 2) 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.
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, urlAmp);
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.
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(postsOrPages[feedPriority], 1);
}
else if(feedPriority === 1){
feedPriority = 0;
getJSON(postsOrPages[feedPriority], 1);
}

}

}

// bloggerJS();
// Incializa BloggerJS.
// Puede recibir como parametro el orden de busqueda para las URL,
// es decir, si iniciará a comparar contra las entradas o las páginas.
// 0 ó vacío = Páginas, 1 = Entradas.
function bloggerJS(priority){

if(priority != undefined) feedPriority = priority;
urlManager();

}
1 change: 1 addition & 0 deletions resources/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/url_demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8cf39fe

Please sign in to comment.