Ionut Flavius Pogacian
Ionut Flavius Pogacian

Reputation: 4801

Detect the referral/s of Url/s using JavaScript or PHP from inside a Bookmarklet

Let's think out of the box!

Without any programming skills, how can you say/detect if you are on a web page that lists products, and not on the page that prints specific details of a product?

The Bookmarklet is inserted using JavaScript in right after the body tag of a website ( eBay, Bloomingdales, Macy's, toys'r'us ... )

Now, my story is: (programming skills needed now)

I have a bookmarklet and my main problem is how to detect if I am on a page that lists products or if i am on the page that prints the product detail.

The best way that I could think, to detect if I am on the detail page of a product is to detect the referral(s) of the current URL. (maybe all the referrals, the entire click history)

Possible problem: a user adds the URL as favorite and does not use my bookmarklet, and closes the browser; then the user uses the browser again, clicks the favorite link and uses my bookmaklet and I think that I can't detect the referral in this case; it's OK, not all the cases are covered or possible;

Can I detect the referral of this link using the cache in this case? (many browsers cache systems involved here, I know)

Upvotes: 2

Views: 572

Answers (4)

papercowboy
papercowboy

Reputation: 3459

how can you say/detect if you are on a web page that lists products, and not on the page that prints specific details of a product

I'd setup Brain.js (a neural net implemented in javascript) and train it up on a (necessarily broad and varied) sample set of DOMs and then pick a threshold product:details ratio to 'detect' (as near as possible) what type of page I'm on.

This will require some trial and error, but is the best approach I can think of (neural nets can get to "good enough" results pretty quickly - try it, you'll be surprised at the results).

Upvotes: 3

Parris
Parris

Reputation: 18408

I would say your goal should first be for it to work for some websites. Then many websites and then eventually all websites.

A) Try hand coding the main sites like Amazon, eBay etc... Have a target in mind.

B) Something more creative might be to keep a list of all currency symbols then detect if a page has maybe 10 scattered around. For instance the $ symbol is found all over amazon. But only when there is say 20 per page can you really say that it is a product listing (this is a bad example, amazon's pages are fairly crazy). Perhaps the currency symbols won't work; however, I think you can you can generalize something similar. Perhaps tons of currency symbols plus detection of a "grid" type system with things lined up in a row. You'll get lots of garbage so you'll need good filtering. Data analysis is needed after you have something working algorithmically like this.

C) I think after B) you'll realize that your system might be better with parts of A). In other words you are going to want to customize the hell out of certain popular websites (or more niche ones for that matter). This should help fill the gap for sites that don't follow any known models.

Now as far as tracking where the user came from why not use a tracking cookie type concept. You could of course use indexedDB or localstorage or whatever. In other words always keep a reference to the last page by saving it on the current page. You could also do things like have a stack and push urls onto it on every page. If you want to save it for some reason just send that data back to your server.

Detecting favorite clicks could involve detecting all AJAX traffic and analyzing it (although this might be hard...). You should first do a survey to see what those calls typically look like. I'd imaging something like amazon.com/favorite/product_id would be fairly common. Also... you could try to detect the selector for the "favorite" button on the page then add an onclick handler to detect when it is clicked.

I tried to solve each problem you mentioned. I don't think I understand exactly what you are trying to do.

Upvotes: 2

DG.
DG.

Reputation: 3517

No. You can't check history with a bookmarklet, or with any normal client side JavaScript. You are correct, the referrer will be empty if loaded from a bookmark.

The bookmarklet can however store the referrer the first time it is used in a cookie or in localStorage and then the next time it is used, if referrer is empty, check the cookie or localStorage.

That said, your entire approach to this problem seems really odd to me, but I don't have enough details to know if it is genius our insanity.

If I was trying to determine if the current page was a list or a details page, I'd either inspect the url for common patterns or inspect the content of the page for common patterns.

Example of common url patterns: Many 'list pages' are search results, so query string will have words like "search=", "q=", "keywords=", etc.

Example of page content patterns: A product page will have only 1 "buy" button or "add to cart", whatever. A list page will have either no such button or have many.

Upvotes: 3

Mathlight
Mathlight

Reputation: 6653

Why don't u use the URL? then you can do something like this http://www.le.url.com?pageid=10&type=DS and then the code will be something like this:

<?php
if(isset($_GET['type']) && $_GET['type'] == 'DS'){
    // Do stuff related to Details Show
} else{
    // Show all the products
}
?>

And you can make the url something like this with an .htacces file: http://www.le.url.com/10/DS

Upvotes: 2

Related Questions