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).*