| 1 | // This script is intended to be run from a web console. It must be run from the correct webpage. |
| 2 | // Step 1: Fill out this and search https://process.dmv.ny.gov/FacilityLookup/ |
| 3 | // Step 2: Select your vehicle type, etc until you receive results |
| 4 | // Step 3: Edit the countResults variable below to the actual number |
| 5 | // Step 4: Inject jQuery into the webpage by copying the commented code below into the web console |
| 6 | // var jq = document.createElement('script');jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js";document.getElementsByTagName('head')[0].appendChild(jq); |
| 7 | |
| 8 | // Step 5: Copy and paste the below into your web console and run it. When the searching is finished, you should receive an array of matches logged to console. |
| 9 | |
| 10 | // modify this value to be the total # of results |
| 11 | countResults = 406; |
| 12 | // fill this with shop names to search for, perhaps from Yelp or another search engine |
| 13 | nameSearch = []; |
| 14 | |
| 15 | // leave empty, this will be populated with search matches |
| 16 | matches = []; |
| 17 | // leave empty, used for acting on the completion of all AJAX requests |
| 18 | promises = []; |
| 19 | // leave empty, will fill with NYS results |
| 20 | results = []; |
| 21 | // leave as is, maps the text/html response TDs to an object with the correct key |
| 22 | tdMap = [ |
| 23 | 'number', |
| 24 | 'name', |
| 25 | 'address', |
| 26 | 'borough', |
| 27 | 'zip', |
| 28 | 'county' |
| 29 | ]; |
| 30 | |
| 31 | // make as many AJAX requests as necessary (due to paged results) |
| 32 | for (var i = 1; i < resultCount; i += 20) { |
| 33 | // add each of these fetch calls to our promises array |
| 34 | promises.push(fetch(`https://process.dmv.ny.gov/FacilityLookup/vsiqSearchLocationResults.cfm?selText=Vehicle%20Category:%20%3Cspan%20class=%27boldRed%27%3ERegistered%20Weight%20of%208,500%20lbs.%20or%20less.%20%3C/span%3E%20Fuel%20Type:%20%3Cspan%20class=%27boldRed%27%3EDiesel%3C/span%3E&StartRow=${i}&facsort=none&facnamesort=ASC&facstreetsort=none&faccitysort=none&faczipsort=none&PageNum=0&COUNTYRES=KING&COUNTYRES_DESC=Kings&ZIP=&inspection=%27SAF%27&insp_types=FLOW4&paging=1&sid=0.5118711909866791`) |
| 35 | .then(function (response) { |
| 36 | // fetch response.body is a stream, this will decode into text |
| 37 | response.text().then(function (text) { |
| 38 | // take our node collection and get the results table, then find only the rows that contain results data... |
| 39 | $($(text)[2]).find('tr:nth-child(n+7):not(:last-child)').each(function (i, tr) { |
| 40 | var result = {}; |
| 41 | |
| 42 | // map each table cell into an object key |
| 43 | $(tr).find('td').each(function (i, td) { |
| 44 | // use of tdMap to map each td to the correct key |
| 45 | result[tdMap[i]] = $.trim($(td).text().toLowerCase()); |
| 46 | }); |
| 47 | |
| 48 | results.push(result); |
| 49 | }); |
| 50 | }); |
| 51 | }) |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | // When all of the fetch requests have received responses and are processed... |
| 56 | Promise.all(promises).then(function () { |
| 57 | // perform a name search for each name we're interested in |
| 58 | nameSearch.forEach(function (name) { |
| 59 | var match = results.find(function (res) { |
| 60 | // we're doing a contains search |
| 61 | return res.name.search(name) > -1; |
| 62 | }); |
| 63 | |
| 64 | if (match) { |
| 65 | // add our match |
| 66 | matches.push(match); |
| 67 | // remove it from our results array so it doesn't get returned more than once |
| 68 | results.splice(results.indexOf(match), 1); |
| 69 | } |
| 70 | }); |
| 71 | |
| 72 | console.log(matches); |
| 73 | }); |
curiouser / nys-vehicle-inspection.js
Last active 3 days ago
A web console script for searching over all NYS Vehicle Inspection search results
Revision 45f38b551a19f89c30ea2748aadbe592bb0f26e9