Skip to content

Instantly share code, notes, and snippets.

@vonagam
Last active October 18, 2021 20:31
Show Gist options
  • Save vonagam/51664e8a43b9f44a7e14be07a7c4d697 to your computer and use it in GitHub Desktop.
Save vonagam/51664e8a43b9f44a7e14be07a7c4d697 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Get CRX
// @description Download CRX of extension from Chrome Web Store
// @match https://chrome.google.com/webstore/*
// @grant none
// @version 2.0
// ==/UserScript==
var getExtensionIdFromLocation = function ( location ) {
var pathnameParts = location.pathname.split( '/' );
if ( pathnameParts.length !== 5 ) return;
var extensionId = pathnameParts[ 4 ];
return extensionId;
};
var CHROME_VERSION = /Chrome\/(\d+\.\d+)/.exec( navigator.userAgent )[ 1 ];
var DOWNLOAD_URL_TEMPLATE = 'https://clients2.google.com/service/update2/crx?response=redirect&prodversion=[CHROME_VERSION]&x=id%3D[EXTENSION_ID]%26installsource%3Dondemand%26uc';
var BUTTON_HTML_TEMPLATE = '<a role="button" class="dd-Va g-c g-c-wb" aria-label="Download CRX" tabindex="0" style="user-select: none;" href="[URL]"><div class="g-c-Hf"><div class="g-c-x">Download CRX</div></div></a>';
var getButtonHtmlForExtension = function ( options ) {
var extensionId = options.extensionId;
var className = options.className;
var url = DOWNLOAD_URL_TEMPLATE;
url = url.replace( '[CHROME_VERSION]', CHROME_VERSION );
url = url.replace( '[EXTENSION_ID]', extensionId );
var buttonHtml = BUTTON_HTML_TEMPLATE;
buttonHtml = buttonHtml.replace( '[URL]', url );
buttonHtml = buttonHtml.replace( '[CLASS_NAME]', className );
return buttonHtml;
};
var MARK_CONTAINER_ATTRIBUTE = 'data-crx';
var markContainer = function ( container ) {
if ( container.getAttribute( MARK_CONTAINER_ATTRIBUTE ) ) return false;
container.setAttribute( MARK_CONTAINER_ATTRIBUTE, true );
return true;
};
var INDIVIDUAL_LINK_CONTAINER_SELECTOR = '.h-e-f-Ra-c.e-f-oh-Md-zb-k';
var INDIVIDUAL_LINK_CLASS_NAME = '';
var handleIndividualLink = function () {
var extensionId = getExtensionIdFromLocation( window.location );
if ( ! extensionId ) return;
var container = document.querySelector( INDIVIDUAL_LINK_CONTAINER_SELECTOR );
if ( ! container ) return;
if ( ! markContainer( container ) ) return;
container.innerHTML = getButtonHtmlForExtension( {
extensionId: extensionId,
className: INDIVIDUAL_LINK_CLASS_NAME,
} );
};
var LIST_LIST_CONTAINER_SELECTOR = '.h-d-Ra-c.d-Ra-c';
var LIST_LINK_CLASS_NAME = '';
var handleListLinks = function () {
var containers = document.querySelectorAll( LIST_LIST_CONTAINER_SELECTOR );
containers.forEach( function ( container ) {
if ( ! markContainer( container ) ) return;
var link = container.parentNode.parentNode.parentNode.parentNode;
if ( link.tagName !== 'A' ) return;
var extensionId = getExtensionIdFromLocation( link );
if ( ! extensionId ) return;
container.innerHTML = getButtonHtmlForExtension( {
extensionId: extensionId,
className: LIST_LINK_CLASS_NAME,
} );
} );
};
setInterval( function () {
handleIndividualLink();
handleListLinks();
}, 500 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment