alihesari's notice.js 0.4.0

移植github修复版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/430098/985170/alihesari%27s%20noticejs%20040.js

  1. // ==UserScript==
  2. // @license MIT
  3. // @original-script https://github.com/alihesari/notice.js
  4. // @original-author Toolkito
  5. // @original-license MIT
  6. // ==/UserScript==
  7. // MIT License
  8.  
  9. // Copyright (c) 2018 Toolkito
  10.  
  11. // Permission is hereby granted, free of charge, to any person obtaining a copy
  12. // of this software and associated documentation files (the "Software"), to deal
  13. // in the Software without restriction, including without limitation the rights
  14. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. // copies of the Software, and to permit persons to whom the Software is
  16. // furnished to do so, subject to the following conditions:
  17.  
  18. // The above copyright notice and this permission notice shall be included in all
  19. // copies or substantial portions of the Software.
  20.  
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  27. // SOFTWARE.
  28.  
  29. (function webpackUniversalModuleDefinition(root, factory) {
  30. if(typeof exports === 'object' && typeof module === 'object')
  31. module.exports = factory();
  32. else if(typeof define === 'function' && define.amd)
  33. define("NoticeJs", [], factory);
  34. else if(typeof exports === 'object')
  35. exports["NoticeJs"] = factory();
  36. else
  37. root["NoticeJs"] = factory();
  38. })(typeof self !== 'undefined' ? self : this, function() {
  39. return /******/ (function(modules) { // webpackBootstrap
  40. /******/ // The module cache
  41. /******/ var installedModules = {};
  42. /******/
  43. /******/ // The require function
  44. /******/ function __webpack_require__(moduleId) {
  45. /******/
  46. /******/ // Check if module is in cache
  47. /******/ if(installedModules[moduleId]) {
  48. /******/ return installedModules[moduleId].exports;
  49. /******/ }
  50. /******/ // Create a new module (and put it into the cache)
  51. /******/ var module = installedModules[moduleId] = {
  52. /******/ i: moduleId,
  53. /******/ l: false,
  54. /******/ exports: {}
  55. /******/ };
  56. /******/
  57. /******/ // Execute the module function
  58. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  59. /******/
  60. /******/ // Flag the module as loaded
  61. /******/ module.l = true;
  62. /******/
  63. /******/ // Return the exports of the module
  64. /******/ return module.exports;
  65. /******/ }
  66. /******/
  67. /******/
  68. /******/ // expose the modules object (__webpack_modules__)
  69. /******/ __webpack_require__.m = modules;
  70. /******/
  71. /******/ // expose the module cache
  72. /******/ __webpack_require__.c = installedModules;
  73. /******/
  74. /******/ // define getter function for harmony exports
  75. /******/ __webpack_require__.d = function(exports, name, getter) {
  76. /******/ if(!__webpack_require__.o(exports, name)) {
  77. /******/ Object.defineProperty(exports, name, {
  78. /******/ configurable: false,
  79. /******/ enumerable: true,
  80. /******/ get: getter
  81. /******/ });
  82. /******/ }
  83. /******/ };
  84. /******/
  85. /******/ // getDefaultExport function for compatibility with non-harmony modules
  86. /******/ __webpack_require__.n = function(module) {
  87. /******/ var getter = module && module.__esModule ?
  88. /******/ function getDefault() { return module['default']; } :
  89. /******/ function getModuleExports() { return module; };
  90. /******/ __webpack_require__.d(getter, 'a', getter);
  91. /******/ return getter;
  92. /******/ };
  93. /******/
  94. /******/ // Object.prototype.hasOwnProperty.call
  95. /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  96. /******/
  97. /******/ // __webpack_public_path__
  98. /******/ __webpack_require__.p = "dist/";
  99. /******/
  100. /******/ // Load entry module and return exports
  101. /******/ return __webpack_require__(__webpack_require__.s = 2);
  102. /******/ })
  103. /************************************************************************/
  104. /******/ ([
  105. /* 0 */
  106. /***/ (function(module, exports, __webpack_require__) {
  107.  
  108. "use strict";
  109.  
  110.  
  111. Object.defineProperty(exports, "__esModule", {
  112. value: true
  113. });
  114. var noticeJsModalClassName = exports.noticeJsModalClassName = 'noticejs-modal';
  115. var closeAnimation = exports.closeAnimation = 'noticejs-fadeOut';
  116.  
  117. var Defaults = exports.Defaults = {
  118. title: '',
  119. text: '',
  120. type: 'success',
  121. position: 'topRight',
  122. timeout: 30,
  123. progressBar: true,
  124. closeWith: ['button'],
  125. animation: null,
  126. modal: false,
  127. scroll: {
  128. maxHeight: 300,
  129. showOnHover: true
  130. },
  131. rtl: false,
  132. callbacks: {
  133. beforeShow: [],
  134. onShow: [],
  135. afterShow: [],
  136. onClose: [],
  137. afterClose: [],
  138. onClick: [],
  139. onHover: [],
  140. onTemplate: []
  141. }
  142. };
  143.  
  144. /***/ }),
  145. /* 1 */
  146. /***/ (function(module, exports, __webpack_require__) {
  147.  
  148. "use strict";
  149.  
  150.  
  151. Object.defineProperty(exports, "__esModule", {
  152. value: true
  153. });
  154. exports.appendNoticeJs = exports.addListener = exports.CloseItem = exports.AddModal = undefined;
  155. exports.getCallback = getCallback;
  156.  
  157. var _api = __webpack_require__(0);
  158.  
  159. var API = _interopRequireWildcard(_api);
  160.  
  161. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  162.  
  163. var options = API.Defaults;
  164.  
  165. /**
  166. * @param {NoticeJs} ref
  167. * @param {string} eventName
  168. * @return {void}
  169. */
  170. function getCallback(ref, eventName) {
  171. if (ref.callbacks.hasOwnProperty(eventName)) {
  172. ref.callbacks[eventName].forEach(function (cb) {
  173. if (typeof cb === 'function') {
  174. cb.apply(ref);
  175. }
  176. });
  177. }
  178. }
  179.  
  180. var AddModal = exports.AddModal = function AddModal() {
  181. if (document.getElementsByClassName(API.noticeJsModalClassName).length <= 0) {
  182. var element = document.createElement('div');
  183. element.classList.add(API.noticeJsModalClassName);
  184. element.classList.add('noticejs-modal-open');
  185. document.body.appendChild(element);
  186. // Remove class noticejs-modal-open
  187. setTimeout(function () {
  188. element.className = API.noticeJsModalClassName;
  189. }, 200);
  190. }
  191. };
  192.  
  193. var CloseItem = exports.CloseItem = function CloseItem(item) {
  194. getCallback(options, 'onClose');
  195.  
  196. // Set animation to close notification item
  197. if (options.animation !== null && options.animation.close !== null) {
  198. item.className += ' ' + options.animation.close;
  199. }
  200. setTimeout(function () {
  201. item.remove();
  202. }, 200);
  203.  
  204. // Close modal
  205. if (options.modal === true && document.querySelectorAll("[noticejs-modal='true']").length >= 1) {
  206. document.querySelector('.noticejs-modal').className += ' noticejs-modal-close';
  207. setTimeout(function () {
  208. document.querySelector('.noticejs-modal').remove();
  209. }, 500);
  210. }
  211.  
  212. // Remove container
  213. var position = '.' + item.closest('.noticejs').className.replace('noticejs', '').trim();
  214. setTimeout(function () {
  215. if (document.querySelectorAll(position + ' .item').length <= 0) {
  216. document.querySelector(position) && document.querySelector(position).remove();
  217. }
  218. }, 500);
  219. };
  220.  
  221. var addListener = exports.addListener = function addListener(item) {
  222. // Add close button Event
  223. if (options.closeWith.includes('button')) {
  224. item.querySelector('.close').addEventListener('click', function () {
  225. CloseItem(item);
  226. });
  227. }
  228.  
  229. // Add close by click Event
  230. if (options.closeWith.includes('click')) {
  231. item.style.cursor = 'pointer';
  232. item.addEventListener('click', function (e) {
  233. if (e.target.className !== 'close') {
  234. getCallback(options, 'onClick');
  235. CloseItem(item);
  236. }
  237. });
  238. } else {
  239. item.addEventListener('click', function (e) {
  240. if (e.target.className !== 'close') {
  241. getCallback(options, 'onClick');
  242. }
  243. });
  244. }
  245.  
  246. item.addEventListener('mouseover', function () {
  247. getCallback(options, 'onHover');
  248. });
  249. };
  250.  
  251. var appendNoticeJs = exports.appendNoticeJs = function appendNoticeJs(noticeJsHeader, noticeJsBody, noticeJsProgressBar) {
  252. var target_class = '.noticejs-' + options.position;
  253. // Create NoticeJs item
  254. var noticeJsItem = document.createElement('div');
  255. noticeJsItem.classList.add('item');
  256. noticeJsItem.classList.add(options.type);
  257. if (options.rtl === true) {
  258. noticeJsItem.classList.add('noticejs-rtl');
  259. }
  260.  
  261. // Add Header
  262. if (noticeJsHeader && noticeJsHeader !== '') {
  263. noticeJsItem.appendChild(noticeJsHeader);
  264. }
  265.  
  266. // Add body
  267. noticeJsItem.appendChild(noticeJsBody);
  268.  
  269. // Add progress bar
  270. if (noticeJsProgressBar && noticeJsProgressBar !== '') {
  271. noticeJsItem.appendChild(noticeJsProgressBar);
  272. }
  273.  
  274. // Empty top and bottom container
  275. if (['top', 'bottom'].includes(options.position)) {
  276. document.querySelector(target_class).innerHTML = '';
  277. }
  278.  
  279. // Add open animation
  280. if (options.animation !== null && options.animation.open !== null) {
  281. noticeJsItem.className += ' ' + options.animation.open;
  282. }
  283.  
  284. // Add Modal
  285. if (options.modal === true) {
  286. noticeJsItem.setAttribute('noticejs-modal', 'true');
  287. AddModal();
  288. }
  289.  
  290. // Add Listener
  291. addListener(noticeJsItem, options.closeWith);
  292.  
  293. getCallback(options, 'beforeShow');
  294. getCallback(options, 'onShow');
  295. document.querySelector(target_class).appendChild(noticeJsItem);
  296. getCallback(options, 'afterShow');
  297.  
  298. return noticeJsItem;
  299. };
  300.  
  301. /***/ }),
  302. /* 2 */
  303. /***/ (function(module, exports, __webpack_require__) {
  304.  
  305. "use strict";
  306.  
  307.  
  308. Object.defineProperty(exports, "__esModule", {
  309. value: true
  310. });
  311.  
  312. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  313.  
  314. var _noticejs = __webpack_require__(3);
  315.  
  316. var _noticejs2 = _interopRequireDefault(_noticejs);
  317.  
  318. var _api = __webpack_require__(0);
  319.  
  320. var API = _interopRequireWildcard(_api);
  321.  
  322. var _components = __webpack_require__(4);
  323.  
  324. var _helpers = __webpack_require__(1);
  325.  
  326. var helper = _interopRequireWildcard(_helpers);
  327.  
  328. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  329.  
  330. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  331.  
  332. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  333.  
  334. var NoticeJs = function () {
  335. /**
  336. * @param {object} options
  337. * @returns {Noty}
  338. */
  339. function NoticeJs() {
  340. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  341.  
  342. _classCallCheck(this, NoticeJs);
  343.  
  344. this.options = Object.assign(API.Defaults, options);
  345. this.component = new _components.Components();
  346.  
  347. this.on('beforeShow', this.options.callbacks.beforeShow);
  348. this.on('onShow', this.options.callbacks.onShow);
  349. this.on('afterShow', this.options.callbacks.afterShow);
  350. this.on('onClose', this.options.callbacks.onClose);
  351. this.on('afterClose', this.options.callbacks.afterClose);
  352. this.on('onClick', this.options.callbacks.onClick);
  353. this.on('onHover', this.options.callbacks.onHover);
  354.  
  355. return this;
  356. }
  357.  
  358. /**
  359. * @returns {NoticeJs}
  360. */
  361.  
  362.  
  363. _createClass(NoticeJs, [{
  364. key: 'show',
  365. value: function show() {
  366. var container = this.component.createContainer();
  367. if (document.querySelector('.noticejs-' + this.options.position) === null) {
  368. document.body.appendChild(container);
  369. }
  370.  
  371. var noticeJsHeader = void 0;
  372. var noticeJsBody = void 0;
  373. var noticeJsProgressBar = void 0;
  374.  
  375. // Create NoticeJs header
  376. noticeJsHeader = this.component.createHeader(this.options.title, this.options.closeWith);
  377.  
  378. // Create NoticeJs body
  379. noticeJsBody = this.component.createBody(this.options.text);
  380.  
  381. // Create NoticeJs progressBar
  382. if (this.options.progressBar === true) {
  383. noticeJsProgressBar = this.component.createProgressBar();
  384. }
  385.  
  386. //Append NoticeJs
  387. var noticeJs = helper.appendNoticeJs(noticeJsHeader, noticeJsBody, noticeJsProgressBar);
  388.  
  389. return noticeJs;
  390. }
  391.  
  392. /**
  393. * @param {string} eventName
  394. * @param {function} cb
  395. * @return {NoticeJs}
  396. */
  397.  
  398. }, {
  399. key: 'on',
  400. value: function on(eventName) {
  401. var cb = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {};
  402.  
  403. if (typeof cb === 'function' && this.options.callbacks.hasOwnProperty(eventName)) {
  404. this.options.callbacks[eventName].push(cb);
  405. }
  406.  
  407. return this;
  408. }
  409. }]);
  410.  
  411. return NoticeJs;
  412. }();
  413.  
  414. exports.default = NoticeJs;
  415. module.exports = exports['default'];
  416.  
  417. /***/ }),
  418. /* 3 */
  419. /***/ (function(module, exports) {
  420.  
  421. // removed by extract-text-webpack-plugin
  422.  
  423. /***/ }),
  424. /* 4 */
  425. /***/ (function(module, exports, __webpack_require__) {
  426.  
  427. "use strict";
  428.  
  429.  
  430. Object.defineProperty(exports, "__esModule", {
  431. value: true
  432. });
  433. exports.Components = undefined;
  434.  
  435. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  436.  
  437. var _api = __webpack_require__(0);
  438.  
  439. var API = _interopRequireWildcard(_api);
  440.  
  441. var _helpers = __webpack_require__(1);
  442.  
  443. var helper = _interopRequireWildcard(_helpers);
  444.  
  445. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  446.  
  447. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  448.  
  449. var options = API.Defaults;
  450.  
  451. var Components = exports.Components = function () {
  452. function Components() {
  453. _classCallCheck(this, Components);
  454. }
  455.  
  456. _createClass(Components, [{
  457. key: 'createContainer',
  458. value: function createContainer() {
  459. var element_class = 'noticejs-' + options.position;
  460. var element = document.createElement('div');
  461. element.classList.add('noticejs');
  462. element.classList.add(element_class);
  463.  
  464. return element;
  465. }
  466. }, {
  467. key: 'createHeader',
  468. value: function createHeader() {
  469. var element = void 0;
  470. if (options.title && options.title !== '') {
  471. element = document.createElement('div');
  472. element.setAttribute('class', 'noticejs-heading');
  473. element.textContent = options.title;
  474. }
  475.  
  476. // Add close button
  477. if (options.closeWith.includes('button')) {
  478. var close = document.createElement('div');
  479. close.setAttribute('class', 'close');
  480. close.innerHTML = '&times;';
  481. if (element) {
  482. element.appendChild(close);
  483. } else {
  484. element = close;
  485. }
  486. }
  487.  
  488. return element;
  489. }
  490. }, {
  491. key: 'createBody',
  492. value: function createBody() {
  493. var element = document.createElement('div');
  494. element.setAttribute('class', 'noticejs-body');
  495. var content = document.createElement('div');
  496. content.setAttribute('class', 'noticejs-content');
  497. content.innerHTML = options.text;
  498. element.appendChild(content);
  499.  
  500. if (options.scroll !== null && options.scroll.maxHeight !== '') {
  501. element.style.overflowY = 'auto';
  502. element.style.maxHeight = options.scroll.maxHeight + 'px';
  503.  
  504. if (options.scroll.showOnHover === true) {
  505. element.style.visibility = 'hidden';
  506. }
  507. }
  508. return element;
  509. }
  510. }, {
  511. key: 'createProgressBar',
  512. value: function createProgressBar() {
  513. var element = document.createElement('div');
  514. element.setAttribute('class', 'noticejs-progressbar');
  515. var bar = document.createElement('div');
  516. bar.setAttribute('class', 'noticejs-bar');
  517. element.appendChild(bar);
  518.  
  519. // Progress bar animation
  520. if (options.progressBar === true && typeof options.timeout !== 'boolean' && options.timeout !== false) {
  521. var frame = function frame() {
  522. if (width <= 0) {
  523. clearInterval(id);
  524.  
  525. var item = element.closest('div.item');
  526. // Add close animation
  527. if (options.animation !== null && options.animation.close !== null) {
  528.  
  529. // Remove open animation class
  530. item.className = item.className.replace(new RegExp('(?:^|\\s)' + options.animation.open + '(?:\\s|$)'), ' ');
  531. // Add close animation class
  532. item.className += ' ' + options.animation.close;
  533.  
  534. // Close notification after 0.5s + timeout
  535. var close_time = parseInt(options.timeout) + 500;
  536. setTimeout(function () {
  537. helper.CloseItem(item);
  538. }, close_time);
  539. } else {
  540. // Close notification when progress bar completed
  541. helper.CloseItem(item);
  542. }
  543. } else {
  544. width--;
  545. bar.style.width = width + '%';
  546. }
  547. };
  548.  
  549. var width = 100;
  550. var id = setInterval(frame, options.timeout);
  551. }
  552.  
  553. return element;
  554. }
  555. }]);
  556.  
  557. return Components;
  558. }();
  559.  
  560. /***/ })
  561. /******/ ]);
  562. });

QingJ © 2025

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