Home Download Buy Blog Forum Support

Sunday JS

Re: Sunday JS

Postby agibsonsw on Mon Jun 25, 2012 11:28 pm

Subtracting 1 from x in the third section (shown here) seems good:

Code: Select all
javascript:
(function(){
  var d=document;
  for(var i=localStorage.length-1;i>=0;i--){
    var skey=localStorage.key(i);
    x=parseInt((skey).substr(3));
    if(skey.substr(0,3)=='cmt'){
     alert(x+' '+localStorage['wrd'+x]+'\n'+localStorage['cmt'+x]);
     found=d.getElementsByTagName('*')[x-1];
     found.style.color='red';
   }
  }
  return false;
}
)();

I'll have to test this a little and then, if it's working(?), concentrate on attaching the comment to the correct word. But any input, suggestions, are welcome. Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Sunday JS

Postby SeanWcom on Tue Jun 26, 2012 3:07 am

facelessuser wrote:I think it should be mandatory for people to learn JS before jQuery. Too many answers to JS questions on the web start with "Using jQuery you can just...". I always find it refreshing to see answers that are posed as "you can do it this way...or with jQuery you can..."; it really shows the person's competence with the language when you see they are not exclusively tied to the jQuery framework. Don't get me wrong though, I like jQuery :).


Not to derail the actual conversation in this thread, but had to chime in and defend myself (I know you weren't calling me out, but I still feel compelled...). :lol:

You are right - learning Javascript first would be the best way, so I feel that I should clarify - I didn't just dive into jQuery before knowing javascript. Quite the opposite. I knew plenty of javascript but had always scoffed at it's potential. Shortly after "Web 2.0" became all the rage, I figured I'd better learn this "ajax" stuff. That's when I started with jQuery.

I'm a developer. I write code for a living. I know a lot of different languages and writing code is more than just a job. It's my passion. I love to learn new things, I just happened to learn this side of things in an odd way. Hehe... So, these days when I sit down to work on a project I usually get a POC in place using jQuery - then I work backwards to remove it. I don't like dependencies. It's a pet peeve of mine. I realize that's bass ackwards, but it works for me. :)

That said - carry on. :)
Multiple selections... one of my favorite ST2 features. View some noob-friendly screen casts here.
SeanWcom
 
Posts: 101
Joined: Wed Aug 03, 2011 1:31 am
Location: Georgia

Re: Sunday JS

Postby facelessuser on Tue Jun 26, 2012 3:34 am

@SeanWcom
You are right, I wasn't calling you out; I was just chiming in on @agibsonw's remark. I taught myself JavaScript (I do firmware not web development), and I remember looking around for examples of ways to do certain things, and I saw a lot of "Just use jQuery". I found that frustrating because that wasn't teaching me about the techniques used. I kind of like doing things the hard way at first so that I understand better what I am learning. So that was just me saying "me too!".

In school, we had a crash course in Assembly (I am an EE major), and everyone was given a choice to do the final project in C or Assembly, I was the only one (dumb enough?) who opted to do the whole thing in Assembly. I hope that paints a better picture of my personality and why I say what I do :).
facelessuser
 
Posts: 1571
Joined: Tue Apr 05, 2011 7:38 pm

Re: Sunday JS

Postby SeanWcom on Tue Jun 26, 2012 2:33 pm

Assembly! Yikes! Yeah, you're way out of my league! Hahah... I originally went to school as a Comp Sci major and started learning C/C++ and gave up. That was before I'd ever written a line of code in my life. Looking back, I wish I had stuck with it. These days I'm a fairly competent C# dev, but really wish my roots were in C++... but with two small kids at home, the likelihood of me learning it... nil. Lol!
Multiple selections... one of my favorite ST2 features. View some noob-friendly screen casts here.
SeanWcom
 
Posts: 101
Joined: Wed Aug 03, 2011 1:31 am
Location: Georgia

Re: Sunday JS

Postby facelessuser on Tue Jun 26, 2012 3:06 pm

Well, I did get through the Assembly project, but I won't say I am an expert...far from it; I am just stubborn and very determined. I never actually knew much C/C++ when I started doing firmware; just enough to be dangerous. Companies are like that with engineers, I guess they just figure we will learn it one way or the other. Once you learn a couple languages, you start to see they are all pretty much the same, and learning new languages comes quicker and quicker. But yeah, unless your job requires you to learn it, it is tough to learn it at home with kids. I also have two kids, and they are exhausting; great, but exhausting :).
facelessuser
 
Posts: 1571
Joined: Tue Apr 05, 2011 7:38 pm

Re: Sunday JS

Postby agibsonsw on Wed Jun 27, 2012 10:39 pm

I'm making some progress with this but would appreciate advice or suggestions :) . I appreciate it's not the most important "feature" ever created, but it's keeping me amused ;)

The bookmarklet code is below, in three parts: 'Add a comment', 'Save the comments' and 'Show the comments'.

1) I think I need to sort the localStorage by key before showing/inserting the (previously saved) comments. This is because it currently doesn't insert them in the correct/reverse order, and then the "position" to insert the next comment is out-of-step with the element-indexing. How can I sort the localStorage by key?

2) I'm using innerHTML to re-insert the comments. It works (almost!) but I would prefer to use 'range' objects - this might prove more robust. Is this doable?

3) localStorage seems to be "per site/domain" rather than per page. Is there a setting to make it per page? If not, I might have to store the page/location as part of the key(s). Alternatively, and easier, can I store another dictionary in localStorage? In which case I could use the location as the main dictionary-key (having stripped out the domain, bookmarks, query string, and encodeURIComponent-ed it)?

Code: Select all
javascript:
(function(){
  var d=document;
  var wrap=d.createElement('span');
  wrap.style.position='relative';
  wrap.className='wrap';
  var cmt=d.createElement('textarea');
  cmt.style.backgroundColor='yellow';
  cmt.style.top='1.5em';
  cmt.style.position='absolute';
  cmt.style.zIndex='99';
  cmt.className='cmt';
  cmt.style.height='4em';
  if(window.getSelection){
    var sel=window.getSelection();
    if(sel.rangeCount){
      var rng=sel.getRangeAt(0).cloneRange();
      rng.surroundContents(wrap);
      sel.removeAllRanges();
      sel.addRange(rng);
      wrap.insertBefore(cmt,wrap.firstChild);
      cmt.thetext=rng;
    }
  }
  return false;
}
)();

javascript:
(function(){
  var d=document;
  localStorage.clear();
  var all=d.getElementsByTagName('*'),j=0;
  for(var i=0;i<all.length;i++){
    if(all[i].className=='wrap'){
      localStorage['cmt'+(i-j)]=all[i+1].value;
      localStorage['wrd'+(i-j)]=all[i+1].thetext;
     j+=2;
    }
  }
  return false;
}
)();

javascript:
(function(){
  var d=document;
  for(var i=localStorage.length-1;i>=0;i--){
    var skey=localStorage.key(i);
    var x=parseInt((skey).substr(3));
    if(skey.substr(0,3)=='cmt'){
      var found=d.getElementsByTagName('*')[x-1];
      var fndtext=found.innerHTML;
     var newtext="<span style='position:relative;' class='wrap'>";
     newtext += "<textarea style=\"position:absolute;background-color:yellow;zindex:99;top:1.5em;height:4em;\" class='cmt'>";
     newtext += localStorage['cmt'+x]+"</textarea></span>";
      fndtext=fndtext.replace(localStorage['wrd'+x],newtext+localStorage['wrd'+x]);
      found.innerHTML=fndtext;
    }
  }
  return false;
}
)();
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Previous

Return to General Discussion

Who is online

Users browsing this forum: Google [Bot] and 27 guests