Microsoft Store Direct Download

Adds direct download links to Microsoft Store when browsing apps.

2023-10-08 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

  1. // ==UserScript==
  2. // @name Microsoft Store Direct Download
  3. // @name:it Download diretto dal Microsoft Store
  4. // @namespace StephenP
  5. // @version 2.0.0
  6. // @description Adds direct download links to Microsoft Store when browsing apps.
  7. // @description:it Aggiunge i link per il download diretto dal Microsoft Store quando si naviga tra le applicazioni.
  8. // @author StephenP
  9. // @grant GM.xmlHttpRequest
  10. // @connect rg-adguard.net
  11. // @match https://apps.microsoft.com/detail/*
  12. // ==/UserScript==
  13. var dlBtn;
  14. (function(){
  15. setInterval(checkReload, 1000);
  16. })();
  17. function checkReload(){
  18. let moreBtn=querySelectorAllShadows(".buy-box-container");
  19. if((moreBtn.length>0)&&(querySelectorAllShadows("#dlBtn").length==0)){
  20. /*const origDlBtn=querySelectorAllShadows("sl-button[href^=ms-windows-store]",moreBtn[0]);
  21. console.log(origDlBtn);*/
  22. dlBtn = document.createElement("DIV");
  23. dlBtn.id="dlBtn";
  24. dlBtn.setAttribute("aria-label","Download from AdGuard Store");
  25. dlBtn.style.background="#00a686";
  26. dlBtn.style.font="initial";
  27. dlBtn.style.textAlign="center";
  28. dlBtn.style.borderRadius="var(--sl-input-border-radius-large)";
  29. dlBtn.style.marginTop="0.5em";
  30. dlBtn.innerText="";
  31. dlBtn.appendChild(document.createElement("P"));
  32. dlBtn.firstChild.innerText="\u25bc";
  33. dlBtn.addEventListener("click",function(){openLink(document.location.href,"url")});
  34. moreBtn[0].appendChild(dlBtn);
  35. }
  36. }
  37. function openLink(id,type){
  38. try{
  39. dlBtn.firstChild.innerText="...";
  40. var link="type="+type+"&url="+id+"&ring=RP&lang=it-IT";
  41. GM.xmlHttpRequest({
  42. method: "POST",
  43. url: "https://store.rg-adguard.net/api/GetFiles",
  44. data: link,
  45. headers: {
  46. "Content-Type": "application/x-www-form-urlencoded"
  47. },
  48. onload: function(response) {
  49. dlBtn.firstChild.innerText="\u25bc";
  50. try{
  51. var oldTable=querySelectorAllShadows("#linkTable");
  52. oldTable[0].parentNode.removeChild(oldTable[0]);
  53. var oldMsg=querySelectorAllShadows("#messageFromServer");
  54. oldMsg[0].parentNode.removeChild(oldMsg[0]);
  55. }
  56. catch(err){
  57. console.log(err);
  58. }
  59. try{
  60. output(response,type);
  61. }
  62. catch(err){
  63. console.log(err);
  64. if(type==="ProductId"){
  65. output(err,type);
  66. }
  67. else{
  68. let newId=querySelectorAllShadows("[product-id]")[0].getAttribute("product-id");
  69. openLink(newId,"ProductId");
  70. }
  71. }
  72. }
  73. });
  74. }
  75. catch(err){
  76. console.log(err);
  77. if(type==="ProductId"){
  78. output(err,type);
  79. }
  80. else{
  81. let newId=querySelectorAllShadows("[product-id]")[0].getAttribute("product-id");
  82. openLink(newId,"ProductId");
  83. }
  84. }
  85. }
  86. function output(response,type){
  87. var linkTable = document.createElement("div");
  88. linkTable.innerHTML=response.responseText;
  89. var justTable=linkTable.getElementsByTagName("TABLE")[0];
  90. var messageFromServer=linkTable.getElementsByTagName("P")[0];
  91. messageFromServer.id="messageFromServer";
  92. messageFromServer.style.fontWeight="bold";
  93. if(justTable!==undefined){
  94. justTable.id="linkTable";
  95. const rows=justTable.getElementsByTagName("TR");
  96. for(let row of rows){
  97. if(row.firstChild.firstChild){
  98. if(row.firstChild.firstChild.innerText.endsWith(".appx")){
  99. row.style.fontWeight="bold";
  100. }
  101. else if(row.firstChild.firstChild.innerText.endsWith(".appxbundle")){
  102. row.style.fontWeight="bold";
  103. }
  104. }
  105. }
  106. let pNl=querySelectorAllShadows("sl-card");
  107. if(pNl.length>0){
  108. const pN=pNl[0].parentNode;
  109. pN.insertBefore(justTable, pN.querySelector("sl-card"));
  110. justTable.style.marginTop="2em";
  111. messageFromServer.style.color="green";
  112. pN.insertBefore(messageFromServer, pN.querySelector("sl-card"));
  113. }
  114.  
  115. }
  116. else if((justTable===undefined)&&(type==="url")){
  117. let newId=querySelectorAllShadows("[product-id]")[0].getAttribute("product-id");
  118. openLink(newId,"ProductId");
  119. }
  120. else{
  121. messageFromServer.style.color="red";
  122. dlBtn.parentNode.parentNode.parentNode.appendChild(messageFromServer);
  123. }
  124. }
  125.  
  126.  
  127. //The following function has been taken from https://stackoverflow.com/questions/38701803/how-to-get-element-in-user-agent-shadow-root-with-javascript
  128. /**
  129. * Finds all elements in the entire page matching `selector`, even if they are in shadowRoots.
  130. * Just like `querySelectorAll`, but automatically expand on all child `shadowRoot` elements.
  131. * @see https://stackoverflow.com/a/71692555/2228771
  132. */
  133. function querySelectorAllShadows(selector, el = document.body) {
  134. // recurse on childShadows
  135. const childShadows = Array.from(el.querySelectorAll('*')).
  136. map(el => el.shadowRoot).filter(Boolean);
  137.  
  138. // console.log('[querySelectorAllShadows]', selector, el, `(${childShadows.length} shadowRoots)`);
  139.  
  140. const childResults = childShadows.map(child => querySelectorAllShadows(selector, child));
  141.  
  142. // fuse all results into singular, flat array
  143. const result = Array.from(el.querySelectorAll(selector));
  144. return result.concat(childResults).flat();
  145. }