AC Content Manager Installation Buttons

Adds Content Manager support to a bunch of AC content websites, such as RaceDepartment, AC Club or Assetto-DB.

当前为 2017-06-16 提交的版本,查看 最新版本

// ==UserScript==
// @name         AC Content Manager Installation Buttons
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Adds Content Manager support to a bunch of AC content websites, such as RaceDepartment, AC Club or Assetto-DB.
// @author       x4fab
// @match        http://assettocorsa.club/*
// @match        http://www.racedepartment.com/*
// @match        http://racedepartment.com/*
// @match        http://www.assetto-db.com/*
// @match        http://assetto-db.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
function _$(selector, fn, p){
  var e = (p || document).querySelector(selector);
  return e ? fn(e) : undefined;
}

function _$each(selector, fn, p){ Array.prototype.forEach.call((p || document).querySelectorAll(selector), fn); }
function _$map(selector, fn, p){ return Array.prototype.map.call((p || document).querySelectorAll(selector), fn); }
function _$some(selector, fn, p){ return Array.prototype.some.call((p || document).querySelectorAll(selector), fn); }
function _$every(selector, fn, p){ return Array.prototype.every.call((p || document).querySelectorAll(selector), fn); }
function _$filter(selector, fn, p){ return Array.prototype.filter.call((p || document).querySelectorAll(selector), fn); }
if (/^http:\/\/assettocorsa\.club\/.*/i.test(location.href)){
if (_$('p.download a', function (x){ return x.textContent; }) == 'DOWNLOAD'){
  _$('.spec > div', function (x) { x.style.width = '60%'; });
  _$('.spec > div:last-child', function (x) { x.style.width = '40%'; });
  _$each('.__cmspecial', function (x){ x.parentNode.removeChild(x); });
  _$('p.download a', function (x) {
    var c = x.cloneNode();
    c.classList.add('__cmspecial');
    c.removeAttribute('target');
    c.setAttribute('href', 'acmanager://install?url=' + encodeURIComponent(x.href));
    c.textContent = 'INSTALL';
    c.setAttribute('style', '\
        color: rgb(100, 166, 112); padding: 0px 0px 6px 40px; line-height: 33px; margin: 2px 0px 2px 12px;\
        background: url(' + JSON.stringify(chrome.extension.getURL('img/icon-acclub.png')) + ') left center / contain no-repeat;\
        display: inline-block;');
    x.parentNode.insertBefore(c, x.nextSibling);
  });
}
} else if (/^http:\/\/www\.racedepartment\.com\/.*/i.test(location.href) || /^http:\/\/racedepartment\.com\/.*/i.test(location.href)){
if (_$some('.crust [itemprop="title"]', x => x.textContent == 'Assetto Corsa')){
  _$each('.__cmspecial', function (x){ x.parentNode.removeChild(x); });
  _$('.resourceInfo .downloadButton', function (x){
    var p = x.parentNode;
    var c = p.cloneNode(true);
    c.classList.add('__cmspecial');
    c.setAttribute('style', 'filter:hue-rotate(-120deg);-webkit-filter:hue-rotate(-120deg)');

    _$('a', function (a){
      a.textContent = 'Install Using Content Manager';
      _$('.resourceInfo .downloadButton .minorText', function (x){ a.appendChild(x.cloneNode(true)) });
      a.setAttribute('href', 'acmanager://install?url=' + encodeURIComponent(location.href));
      a.removeAttribute('target');
    }, c);
    _$('.downloadDisabled', function (b) { b.classList.remove('downloadDisabled') }, c);
    p.parentNode.insertBefore(c, p.nextSibling);
  });
}
} else if (/^http:\/\/www\.assetto-db\.com\/.*/i.test(location.href) || /^http:\/\/assetto-db\.com\/.*/i.test(location.href)){
_$each('.__cmspecial', function (x){ x.parentNode.removeChild(x); });
_$('.carDetails a[href^="/"][href$="/download"]', function (x) {
  var c = x.cloneNode();
  c.classList.add('__cmspecial');
  c.removeAttribute('target');
  c.setAttribute('href', 'acmanager://install?url=' + encodeURIComponent(location.href));
  c.textContent = 'Install using Content Manager';

  // with hue filter, hover/active colors are already there, and also it won’t look weird
  // in case of sudden redesign
  c.setAttribute('style', 'filter:hue-rotate(-120deg);-webkit-filter:hue-rotate(-120deg)');
  c.classList.remove('btn-default');
  c.classList.add('btn-primary');
  x.parentNode.insertBefore(c, x.nextSibling);
});
}
})();