views:

29

answers:

3

Its a simple javascript bookmarklet which grabs title,url and selected text. To make sure it grabs the title perfectly(just the title) I had to make this bookmarklet long.It works just fine if i edit the bookamrklet manually.Its way too long to drag and drop just like usual bookmarklet. But i tried a lot through different way to make an external javascript way . I couldn't make it. I used google sites as the hosting site for JS file.

     javascript: 
q=(document.location.href);
t=(document.title); 
tt=t.lastIndexOf('|');
if(tt>-1) title=t.substring(0,tt);
else 
{ 
if(t.lastIndexOf('%E2%80%A2')>-1) title=t.substring(0,t.lastIndexOf('%E2%80%A2'));
else if(
t.lastIndexOf('%C2%AB')>-1) title=t.substring(0,t.lastIndexOf('%C2%AB')); 
      else { title=t; } }
if(q.search("inblog")!=-1) 
{ x=title.lastIndexOf('-'); title=title.substring(0,x); }
else if(q.search("m-alo.com")!=-1) 
{ x=title.lastIndexOf('-'); title=title.substring(x+2); }
else if(q.search("blog4")!=-1) 
 { x=title.indexOf('|'); title=title.substring(x+2,title.length); }   
else if(q.search("blogspot")!=-1) 
{ x=title.indexOf(':'); title=title.substring(x+2,title.length); }
else if(q.search("blog")!=-1)          title=document.getElementsByTagName('h2').item(0).innerHTML; 
else if(q.search("arts")!=-1) 
{ x=title.lastIndexOf('%C2%BB'); title=title.substring(x+2); } 
else if(q.search("sports")!=-1||q.search("tech")!=-1)      title=document.getElementsByTagName('h1').item(0).innerHTML;
else if(q.search("blog2")!=-1) { title=document.getElementsByTagName('h1').item(0).innerHTML; x=title.lastIndexOf('">'); title=title.substring(x+4,title.length-4); }
d=document,s=''; 
if(d.getSelection) s=d.getSelection(); if(d.selection) s=d.selection.createRange().text;
if(window.getSelection) s=window.getSelection();
void(open('http://domain.com/submit?url='+encodeURIComponent(q.replace(/[#].*/,''))+'&title='+encodeURIComponent(title)+'&body='+encodeURIComponent(s),'',
'resizable,location,menubar,toolbar,scrollbars,status'));

Can you help me? How can i put this script into an external js file and load it?

+1  A: 

A bookmarklet usually loads a <script> tag which then takes care of doing the rest of the stuff. This reduces the bookmarklet size. Something like:

javascript:(function(){

var head = document.getElementsByTagName("head")[0];
var script = document.createElement("script");
script.src = "http://yoursite.com/external.js";
head.appendChild(script);

})();

Be sure the edit the "http://yoursite.com/external.js" part.

Luca Matteis
javascript: (function(){ var head=document.getElementsByTagName("head")[0]; var script=document.createElement("script"); script.src="http://sites.google.com/site/Myfile/s3.js"; head.appendChild(script); } )();on my file i just put the above code.It didn't work!
roboticman
and thank u for ur answer.
roboticman
You do realize that 'http://sites.google.com/site/Myfile/s3.js' doesn't exist?
Luca Matteis
yes Luca, i just changed the name :)
roboticman
A: 

If I understand your question properly, this is what you're looking for:

http://refresh-sf.com/yui/

This is an online version of YUI Compressor. It will compact your code to one line and (optionally) obfuscate it.

Salman A
A: 
javascript:(function(){

var head = document.getElementsByTagName("head")[0];
var script = document.createElement("script");
script.src = "http://sites.google.com/site/mysite/s3.js";
head.appendChild(script);

})();

I put below code in that file

var q=(document.location.href);
var t=(document.title); 
var tt=t.lastIndexOf('|');
if(tt>-1) var title=t.substring(0,tt);
else 
{ 
if(t.lastIndexOf('%E2%80%A2')>-1) title=t.substring(0,t.lastIndexOf('%E2%80%A2'));
else if(
t.lastIndexOf('%C2%AB')>-1) title=t.substring(0,t.lastIndexOf('%C2%AB')); 
      else { title=t; } }
if(q.search("inblog")!=-1) 
{ x=title.lastIndexOf('-'); title=title.substring(0,x); }
else if(q.search("m-alo.com")!=-1) 
{ x=title.lastIndexOf('-'); title=title.substring(x+2); }
else if(q.search("blog4")!=-1) 
 { x=title.indexOf('|'); title=title.substring(x+2,title.length); }   
else if(q.search("blogspot")!=-1) 
{ x=title.indexOf(':'); title=title.substring(x+2,title.length); }
else if(q.search("blog")!=-1)          title=document.getElementsByTagName('h2').item(0).innerHTML; 
else if(q.search("arts")!=-1) 
{ x=title.lastIndexOf('%C2%BB'); title=title.substring(x+2); } 
else if(q.search("sports")!=-1||q.search("tech")!=-1)      title=document.getElementsByTagName('h1').item(0).innerHTML;
else if(q.search("blog2")!=-1) { title=document.getElementsByTagName('h1').item(0).innerHTML; x=title.lastIndexOf('">'); title=title.substring(x+4,title.length-4); }
var d=document,var s=''; 
if(d.getSelection) s=d.getSelection(); if(d.selection) s=d.selection.createRange().text;
if(window.getSelection) s=window.getSelection();
void(open('http://domain.com/submit?url='+encodeURIComponent(q.replace(/[#].*/,''))+'&amp;title='+encodeURIComponent(title)+'&amp;body='+encodeURIComponent(s),'',
'resizable,location,menubar,toolbar,scrollbars,status'));

It is still not working

roboticman