alphathreetwo / homehardware.ca - Quick view product SDS

// ==UserScript==
// @name        homehardware.ca - Quick view product SDS
// @namespace   Violentmonkey Scripts
// @match       https://www.homehardware.ca/en/*/p/*
// @grant       GM.xmlHttpRequest
// @version     1.0
// @license MIT
// @author      aw3
// ==/UserScript==

let product_code = document.location.pathname.split("/p/")[1];


make_request = (request) => {
  return new Promise((resolve, reject) => {
    request.onload = (response) => {
      resolve(response);
    }
    GM.xmlHttpRequest(request);
  });
}


login_sds = () => {
  return make_request({
    method: 'GET',
    url: 'http://msds.homehardware.ca/'
  }).then((response) => {
    console.log(response);
    return true;
  });
}


get_sds_url = (pid) => {
  return make_request({
    method: "POST",
    url: "https://chemmanagement.ehs.com/9/Ebinder/GetSearchResults",
    headers: {"accept": "*/*","content-type": "application/json; charset=UTF-8"},
    data: "{\"PageNumber\":1,\"PageSize\":50,\"CategoryID\":9,\"IsManualSearch\":true,\"SortID\":11,\"SearchTerm\":\"" + pid + "\",\"SearchSuggestID\":0,\"UserInformation\":null,\"Filters\":{\"LanguagesFilterIDs\":[101],\"ManufacturerFilterIDs\":[],\"LocationStatusFilterID\":null,\"ApprovalFilterID\":null,\"ContainersFilterID\":null,\"RegulatoryFormatsFilterIDs\":[],\"SourceFilterIDs\":[],\"RevisionDatesFilterID\":null,\"DotFilterParameters\":[],\"GhsFilterParameters\":[],\"GroupFilterParameters\":[],\"HealthHazardsFilterParameters\":[],\"HmisFilterParameters\":[],\"IngredientsFilterParameters\":[],\"NfpaFilterParameters\":[],\"PhysicalHazardsFilterParameters\":[],\"PpeFilterParameters\":[],\"PrecautionaryStatementsFilterParameters\":[],\"RegulatoryListsFilterParameters\":[],\"RiskPhrasesFilterParameters\":[],\"SafetyPhrasesFilterParameters\":[],\"TargetOrgansFilterParameters\":[],\"TdgFilterParameters\":[],\"WhmisFilterParameters\":[],\"CustomModuleFilter\":\"\",\"FirstAidFilter\":\"\",\"BannedFilter\":false,\"MultipleVersionsFilter\":false,\"DiscontinuedProductsFilter\":false,\"ArchivedDocumentsFilter\":false,\"Location\":{\"EbinderSearchFilterID\":32,\"FilterID\":0,\"FilterOptionID\":1,\"Value\":\"0\"}}}"
  }).then((response) => {
    try {
      let responseObject = JSON.parse(response.responseText);
      let libraryId = responseObject["results"][0]["LibraryID"];
      return "https://chemmanagement.ehs.com/9/pdf/?libraryID=" + libraryId + "&pageID=0&newWindow=false&autoOpened=false&documentQueueID=0";
    }
    catch (e) {
      if (response.responseText.includes("<title>Login")) {
        login_sds().then(() => {
          window.location.reload();
        });
        throw "Not logged in to MSDS service";
      }
    }
    
  }); 
}


get_sds_url(product_code).then((url) => {
  const node = document.querySelector('.productdetails-container');
  const link_element = document.createElement('a');
  link_element.href = url;
  link_element.textContent = "Safety Datasheet";
  node.prepend(link_element);
}, (error) => {
  console.log("Couldn't find product SDS! Reason: " + error);
});