Difficulty executing bookmarklet javascript -
i'm trying create bookmarklet app parses information amazon, etsy, , jcrew , transmits wishlist application built in rails. i've succeeded in creating bookmark button loads javascript file dom, not seem executing correctly , passing data app. or maybe javascript faulty. can me figure out i'm doing wrong? i've included js file below:
function() { function get_amazon_product_info() { var title_span = document.getelementbyid("btasintitle"); var title = title_span.innertext; var image_tag = document.getelementbyid("main-image"); var image = image_tag.getattribute("src"); var price_span = document.getelementbyid("actualpricevalue"); var price = price_span.innertext; var product_info = { title: title, image: image, price: price } return product_info } function get_etsy_product_info() { var title_span = document.getelementbyid("item-title"); var title = title_span.innertext; var image_div = document.getelementbyid("fullimage_link1"); var image_tag = image_div.getelementsbytagname("img"); var image = image_tag[0].getattribute("src"); var price_div = document.getelementsbyclassname("item-price"); var price_span = price_div[0].getelementsbyclassname("currency-value") var price = price_span[0].innertext; var product_info = { title: title, image: image, price: price } return product_info } function get_jcrew_product_info() { var title_span = document.getelementbyid("pdp-title"); var title = title_span.innertext; var image_div = document.getelementsbyclassname("prod_main_img"); var image_tag = image_div[0].getelementsbytagname("img"); var image = image_tag[0].getattribute("src"); //lame implementation -- need able determine radio button checked. finish later! var price_div = document.getelementsbyclassname("pdp-shapes"); var price_span = price_div[0].getelementsbyclassname("price") var price = price_span[0].innertext; var product_info = { title: title, image: image, price: price } return product_info } function determine_params() { domain = document.domain; if (domain == 'www.amazon.com') { get_amazon_product_info(); } else if (domain == 'www.etsy.com') { get_etsy_product_info(); } else if (domain == 'www.jcrew.com') { get_jcrew_product_info(); } } function send_data(product_info) { var link_url = document.url; var http = new xmlhttprequest(); var url = "http://max-miller.local:3000/add_product"; var params = "title=" + product_info[title] + "&image=" + product_info[image] + "&price=" + product_info[price] + "&link_url=" + link_url; http.open("post", url , true); //send proper header information along request http.setrequestheader("content-type", "application/x-www-form-urlencoded"); http.setrequestheader("content-length", params.length); http.setrequestheader("connection", "close"); http.onreadystatechange = function() { if(http.readystate == 4 && http.status == 200) { alert(http.responsetext); } } http.send(params); } determine_params(); send_data(product_info); }
as noted in comments, there 2 problems see code as-is.
first, closure never executed - it's syntactically incorrect as-is. in browser's console, it's difference between example:
function() { console.log('something'); } and one:
(function() { console.log('something'); })(); second, xmlhttprequest work within context of page's domain. (the script doesn't receive special privileges domain comes.) so, given nature of script, looks max-miller.local:3000 not intended domain-of-execution, , request fail.
look jsonp-like solutions.
Comments
Post a Comment