html-utils

utils for DOM manipulation and fetching info of a webpage

当前为 2016-05-31 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/20131/128690/html-utils.js

  1. // extend HtmlFactory
  2. var HtmlFactoryExtension = (function() {
  3. /**
  4. * Creates a 'button' Html Element.
  5. * The initial class attribute is <code>class='smallbutton'</code>.
  6. * @param {function} [callback] - The callback function for the <code>onClick<code> event
  7. * @param {string} [text] - The content text of the element (text shown on the button)
  8. * @param {string} [id] - The value for the <code>id</code> attribute
  9. * @returns {ElType} The 'button' HTML element
  10. */
  11. var createButtonSmall = function(callback, text, id) {
  12. return HtmlFactory.newButton('smallbutton', callback, text, id);
  13. };
  14. /**
  15. * Creates a 'button' Html Element.
  16. * The initial class attribute is <code>class='tinybutton'</code>.
  17. * @param {function} [callback] - The callback function for the <code>onClick<code> event
  18. * @param {string} [text] - The content text of the element (text shown on the button)
  19. * @param {string} [id] - The value for the <code>id</code> attribute
  20. * @returns {ElType} The 'button' HTML element
  21. */
  22. var createButtonTiny = function ButtonTiny(callback, text, id) {
  23. var elBtn = HtmlFactory.newButton('tinybutton', callback, text, id);
  24. elBtn.style = 'margin:0;padding:0;width:auto;';
  25. return elBtn;
  26. };
  27. /**
  28. * Creates a 'Select' HTML element for selecting camamba users.
  29. * The options will be generated by the list of users.
  30. * The initial class attribute is <code>class='smallselect'</code>.
  31. * @param {Object<string, User>[]} users - The list of users shown as options
  32. * @param {string} [id] - The value for the <code>id</code> attribute
  33. * @returns {ElType} The 'select' HTML element
  34. */
  35. var createSelectUsers = function(users, id) {
  36. var elSelect = HtmlFactory.newSelect('smallselect', id);
  37. var _onchangeCallback;
  38. /**
  39. * Returns the user of the selected option
  40. * @returns {User} The current selected user
  41. */
  42. elSelect.getSelectedUser = function() {
  43. return elSelect.options[elSelect.selectedIndex].user;
  44. };
  45. /**
  46. * Sets a callback function triggered by the events <code>OnChange</code>, <code>OnKeyUp</code> and <code>OnFocus</code>.
  47. * Removes any former callback function registered to these events.
  48. * @param {function} callback
  49. */
  50. elSelect.setOnChangeKeyUpFocus = function(callback) {
  51. if (_onchangeCallback) {
  52. elSelect.removeEventListener("focus", _onchangeCallback);
  53. elSelect.removeEventListener("change", _onchangeCallback);
  54. elSelect.removeEventListener("keyup", _onchangeCallback);
  55. }
  56. if (typeof callback === 'function') {
  57. _onchangeCallback = callback;
  58. elSelect.addEventListener("focus", callback);
  59. elSelect.addEventListener("change", callback);
  60. elSelect.addEventListener("keyup", callback);
  61. }
  62. };
  63. /**
  64. * The options will be regenerated by the list of users.
  65. * @param {Object<string, User>[]} users - The list of users shown as options
  66. */
  67. elSelect.updateUsers = function(users) {
  68. var sortUsers = [];
  69. var remainingUsers = {};
  70. for (var i = 0; i <= elSelect.length - 1; i++) {
  71. var userInSelect = elSelect[i].user;
  72. if (!users[userInSelect.uid]) { // deleted users
  73. elSelect.remove(i);
  74. } else { // remaining users
  75. remainingUsers[userInSelect.uid] = true;
  76. sortUsers.push({ user:userInSelect, selected:elSelect[i].selected });
  77. }
  78. }
  79. Object.keys(users).forEach(function(uid){
  80. if (!remainingUsers[uid]) { // additional users
  81. var user = users[uid];
  82. sortUsers.push({ user:user, selected:false });
  83. /**
  84. * Html 'Option' Child of a Html 'Select' Element that holds a User
  85. * @type {HTMLOptionElement}
  86. * @property {User} user - The User related to the option
  87. */
  88. elSelect.add(document.createElement('OPTION'));
  89. }
  90. });
  91. elSelect.length = sortUsers.length;
  92. sortUsers.sort(function (a, b) {
  93. if (a.user.uname < b.user.uname) { return -1; }
  94. if (a.user.uname > b.user.uname) { return 1; }
  95. return 0;
  96. });
  97. sortUsers.forEach(function (opt, i) {
  98. elSelect[i].text = opt.user.uname;
  99. elSelect[i].value = opt.user.uid;
  100. elSelect[i].user = opt.user;
  101. elSelect[i].selected = opt.selected;
  102. });
  103. };
  104. elSelect.updateUsers(users);
  105. return elSelect;
  106. };
  107. return {
  108. newButtonSmall : createButtonSmall,
  109. newButtonTiny : createButtonTiny,
  110. newSelectUsers : createSelectUsers
  111. }
  112. })();
  113.  
  114. Object.keys(HtmlFactoryExtension).forEach(function (propName) {
  115. HtmlFactory[propName] = HtmlFactoryExtension[propName];
  116. });
  117.  
  118. //extend Page
  119. var PageExtension = (function() {
  120. var isGerman = window.location.hostname.indexOf("de.camamba.com") >= 0;
  121. var uriRoute = /^\/(.+?)(?:_de)?\.php.*/g.exec(location.pathname)[1];
  122. /**
  123. * Verifies an uri, if it loads the German version of camamba.
  124. * @param {string} uri The uri for a camamba Page.
  125. * @returns {boolean} <code>true</code> if the uri will request a camamba Page in German.
  126. */
  127. var uriIsGerman = function(uri) {
  128. return (uri.indexOf('www.de.camamba.com') >= 0);
  129. };
  130. /**
  131. * Transforms the uri of a camamba Page wether to request that Page in English or German, depending the language of the current Page.
  132. * @param {string} uri - The uri for a cammaba Page.
  133. * @param {Object.<string,string>[]} [queryParamsObj] - A key-value Object for additional query parameter to be attached to the uri.
  134. * @returns {string} The localized uri
  135. */
  136. var uriLocalized = function(uri, queryParamsObj) {
  137. var localizedUri = uri;
  138. if (isGerman && !uriIsGerman(uri)) {
  139. localizedUri = uri
  140. .replace("www.camamba.com", "www.de.camamba.com")
  141. .replace(".php", "_de.php");
  142. } else if (!isGerman && uriIsGerman(uri)) {
  143. localizedUri = uri
  144. .replace("www.de.camamba.com", "www.camamba.com")
  145. .replace("_de.php", "php");
  146. }
  147. var queryParams = '';
  148. if (queryParamsObj) {
  149. var hasParams = uri.indexOf('.php?') >= 1;
  150. Object.keys(queryParamsObj).forEach(function (key) {
  151. var sep = (hasParams ? '&' : '?');
  152. var value = queryParamsObj[key];
  153. queryParams += sep + key + '=' + value;
  154. hasParams = true;
  155. });
  156. }
  157. return localizedUri + queryParams;
  158. };
  159. return {
  160. /**
  161. * Indicates the localization of the current camamba Page.
  162. * @returns {boolean} <code>true</code> if the current Page is in German
  163. * <code>false</code> the current Page is in English
  164. */
  165. isGerman : isGerman,
  166. /**
  167. * The current path in camamba according to the uri.
  168. * @returns {string} The current path.
  169. */
  170. route : uriRoute,
  171. localizeUri : uriLocalized
  172. };
  173. })();
  174. Object.keys(PageExtension).forEach(function (propName) {
  175. Page[propName] = PageExtension[propName];
  176. });
  177.  
  178.  
  179. /**
  180. * @name uidType
  181. * @type string|number
  182. */
  183.  
  184. /**
  185. * Represents a camamba user.
  186. * Intitially tries to load the User from the database that has the given uid.
  187. * @constructor
  188. * @param {uidType} uid Identifies the User with its camaba uid.
  189. */
  190. function User(uid) {
  191. var _uid = parseInt(uid, 10); // camamba user ID (readonly)
  192. var _key = 'uid' + _uid, // key for storing (readonly)
  193. _uname = "", // camamba username
  194. _name = "", // custom username
  195. _note = "", // custom annotation
  196. _hasChanged = true; // is any value is unsaved (readonly)
  197. /**
  198. * @name User#uid
  199. * @type number
  200. * @readonly
  201. */
  202. /**
  203. * @name User#key
  204. * @type String
  205. * @readonly
  206. */
  207. /**
  208. * @name User#hasChanged
  209. * @type Boolean
  210. * @readonly
  211. */
  212. /**
  213. * @name User#note
  214. * @type String
  215. */
  216. Object.defineProperties(this, {
  217. uid : { value : _uid, writable : false },
  218. key : { value : _key, writable : false },
  219. hasChanged : { get : function() { return _hasChanged; } },
  220. uname : {
  221. get : function() { return _uname; },
  222. set : function(val) {
  223. if (_uname != val) {
  224. _uname = val;
  225. _hasChanged = true;
  226. }
  227. }
  228. },
  229. name : {
  230. get : function() { return _name || _uname || _uid.toString(); },
  231. set : function(val) {
  232. if (_name != val) {
  233. _name = val;
  234. _hasChanged = true;
  235. }
  236. }
  237. },
  238. note : {
  239. get : function() { return _note; },
  240. set : function(val) {
  241. if (_note != val) {
  242. _note = val;
  243. _hasChanged = true;
  244. }
  245. }
  246. }
  247. });
  248. /**
  249. * Saves or updates the user in the database of this script.
  250. * Overwrites an existing entry or creates a new entry if it doesn't alread exist.
  251. */
  252. this.save = function() {
  253. User.prototype.save.call(this);
  254. _hasChanged = false;
  255. };
  256. /**
  257. * Loads the User from the database of this script.
  258. * @returns {Boolean} <code>true</code> if the user was found and could be sucessfully loaded from db
  259. */
  260. this.load = function() {
  261. var isSuccess = User.prototype.load.call(this);
  262. if (isSuccess) {_hasChanged = false; }
  263. return isSuccess;
  264. };
  265. /**
  266. * Removes the User from the database of this script.
  267. */
  268. this.remove = function () {
  269. User.prototype.remove.call(this);
  270. _hasChanged = true;
  271. };
  272. this.load();
  273. }
  274. User.prototype = {
  275. constructor : User,
  276. save : function() {
  277. if (this.hasChanged) {
  278. GM_setValue("uid" + this.uid, JSON.stringify({
  279. uname : this.uname,
  280. name : this.name,
  281. note : this.note
  282. }));
  283. }
  284. },
  285. load : function() {
  286. var isScuccess = false;
  287. var loadedString = GM_getValue("uid" + this.uid);
  288. if (loadedString) {
  289. var loadedObj = JSON.parse(loadedString);
  290. var uname = loadedObj.uname;
  291. var name = loadedObj.name;
  292. var note = loadedObj.note;
  293. if (uname !== undefined && name !== undefined && note !== undefined){
  294. this.uname = uname;
  295. this.name = name;
  296. this.note = note;
  297. isScuccess = true;
  298. }
  299. }
  300. return isScuccess;
  301. },
  302. remove : function() {
  303. GM_deleteValue("uid" + this.uid);
  304. },
  305. /**
  306. * Gets all users stored from the database determined for this Script.
  307. * @returns {{}<uidType,User>[]} List with all Stored Users
  308. */
  309. loadAllUsers : function() {
  310. var users = {};
  311. var storedKeys = GM_listValues();
  312. for (var i = 0; i <= storedKeys.length - 1; i++) {
  313. var key = storedKeys[i].toString();
  314. if (key.indexOf('uid') === 0) {
  315. var uid = key.substr(3);
  316. users[uid] = new User(uid);
  317. }
  318. }
  319. return users;
  320. },
  321. /**
  322. * Has the browser open the profile Page of this user.
  323. * @param {boolean} [asNewTab=false]
  324. * <code>true</code>, Page is opened in a new tab.
  325. * <code>false</code>, replaces the current Page.
  326. */
  327. openProfilePage : function (asNewTab) {
  328. var profPageLocPath = location.protocol + '//www.camamba.com/profile_view.php';
  329. var queryParamsObj = { uid : this.uid, m : 'start' };
  330. var uri = Page.localizeUri(profPageLocPath, queryParamsObj);
  331. var target = asNewTab ? '_blank' : '_self';
  332. window.open(uri, target);
  333. }
  334. };

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址