I am writing an extension. Its logic is as follows: the DOM parsim, we are looking for a price there, we compare it with our given one.

manifest.json

{ "name": "SteamStickergunBuy", "description": "sticker buy", "version": "1.0", "manifest_version": 2, "icons":{ "128":"icons/red.png" }, "browser_action": { "default_title": "ThisIsGooood", "default_icon": "icons/red.png", "default_popup": "popup.html" }, "background": { "scripts": ["background.js"] }, "permissions": [ "http://steamcommunity.com/market/listings/730/*", "tabs", "background" , "storage", "alarms", "notifications" ] } 

background.html

 <div style='display: none' id='example'>Example</div> <script src="background.js"></script> 

background.js

 var items=[ {item_id:"http://steamcommunity.com/market/listings/730/AWP%20%7C%20Asiimov%20%28Battle-Scarred%29",price: '35500'}, {item_id:"http://steamcommunity.com/market/listings/730/AWP%20%7C%20Pit%20Viper%20%28Battle-Scarred%29",price: '35500'}, {item_id:"http://steamcommunity.com/market/listings/730/AWP%20%7C%20Corticera%20%28Field-Tested%29",price: '35500'}, {item_id:"http://steamcommunity.com/market/listings/730/AWP%20%7C%20Corticera%20%28Minimal%20Wear%29",price: '35500'}, {item_id:"http://steamcommunity.com/market/listings/730/AWP%20%7C%20Electric%20Hive%20%28Field-Tested%29",price: '35500'} ]; var i = 0; var timerId = setInterval(function() { var url=items[i].item_id; var xhr = new XMLHttpRequest(); xhr.open("GET", url, true); xhr.onreadystatechange = function() { if (xhr.readyState == 4){ var data = xhr.responseText; document.body.innerHTML=data; var name=document.getElementsByClassName("market_listing_item_name")[0].innerHTML; if(document.getElementsByClassName("market_listing_table_message").length==0){ var market_price = document.getElementsByClassName("market_listing_price market_listing_price_with_fee")[0].innerHTML; var marketPrice = market_price.replace(/[A-Za-zА-Яа-я.]+/, market_price); marketPrice = parseFloat(marketPrice) * 100;//приводим к копейкам if(marketPrice <= items[i].price){ var dannie=document.getElementById("dannie"); dannie.innerHTML=name+":"+marketPrice; } console.log(name+":"+marketPrice); }else{console.log("Не обнаружены лоты для данного предмета с учетом выбранных фильтров.")} } } i++; xhr.send(); }, 10000); 

And popup.html

 <script src="popup.js"></script> <script src="background.js"></script> <div style="height:100px;width:200px;font-size:10px;padding:3px" id="dannie">Ѕлок с данными</div > 

So the code works. Everything is fine, but the entire page is loaded into popup.html (because I insert xhr.responcetext into the body).

If I insert into the prepared invisible div example (as I want the popup to display only the result), then an error occurs (supposedly equal to zero, it cannot find this div)

How to be? how best to implement it ?. To see if the price is lower than what I asked. In popup.html came out the name of the price and the link is not the subject.

    1 answer 1

    I think you are going the wrong way. The work of your extension depends on the structure of the page that you are parsing, if something changes, you will have to look for what and where it broke.

    Almost the first link in Google is Price Tracking + Web API .

    You can receive data immediately in JSON format by performing GET requests to the following address:

    http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=AWP%20%7C%20Asiimov%20%28Battle-Scarred%29

    Where:
    currency - currency. Apparently you will need to play with the values.
    appid - ID of the game. In your case it is 730 (CS: GO)
    market_hash_name is the name of the item. Ie rifle AWP in your case.

    Although the forum wrote a request that has another parameter country with a value of DE . But his changes did not seem to change the result of the query.

    The response will be as follows:

     {"success":true,"lowest_price":"21,82\u20ac","volume":"508","median_price":"22,--\u20ac"} 

    Where:
    success - a value that indicates the status of the request. Ie, well or not.
    lowest_price - the lower price for which the item is sold.
    volume - the total number of objects (one type, the same AWP rifle)
    median_price - the average price at which an item was sold.

    As a result, you do not need to mess around with DOM, regulars, etc.

    PS I did not come across this, so there may not be accuracy, but I think the essence will be the same. As an option, here someone offers market api , not the official one, of course.

    For more information on query parameters, you'd better turn to Google, Steam forums, and so on. Steam even had a Wiki for developers. Look for it.

    • via json fails to send a request with filters. For example ( steamcommunity.com/market/priceoverview/… ) the request will not pass. If something changes, I'll have to correct it, but I am writing only for myself, so this is a 5 minute deal. I would just link the background and popup. Those. in the background parsim and check, and if we find the right one, then it is in the popup. Everything would be fine if I did not insert the resulting DOM into document.body (it is not inserted into other places). - ThreegunD
    • It would be possible to simply connect background.js to popup.html. Everything would be fine if I didn’t insert the result from the request into document.body (it isn’t inserted into other places). And it turns out in the popup in the body the whole page is visible. Where better to insert received from the request? that it was not visible, but you could get "doceument.getELement" - ThreegunD