No Caps

Makes stuff with all caps lowercase

目前為 2016-09-02 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         No Caps
// @version      1.0
// @description  Makes stuff with all caps lowercase
// @author       abbott
// @match        *://*/*
// @namespace https://greasyfork.org/users/59102
// ==/UserScript==
"use strict";

window.onload = function() {
  var elements = document.body.getElementsByTagName('*');

  for (var i = 0, element;  element = elements[i]; i++) {
    var text = '';
    element.innerHTML.split(/(<.+?>)/).forEach(function(s) {
      text += s.charAt(0) === '<' ? s : noCaps(s);
    });

    element.innerHTML = text;
  }
};

// enforces proper capitalization, lowercases words with all caps
function noCaps(s) {
  return s.split(' ').map(function(word) {
    var caps = 0;

    for (var i = 0, char; char = word.charAt(i); i++) {
      if (char !== char.toLowerCase()) {
        caps++;
      }
    }

    if (caps === (word.length - nonAlpha(word))) {
      return word.toLowerCase();  
    }
    
    return word;
  }).join(' ');
}

function nonAlpha(s) {
  var count = 0;

  for (var i = 0, char; char = s.charAt(i); i++) {
    count += (char.toUpperCase() === char.toLowerCase()) ? 1 : 0; // non letters would return the same result
  }

  return count;
}