Created
May 26, 2012 18:29
-
-
Save XP1/2794864 to your computer and use it in GitHub Desktop.
Enhance Chrome Web Store: Replaces the "Available on Chrome" button, shown to other browsers, with the application download button.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Enhance Chrome Web Store | |
// @version 1.03 | |
// @description Replaces the "Available on Chrome" button, shown to other browsers, with the application download button. | |
// @namespace https://gist.github.com/2794864/ | |
// @copyright 2012 | |
// @author XP1 | |
// @homepage https://github.com/XP1/ | |
// @license Apache License, Version 2.0; http://www.apache.org/licenses/LICENSE-2.0 | |
// @include http*://chrome.google.*/webstore/detail/* | |
// @include http*://*.chrome.google.*/webstore/detail/* | |
// ==/UserScript== | |
/* | |
* Copyright 2012 XP1 | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
/*jslint browser: true, vars: true, maxerr: 50, indent: 4 */ | |
(function (console, window, ANY_TYPE) { | |
"use strict"; | |
var userScript = { | |
name: "Enhance Chrome Web Store" | |
}; | |
var handleException = function handleException(theFunction) { | |
try { | |
theFunction(); | |
} catch (exception) { | |
console.error("[" + userScript.name + "]: Exception: " + exception + "\n\nStack:\n" + exception.stack + "\n\nStacktrace:\n" + exception.stacktrace); | |
} | |
}; | |
function getApplicationId() { | |
var location = window.location; | |
var names = location.pathname.split("/"); | |
var idRegex = /[A-Za-z0-9]{32}/g; | |
var i = null; | |
for (i = names.length; i >= 0; i -= 1) { | |
var id = names[i]; | |
if (idRegex.test(id)) { | |
return id; | |
} | |
} | |
throw new Error("Cannot find application ID."); | |
} | |
function showDownload() { | |
var document = window.document; | |
var location = window.location; | |
var button = document.evaluate("//div[@role=\"button\"][.=\"Available on Chrome\"]", document, null, ANY_TYPE, null).iterateNext(); | |
document.evaluate("//[text()=\"Available on Chrome\"]", button, null, ANY_TYPE, null).iterateNext().setAttribute("id", "buttonLabel"); | |
var parent = button.parentNode; | |
var downloadUri = "https://clients2.google.com/service/update2/crx?response=redirect&x=id%3D" + getApplicationId() + "%26uc%26lang%3Den"; | |
var downloadButton = button.cloneNode(true); | |
downloadButton.querySelector("#buttonLabel").textContent = "Download"; | |
downloadButton.addEventListener("click", function downloadButtonClick() { | |
location.assign(downloadUri); | |
}, false); | |
parent.insertBefore(downloadButton, button); | |
parent.removeChild(button); | |
} | |
function initialize() { | |
var functions = [showDownload]; | |
var i = null; | |
var length = functions.length; | |
for (i = 0; i < length; i += 1) { | |
handleException(functions[i]); | |
} | |
} | |
window.addEventListener("DOMFrameContentLoaded", initialize, false); | |
}(this.console, this, this.XPathResult.ANY_TYPE)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment