Almost there, but not quite - if anyone’s willing to assist just for the fun of it
I’m giving the wrapper element the classname ‘wrap’. But I need to adjust the stored index-numbers because I’ve added (temporarily) new elements to the page. Here’s the whole code (3 bookmarklets) I have so far:
[code]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;
var all=d.getElementsByTagName(’’),j=0;
for(var i=0;i<all.length;i++){
if(all.className==‘wrap’){
localStorage’cmt’+(i-j)]=all*.value;
localStorage’wrd’+(i-j)]=all*.thetext;
j+=2;
}
}
return false;
}
)();
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];
found.style.color=‘red’;
}
}
return false;
}
)();
[/code]
The third section isn’t quite working as it’s not applying red colouring to the correct (parent) element.
When I’m searching to decide where to insert the previously saved comments (the 3rd code section) I’ll need to search from the bottom upwards, as I will be inserting new nodes.
I can’t use querySelector() to find the previously stored word, because it returns an element. That is, it will return the *parent ***that contains the word - which I already have anyway. I might have to extract the text from the parent and just do a JS string search. It will then be a little messy as I’ll have to rebuild the whole text-content and re-insert it.
If I get this working I suppose it may have some uses. You can comment web-pages and, when you revisit the page later, you’ll be able to reinstate the comments. I should even be able to reduce it to two (or possibly one) bookmarklet(s).*