Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- startFetch: function () {
- _chromeStorage.get(CONST.STORE_QUERY_URL, function (items) {
- var trIfEmptyQuery = "<tr style='background-attachment: fixed' class='tr-when-empty'><td colspan='4'>Trenutno ni nastavljen noben filter iskanja.... <a href='http://www.avto.net/_MOTO/'>(-nastavi-)</a></td></tr>"
- if (items != undefined && Object.getOwnPropertyNames(items).length >= 1 && items.queryUrl != undefined) {
- var queryUrl = items.queryUrl;
- var keyObject = {};
- keyObject[CONST.STORE_LAST_FETCHED] = {dateTime: new Date().toString()};
- _chromeStorage.set(keyObject);
- var promis = new Promise(function(resolve){
- resolve(AVTONET.fetchAllImportIO(queryUrl));
- });
- promis.then(function(a){
- console.log(a)
- })
- }
- });
- },
- //fetchAllYQL: function(queryUrl){
- // var jsonResults = [];
- // $.getJSON("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22"+encodeURIComponent(queryUrl)+"%22%20and%20xpath%3D%22%2F%2Fdiv%5Bcontains(concat('%20'%2C%20%40class%2C%20'%20')%2C%20'%20ResultsAd%20')%5D%22&format=json&callback=", function(r){
- // console.log("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22"+encodeURIComponent(queryUrl)+"%22%20and%20xpath%3D%22%2F%2Fdiv%5Bcontains(concat('%20'%2C%20%40class%2C%20'%20')%2C%20'%20ResultsAd%20')%5D%22&format=json&callback=")
- // $.each(r.query.results, function(){
- // jsonResults.push(this)
- // });
- //
- // _chromeStorage.get("storedId", function(item){
- // var setPromises = [];
- // var storedId = item;
- // if(storedId == undefined || Object.getOwnPropertyNames(storedId).length < 1){
- // storedId = 0;
- // }else{
- // storedId = item.storedId
- // }
- // console.log(jsonResults);
- // var i;
- // for(i = 0; i < jsonResults[0].length; i++){
- // var jsonObj = jsonResults[0][i];
- // var url = jsonObj.div[0].div;
- // let photo;
- // if(url.length > 1){
- // photo = url[0].a.img.src;
- // url = url[0].a.href;
- // }else{
- // photo = url.a.img.src;
- // url = url.a.href;
- // }
- // let uri = encodeURI("http://www.avto.net/_MOTO"+url.substring(2, url.length));
- // let title = jsonObj.div[1].a.content;
- // let price = jsonObj.div[2].div[0].content.trim();
- //
- // setPromises.push(new Promise(function(resolve){
- // UTILS.adExists(uri, function(exits, obj){
- // if(!exits){
- // var keyObject = {};
- // keyObject["url"+storedId] = {url: uri, title: title, price:price, newPrice:price, pic:photo, removed: false, pinned: false, note:""};
- // _chromeStorage.set(keyObject);
- // console.log("persisting "+uri+"|"+title+"|"+price+" to key url"+storedId);
- // storedId++;
- // }else{
- // if(obj.price != price){
- // var keyObject = {};
- // if(obj.newPrice != undefined){
- // obj.price = obj.newPrice;
- // }
- // obj.newPrice = price;
- //
- // keyObject[obj.key] = obj;
- // _chromeStorage.set(keyObject);
- // console.log("price has changed from "+obj.price+"to "+price);
- //
- // }else{
- // console.log("uri: "+uri+" already exists, not persisting");
- // }
- // }
- // resolve();
- // });
- // }))
- //
- // }
- //
- // Promise.all(setPromises).then(function() {
- // var storedObj = {};
- // storedObj["storedId"] = storedId;
- // _chromeStorage.set(storedObj);
- //
- // _chromeStorage.get("storedId", function(item){
- // console.log("stored id after complete: ");
- // console.log(item);
- // });
- // });
- //
- //
- //
- // });
- // });
- //},
- fetchAllImportIO: function (queryUrl, page) {
- queryUrl = queryUrl + CONST.URL_PARAM_PAGE + page;
- var allDonePromises = [];
- var jsonResults = [];
- var rtrnUris = [];
- var ioimporturl = CONST.FETCH_IOIMPORT_URL_P1 + encodeURIComponent(queryUrl) + CONST.FETCH_IOIMPORT_URL_P2;
- $.getJSON(ioimporturl, function (r) {
- console.log(r);
- $.each(r.tables[0].results, function () {
- jsonResults.push(this)
- });
- allDonePromises.push(new Promise(function(resolve){
- _chromeStorage.get(CONST.STORE_STORED_ID, function (item) {
- var setPromises = [];
- var storedId = item;
- if (storedId == undefined || Object.getOwnPropertyNames(storedId).length < 1) {
- storedId = 0;
- } else {
- storedId = item.storedId
- }
- console.log(jsonResults);
- var i;
- for (i = 0; i < jsonResults.length; i++) {
- var jsonObj = jsonResults[i];
- var url = encodeURI(jsonObj[CONST.IOIMPORT_PARSE_URL]);
- let uri = encodeURI(CONST.AVTONET_MOTO_PREFIX + url.substring(2, url.length));
- rtrnUris.push(uri);
- let title = jsonObj[CONST.IOIMPORT_PARSE_TITLE];
- let price = jsonObj[CONST.IOIMPORT_PARSE_PRICE];
- let photo = jsonObj[CONST.IOIMPORT_PARSE_PHOTO];
- if(price == undefined){
- price = "Pokličite za ceno"
- }
- setPromises.push(new Promise(function (resolve) {
- UTILS.adExists(uri, function (exits, obj) {
- if (!exits) {
- var keyObject = {};
- keyObject["url" + storedId] = {
- url: uri.toLowerCase(),
- title: title,
- price: price,
- pic: photo,
- removed: false,
- pinned: false,
- note: "",
- key: "url" + storedId
- };
- _chromeStorage.set(keyObject);
- console.log("persisting " + uri + "|" + title + "|" + price + " to key url" + storedId);
- chrome.runtime.sendMessage(CONST.MSG_ICON_NEW);
- storedId++;
- } else {
- if (obj.price != price) {
- var keyObject = {};
- if (obj.newPrice != undefined) {
- obj.price = obj.newPrice;
- }
- obj.newPrice = price;
- keyObject[obj.key] = obj;
- _chromeStorage.set(keyObject);
- console.log("price has changed from " + obj.price + "to " + price);
- chrome.runtime.sendMessage(CONST.MSG_ICON_NEW);
- } else {
- console.log("uri: " + uri + " already exists, not persisting");
- }
- }
- resolve();
- });
- }));
- }
- Promise.all(setPromises).then(function () {
- var storedObj = {};
- storedObj[CONST.STORE_STORED_ID] = storedId;
- _chromeStorage.set(storedObj);
- });
- resolve();
- });
- }));
- Promise.all(allDonePromises).then(function(){
- return rtrnUris;
- });
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement