Using Favicon With Bookmarklets - bookmarklet

I have a bookmarklet for my webservice. This is bookmarklet link's code :
(spaces added for readability)
javascript: void((function () {
var e = document.createElement('script');
e.setAttribute('type', 'text/javascript');
e.setAttribute('src', 'http://www.girmiyor.co.cc/bookmarklet.js');
document.body.appendChild(e)
})())
And i want to add favicon for this bookmarklet link. I found this post about it.
They give an example javascript code for adding favicon
javascript:’<!DOCTYPE html><html><head><title>Hello World</title><link rel=”icon” type=”image/png” href=”http://www.tapper-ware.net/devel/js/JS.Bookmarklets/icons/next.png” /></head><body>Hello World</body></html>’;
I want to use this method on my bookmarklet code. I have to combine this codes. But this isn't work.
readability version :
javascript: void((function () {
document.write('<html><head><link rel=\'shortcut icon\' href=\'http://www.girmiyor.co.cc/favicon.ico\'/></head></html>');
var e = document.createElement('script');
e.setAttribute('type', 'text/javascript');
e.setAttribute('src', 'http://www.girmiyor.co.cc/bookmarklet.js');
document.body.appendChild(e)
})())
Original version :
javascript:void((function(){document.write('<html><head><link%20rel=\'shortcut%20icon\'%20href=\'http://www.girmiyor.co.cc/favicon.ico\'/></head></html>');var%20e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('src','http://www.girmiyor.co.cc/bookmarklet.js');document.body.appendChild(e)})())

I think you got it totally wrong from that example. You don't need to document.write the HTML tags directly. I suggest you to read again the article and understand the branching it does.
I'm sorry if I'm not more specific, but I will just repeat the same that's already on the article you linked.

No it does not work in Chrome, and not well in Firefox.
Up to now, the only way to have an icon for a bookmarklet in Chrome is to generate a bookmark (.HTML) file the user is importing where the bookmark has an icon attached.
This is very painful, but all other tentatives fails somehow.
For chrome, generate a file that contains:
<!DOCTYPE NETSCAPE-Bookmark-file-1>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks</H1>
<DL><p>
<DT><H3 ADD_DATE="1346919281" LAST_MODIFIED="1381755311" PERSONAL_TOOLBAR_FOLDER="true">Favorite</H3>
<DL><p><DT>Your bookmarklet name here *MUST BE THE SAME AS THE BOOKMARKLET*
<DT>
</DL><p>
</DL><p>
Then let the user save this file as mybookmark.html and then let it import it in the "Favorite manager" (open favorite manager, then right click on the root folder, select 'Import HTML bookmark...'). This is completely safe as the import goes into a new folder, and it can be deleted immediately after import.
Chrome recognizes it's the same URL (the javascript code) and title, and reuse the icon.

Related

How to get high resolution website logo (favicon) for a given URL

I'm developing a web browser on Android and want to show the URL logo for the most visited sites like in Chrome (4 X 2). But the problem is that most favicons (eg: http://www.bbc.co.uk/favicon.ico) are of size either 16X16 or 32X32 and they don't look good when scaled up.
Is there a way I can download a high resolution icon/bitmap for an URL in a standard way? How about opening the home page and then extracting all the image links and then choose an image with the name logo in it? Would this method work for all the URLs? I want to know if there is a standard way to obtain a high resolution icon for a given URL or favicon is the only standard way to get the website logo?
You can code it yourself or use an existing solution.
Do-it-yourself algorithm
Look for Apple touch icon declarations in the code, such as <link rel="apple-touch-icon" href="/apple-touch-icon.png">. Theses pictures range from 57x57 to 152x152. See Apple specs for full reference.
Even if you find no Apple touch icon declaration, try to load them anyway, based on Apple naming convention. For example, you might find something at /apple-touch-icon.png. Again, see Apple specs for reference.
Look for high definition PNG favicon in the code, such as <link rel="icon" type="image/png" href="/favicon-196x196.png" sizes="196x196">. In this example, you have a 196x196 picture.
Look for Windows 8 / IE10 and Windows 8.1 / IE11 tile pictures, such as <meta name="msapplication-TileImage" content="/mstile-144x144.png">. These pictures range from 70x70 to 310x310, or even more. See these Windows 8 and Windows 8.1 references.
Look for /browserconfig.xml, dedicated to Windows 8.1 / IE11. This is the other place where you can find tile pictures. See Microsoft specs.
Look for the og:image declaration such as <meta property="og:image" content="http://somesite.com/somepic.png"/>. This is how a web site indicates to FB/Pinterest/whatever the preferred picture to represent it. See Open Graph Protocol for reference.
At this point, you found no suitable logo... damned! You can still load all pictures in the page and make a guess to pick the best one.
Note: Steps 1, 2 and 3 are basically what Chrome does to get suitable icons for bookmark and home screen links. Coast by Opera even use the MS tile pictures to get the job done. Read this list to figure out which browser uses which picture (full disclosure: I am the author of this page).
APIs and open source projects
RealFaviconGenerator: You can get any web site favicon or related icon (such as the Touch Icon) with this favicon retrieval API. Full disclosure: I'm the author of this service.
BestIcon: Although less comprehensive, Besticon offers a good alternative, especially if you want to host the code yourself. There is also a hosted version you can use right away.
The Go code at https://github.com/mat/besticon tries to solve this problem.
For example
$ besticon http://github.com
http://github.com: https://github.com/apple-touch-icon-144.png
There is also an accompanying hosted version of the code, see for example http://icons.better-idea.org/icons?url=github.com.
(Disclaimer: I wrote it because I needed to solve the same problem a while ago.)
another option is getting favicons from any domain using a hidden google API
the favicon link pattern will be
https://www.google.com/s2/favicons?domain={domain}&sz={size}
for example
https://www.google.com/s2/favicons?domain=stackoverflow.com&sz=64
Logos are not going to be consistently named and very difficult to identify consistently. Consider putting the favicon on a colour tile of suitable dimensions. People will quickly associate the colour with the website. You could either extract a dominant colour from the website or favicon using something like colorthief, or make each one unique using a golden angle formula to choose a hue.
Here is a new and genuine solution which will always give you the best results-
Webchromeclient gives a callback of onReceivedTouchIconUrl method for all the websites just hold the url from here.
Next step is to convert this url to bitmap which can be done like this-
try {
URL url = new URL(touchiconUrl);
HttpURLConnection connection =
(HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
Next step is to send this bitmap for the shortcut.
Note: Remember to create bitmap on background thread like asynctask.
This HTML document requires a base url and the HTML/"View Page Source" of the web page and should output the values.
<!doctype html>
<input type=text placeholder=URL><br>
Place "View Page Source" of HTML homepage<br>
<textarea id=HTML placeholder="HTML content of webpage">
</textarea><br>
<input type=Submit>
<script>
function url(u,n){
try{
u = u.getAttribute(n);
}
catch(e){
return 'null';
}
if(u.slice(0,2) == "//"){
u = "http:"+u;
}
else if(u.slice(0,1) == "/"){
u = u.slice(0,1);
}
return '<img src="'+u+'">';
}
document.querySelector('input[type=Submit]').onclick = function(){
var output = '';
var HTML = document.getElementById('HTML').value;
var doc = document.implementation.createHTMLDocument("New Document");
doc.documentElement.innerHTML = HTML;
output = output + "apple-touch-icon<br>"+url([].slice.apply(doc.querySelectorAll('link[rel="apple-touch-icon"]')).reverse()[0],'href')
// deprecated output = output + "apple-touch-icon-precomposed<br>"+url([].slice.apply(doc.querySelectorAll('link[rel="apple-touch-icon-precomposed"]')).reverse()[0],'href')
output = output + "<br>image/png<br>" + url(doc.querySelectorAll('link[rel="icon"][type="image/png"]')[0],'href');
// <meta name="msapplication-TileImage" content="/mstile-144x144.png">
// deprecated output = output + "<br>msapplication-Ti:<br>"+ url(doc.querySelectorAll('link[name="msapplication-TileImage"]')[0],'content');
// <meta name="msapplication-config" content="/browserconfig.xml/ ">
//output = output + "<br>msapplication-con: "+ url(doc.querySelectorAll('meta[name="msapplication-config"]')[0],'content');
// <meta property="og:image" content="http://somesite.com/somepic.png"/>
output = output + "<br>og:image<br>" + url(doc.querySelectorAll('meta[property="og:image"]')[0],'content');
// <link rel="image_src" href="https://cdn.sstatic.net/Sites/stackoverflow/Img/apple-touch-icon.png?v=c78bd457575a">
output = output + "<br>image_source<br>" + url(doc.querySelectorAll('link[rel="image_src"]')[0],'href');
var URL = window.location.hash;
document.getElementById('output').innerHTML = output;
};</script>
<div id=output></div>
If you would like to automate the retrieval of the HTML you could use something like the following for PHP.
<? echo file_get_contents($_GET["url"]);
Usually favicon is small (like 16x16 or 32x32). If you need bigger dimensions, extract not favicon, but logo from homepage/header.

Using an iframe in FireFox adds an extra <body> tag?

I have a webpage that uses an iframe to embed another one of our websites. However, FireFox is having issues rendering the contents of the iframe. When I inspected the raw html that was in the DOM, I noticed the following DOM structure inside the iframe:
#document
<!DOCTYPE html>
<html>
<body></body>
<head> … </head>
<body> … </body>
</html>
Notice the body tag above the head tag - that's not in the source DOM! Removing it from within the developer tools fixes all of the rendering issues. For some reason, FireFox is adding a second body tag just before the head tag. Here is my puzzle:
The extra body is not in the source HTML being delivered
The extra tag only shows up in FireFox, Chrome and IE do not have it in there iframes
If I go straight to the url the iframe is loading in FireFox, the extra body tag is not there!
I have no addons - FireFox install is clean
I have the latest FireFox as of this post (v24.0)
Does anyone know what could be causing this? The site being embedded is really simple and does not have any javascript that could be adding this extra tag.
I don't know what causes this to happen in some FF iframes and not others, but if you have access that allows you to change the code of the page that is loaded into the iframe, you could add this script that removes the first empty body tag:
<script type="text/javascript">
var ffFixCount = 0,
clearExtraBody = function(){
var bodies = document.getElementsByTagName("body");
if(bodies.length > 1){
// assumes the empty, extra body tag you want to remove is the first one
bodies[0].parentNode.removeChild(bodies[0]);
window.clearInterval(ffBodyFixer);
}else{
ffFixCount++;
}
if(ffFixCount = 20){
window.clearInterval(ffBodyFixer);
}
};
//check for extra body tag will run every 100ms,
// 20 times, or, for 2 seconds (to give time for bug to happen)
// or will stop if extra body tag is found
var ffBodyFixer = window.setInterval(
function(){
window.clearExtraBody();
}, 100);
</script>

Why codeigniter display double quote(empty) on top page

I use codeigniter framework, run on chrome but display double quote on top page.
My controller call view page.
public function index(){
$this->load->view('dash_main');
}
dash_main.php
<html>
<head>
<title>test</title>
</head>
<body>
Hello World
</body>
</html>
How can i edit?
Those aren't in your source code, it's just what the Chrome inspector shows around text. Nothing to do with Codeigniter, nothing to do with your code or HTML output.
View the actual page source and you won't see any quotes (but you will see a lot of white space judging from your screen shot).
Make sure that your file (view) has been saved to UTF-8 format.
I got the same problem as you, just change file to utf-8 format. that's all ^^

Redirect and keep Referral Url

I have a page which I redirect from it to another page (let's call it "middle page") with parameters in the query string. and I want to redirect from the middle page to some other page in another domain without losing the referral url (the query string of the middle page). my project is MVC C#.
I've tried:
- redirecting in the server side -> didn't work since the query string of the middle page didn't have a chance to change in the client side.
- redirecting in the client side through java script: location.replace/ location.href -> didn't keep the referral url.
- redirecting in the client side with Meta tag refresh -> didn't work.
I did see that if i place a link () in the middle page, clicking it would keep the referral url. so my last resolution was placing a javascript that automatically clicks on the link on document load-> it didn't work as well.
anyone has a solution for this?
I've solved it by using a timeout before I auto-click my link.
however I don't like this solution. does anyone have a better solution?
Try to submit a form using javascript: (Edited version)
So, we have a file "first.html" which calls "middle.html" with parameters:
<html>
<body>
<script>
document.location.href="middle.html?a=1&b=2";
</script>
</body>
</html>
Now "middle.html" makes form-submit to another domain:
<html>
<body>
<script>
var myForm = document.createElement("form");
myForm.method = "GET";
myForm.action = "http://google.com";
document.body.appendChild(myForm);
myForm.submit();
</script>
</body>
</html>
I checked it in FF with Firebug where under GET-request i can see HttpHeader "Referer" which equals to "middle.html?a=1&b=2"

How to make <div>s in HTML5 draggable for Firefox?

I am playing around with the HTML5 features, and I want div's (and similar containers like articles, sections, etc.) to be draggable. Consider the following code:
<!DOCTYPE html>
<html>
<head>
<title>A Simple Draggable Object</title>
</head>
<body>
<h1>Test #1: A Simple Draggable Object</h1>
<div draggable="true">This text should be draggable.</div>
</body>
</html>
I tested in OS X the following browsers:
In Chrome 7.0 and Safari 5.0.2 I can successfully drag the text around, but in Firefox 3.6 and 4.0b6 I can neither drag the text nor mark it (as if it was usual text). Is this a bug or a feature?
How do I achieve that Firefox lets me drag around these tags without using jQuery ?
According to HTML5 Doctor, this won't work in Firefox without some JS help.
The HTML 5 spec says it should be as
simple as adding the following
attributes to the markup of the
elements in question:
draggable="true"
However, this doesn’t work completely
for Safari or Firefox. For Safari you
need to add the following style to the
element:
[draggable=true] {
-khtml-user-drag: element;
}
This will start working in Safari, and
as you drag it will set a default,
empty value with the dataTransfer
object. However, Firefox won’t allow
you to drag the element unless you
manually set some data to go with it.
To solve this, we need a dragstart
event handler, and we’ll give it some
data to be dragged around with:
var dragItems = document.querySelectorAll('[draggable=true]');
for (var i = 0; i < dragItems.length; i++) {
addEvent(dragItems[i], 'dragstart', function (event) {
// store the ID of the element, and collect it on the drop later on
event.dataTransfer.setData('Text', this.id);
});
}

Resources