Skip to content

Instantly share code, notes, and snippets.

@tscholze
Created January 12, 2014 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tscholze/8382705 to your computer and use it in GitHub Desktop.
Save tscholze/8382705 to your computer and use it in GitHub Desktop.
MG-Chrome-Extension
{
"name": "MobileGeeks Top 5",
"version": "0.1",
"manifest_version": 2,
"description": "Presents you the latest five article of mobilegeeks.de",
"homepage_url": "http://www.mobilegeeks.de",
"browser_action":
{
"default_icon": "icon48.png",
"default_popup": "popup.html"
},
"icons":
{
"16": "icon16.png",
"48": "icon48.png",
"128": "icon128.png"
},
"permissions":
[
"http://www.mobilegeeks.de/feed/"
],
"content_security_policy": "script-src 'self' https://ajax.googleapis.com; object-src 'self'"
}
body
{
min-width: 500px;
min-height: 150px;
font-family: Helvetica, sans-serif;
font-size: 0.8em;
margin: 5px;
padding-top: 15px;
}
header
{
font-size: 1.25em;
font-weight: bold;
color: #E6782D;
}
footer
{
font-size: 0.5em;
color: #c0c0c0;
text-align: center;
padding-top: 10px;
}
ul
{
padding-left: 5px;
}
ul li
{
list-style: none;
padding-top: 3px;
}
a
{
text-decoration: none;
color: black;
}
footer span
{
padding-right: 5px;
}
footer span:last-child
{
padding-right: 0px;
}
<html>
<head>
<title>MobileGeeks Latest News</title>
<!-- Javascript -->
<script src="jquery.min.js"></script>
<script src="popup.js"></script>
<!-- Styles -->
<link href="popup.css" rel="stylesheet" />
</head>
<body>
<!-- Header -->
<header>
MobileGeeks Latest News
</header>
<!-- List -->
<ul id="entries"></ul>
<!-- Footer -->
<footer>
<span id="mg-link">
Visit <a href="http://www.mobilegeeks.de" target="_blank">MobileGeeks</a>
</span>
<span id="gh-link">
Fork me at <a href="#">Github.com</a>
</span>
<span id="version"></ul>
</footer>
</body>
</html>
/*
The MIT License (MIT)
Copyright (c) 2014 Tobias Scholze
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.
*/
/* Constant values */
var googleService = 'https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&callback=?&q=';
var feedUrl = encodeURIComponent('http://www.mobilegeeks.de/feed/');
var limit = 5;
var version = '0.1';
/* Generated values */
var url = googleService + feedUrl;
/*
* On document loaded, parse Feed.
* - Set version number.
* - Parse feed.
*/
$(document).ready(function()
{
// E.g. Output: (v 0.1)
$('#version').html('(v' + version + ')');
// Parse the feed.
parseFeed();
});
/*
* Parses the feed and returns a callback to
* format output as html dom elements.
*/
function parseFeed()
{
$.ajax(
{
url: url,
dataType: 'json',
success: function(data)
{
/* Object structure:
* 1. data = http response
* 2. responseData = feed + javascript methods
* 3. feed = parsed feed (blog meta infos, entries, etc)
* 4. entries = blog entries
*/
formatJsonToHtmlDomElements(data.responseData.feed.entries);
}
}
);
}
/*
* Formats an entry object into a html dom element.
*/
function formatJsonToHtmlDomElements(entries)
{
// Contains all generated elements.
var list = '';
// Apply limit.
entries = entries.slice(0,5);
// Iterate the array.
entries.forEach(function(entry)
{
// Open tag.
var element = '<li>';
// Title with link.
var title = '<a href="'+entry.link+'" target="_blank">'+ trim(entry.title, 75, " ...")+'</a>';
element = element.concat(title);
// Closing tag.
element = element.concat('</li>');
// Append to list.
list = list.concat(element);
});
// Append to existing html dom.
$('#entries').append(list);
}
/**
* If text is longer as the given length, shorten the text
* and append the given postfix.
* Usecase: 'Hello world' -> 'Hello ...'
*/
function trim(text, length, postfix)
{
if (text.length <= length)
{
return text;
}
else
{
/*
* 1. trim() -> remove bad symbols
* 2. substring() -> shortens the string
* 3. split() -> converts a string in
* an array (words)
* 4. slice() -> removes the last (incomplete)
* word
* 5. join() -> converts the array back to
* a string
* + postfix -> appends the given postfix
*/
return $.trim(text)
.substring(0,(length - postfix.length))
.split(" ")
.slice(0, -1)
.join(" ") + postfix;
}
}
/*
* Just a simple console logger.
*/
function log(msg)
{
console.log(msg);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment