Toolbox

-

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greatest.deepsurf.us/scripts/402133/798662/Toolbox.js

  1. // ==UserScript==
  2. // @name Toolbox
  3. // @version 0.41
  4. // @description -
  5. // @author LianSheng
  6. // ==/UserScript==
  7.  
  8.  
  9. // 【通用】添加 Script
  10. // 預設添加於 body 尾端.
  11. // 若指定的 target 有複數匹配,則只作用於第一個配對到的.
  12. function addScript(appendScript, target="body", id="") {
  13. let s = document.createElement('script');
  14. s.type = "text/javascript";
  15. s.innerHTML = appendScript;
  16. s.id = id;
  17. try{
  18. document.querySelectorAll(target)[0].appendChild(s);
  19. } catch (e) {
  20. console.log(`%c【錯誤】%c %c"${target}" %c嘗試新增 %cScript %c失敗:\n"${e}"`, "font-size: 16px; color: red", "color: auto", "color: blue", "color: auto", "color: brown", "color: auto");
  21. }
  22. }
  23.  
  24. // 【通用】添加 Style
  25. // 預設添加於 body 尾端.
  26. // 若指定的 target 有複數匹配,則只作用於第一個配對到的.
  27. function addStyle(appendStyle, target="body", id="") {
  28. let css = document.createElement("style");
  29. css.type = "text/css";
  30. css.innerHTML = appendStyle;
  31. css.id = id;
  32. try{
  33. document.querySelectorAll(target)[0].appendChild(css);
  34. } catch (e) {
  35. console.log(`%c【錯誤】%c %c"${target}" %c嘗試新增 %cStyle %c失敗:\n"${e}"`, "font-size: 16px; color: red", "color: auto", "color: blue", "color: auto", "color: brown", "color: auto");
  36. }
  37. }
  38.  
  39. // 【通用】添加 Style Link
  40. function addStyleLink(href, id="", rel="stylesheet", type="text/css") {
  41. let link = document.createElement("link");
  42. link.type = type;
  43. link.rel = rel;
  44. link.href = href;
  45. link.id = id;
  46. try{
  47. document.head.appendChild(link);
  48. } catch (e) {
  49. console.log(`%c【錯誤】%c %c<head> %c嘗試新增 %cStyle Link %c失敗:\n"${e}"`, "font-size: 16px; color: red", "color: auto", "color: blue", "color: auto", "color: brown", "color: auto");
  50. }
  51. }
  52.  
  53. // 【通用】添加 Style,每個屬性皆爲 !important
  54. // 預設添加於 html 尾端. (可用於 @run-at document-start)
  55. // 若指定的 target 有複數匹配,則只作用於第一個配對到的.
  56. function addStyleImportant(appendStyle, target="html", id="") {
  57. let css = document.createElement("style");
  58. let importantStyle = appendStyle.replace(/([a-zA-Z\-]+:[\ ]*.+);/g, "$1 !important;")
  59. css.type = "text/css";
  60. css.innerHTML = importantStyle;
  61. css.id = id;
  62. css.setAttribute("info", "user custom style");
  63. try{
  64. document.querySelectorAll(target)[0].appendChild(css);
  65. } catch (e) {
  66. console.log(`%c【錯誤】%c %c"${target}" %c嘗試新增 %cStyle %c失敗:\n"${e}"`, "font-size: 16px; color: red", "color: auto", "color: blue", "color: auto", "color: brown", "color: auto");
  67. }
  68. }
  69.  
  70. // 【通用】添加 HTML
  71. // 預設添加於 body 尾端.
  72. // 若指定的 target 有複數匹配,則只作用於第一個配對到的.
  73. // type 可選 'beforebegin', 'afterbegin', 'beforeend', 'afterend'.
  74. function addHTML(html, target="body", type="beforeend") {
  75. try{
  76. document.querySelectorAll(target)[0].insertAdjacentHTML(type, html);
  77. } catch (e) {
  78. console.log(`%c【錯誤】%c %c"${target}" %c嘗試新增 %cHTML %c失敗:\n"${e}"`, "font-size: 16px; color: red", "color: auto", "color: blue", "color: auto", "color: brown", "color: auto");
  79. }
  80. }