Craigslist locations filter

By Kuzmeech Last update Jan 15, 2011 — Installed 1,343 times.

Quck hack: Add 'memo' field (Source included)

in
Subscribe to Quck hack: Add 'memo' field (Source included) 4 posts, 3 voices



CBWhiz Scriptwright
FirefoxWindows

This is a quick hack to add a memo field to each listing. Hopefully you can make it prettier than I can :)

First add "makeMemoHandler(ps[i]);" after the line "ps[i].style.display = visible ? 'block' : 'none';".

Next, add this code outside of the main functions:

function makeMemoHandler(ad_p) {
/* New thing that allows memos */

ad_id = ad_p.getElementsByTagName('a')[0].getAttribute('href', "");

ad_id = ad_id.replace("/", "_", "g"); //sanitize / to _
ad_id = ad_id.substr(1); //remove initial _

ad_id = ad_id.replace(".html", ""); //remove trailing .html


main_id = "cl_loc_memo_ad_" + ad_id;

if (document.getElementById(main_id)) {
return;
//Did this already
}

var memo_main = $n("span", ad_p);

memo_main.id = main_id
memo_main.class = "cl_memo"

//console.log(ad_id)
gm_ad_id = "cl_loc_memo_ad_" + ad_id
memo_txt = GM_getValue(gm_ad_id, "[no memo]");

$t(" - ", memo_main);

var memo_node = $n("span", memo_main);
memo_node.setAttribute("cl_gm_ad_id", gm_ad_id);
$t(memo_txt, memo_node)
memo_node.addEventListener("dblclick", memo_make_edit, false);

}

function memo_make_edit(e) {
//console.log(this, e);
if (this.getElementsByTagName("input").length > 0) {
return; //already editing this
}
ih = this.innerHTML;
this.innerHTML = "";
var txtin = $n("input", this);
txtin.value = ih;
txtin.style.width = "30%";
//var txtsave = $n("input", this);
txtsave = document.createElement("input");
txtsave.type = "submit";
txtsave.value = "Save";
this.appendChild(txtsave);
var _ths = this;
txtsave.addEventListener("click", function() { return saveMemo(_ths, txtin); }, false);
}

function saveMemo(spanPlace, inputNode) {
var v = inputNode.value;
gm_ad_id = spanPlace.getAttribute("cl_gm_ad_id", "");
GM_setValue(gm_ad_id, v);
spanPlace.innerHTML = "";
$t(v, spanPlace);
}

 
Kuzmeech Script's Author
FirefoxWindows

Thank you, will review and add soon :)

 
paka Scriptwright
FirefoxWindows

Great idea but it is too memory consuming and has too many event handlers. Also, there is no garbage collection on the GM_setValue items. What happens when the ad is expired from Craigslist? The now inaccessible memos are useless and will continue to build up and waste memory. This should use GM_listValues() to run through the memos, compare to a list of active ads, and then GM_deleteValue() to remove the stale entries.

I'm working on an efficient memo handler for Craigslist and will post a note here when it's available.

 
paka Scriptwright
FirefoxWindows

Update: Incorporating CBWhiz's code above, I have now added a memo management tool for viewing/deleting the memo fields. I've uploaded and renamed the script to "Craigslist Manager" here: http://userscripts.org/scripts/show/97793

I still need to reduce the event handlers, but the script seems to work reasonably fast the way it is. If I get around to it, I'll post an update to Craigslist Manager.

Cross
Presentational HTML allowed.
Use <code> for inline code and <pre> for code blocks. Use &lt; and &gt; for literal < and >.
We help break paragraphs and link your links.
or cancel