Greasy Fork is available in English.

Functions

this is my biblothek for functions that I often use in my Userscripts

Questo script non dovrebbe essere installato direttamente. È una libreria per altri script da includere con la chiave // @require https://update.greatest.deepsurf.us/scripts/24991/158903/Functions.js

  1. // ==UserScript==
  2. // @name Functions
  3. // @description this is my biblothek for functions that I often use in my Userscripts
  4. // ==/UserScript==
  5. function copy(id) {
  6. var copy = document.getElementById(id);
  7. copy.contenteditable = true;
  8. copy.select();
  9. document.execCommand('copy');
  10. }
  11.  
  12. function dom() {
  13. return new Element('html', 'style|width:100%;height:100%', '','|')
  14. }
  15.  
  16. function popup(title ,text, param)
  17. {
  18. var popup = window.open('about:blank', title, param || 'width=1000px;height=500px');
  19. popup.document.write(text);
  20. popup.focus();
  21. };
  22.  
  23. function popupUrl(title, url, param){
  24. var popup = window.open(url, title, param)
  25. };
  26.  
  27. function getCookie(cname){
  28. var name = cname + "=";
  29. var ca = document.cookie.split(';');
  30. for(var i = 0; i <ca.length; i++) {
  31. var c = ca[i];
  32. while (c.charAt(0)==' ') {
  33. c = c.substring(1);
  34. }
  35. if (c.indexOf(name) == 0) {
  36. return c.substring(name.length,c.length);
  37. }
  38. }
  39. return "";
  40. };
  41.  
  42. function setCookie(cname, cvalue, exdays){
  43. var d = new Date();
  44. d.setTime(d.getTime() + (exdays*24*60*60*1000));
  45. var expires = "expires="+ d.toUTCString();
  46. document.cookie = cname + "=" + cvalue + "; " + expires;
  47. };
  48.  
  49. function postToUrl(url, param, method){
  50. var method = method || 'POST',
  51. param = param.split(';');
  52. p = [],
  53. form = new Element('form', 'action|' + url + '|method|' + method, '', '|');
  54. for (var i = 0; i < param.length; i++) {
  55. p.push(param[i].split('=')[0]);
  56. p.push(param[i].split('=')[1]);
  57. }
  58. param = p;
  59. p = 1;
  60. delete p;
  61. for (var i = 0; i < param.length; i++) {
  62. form.appendChild(new Element('input', 'type|hidden|name|' + param[i] + '|value|' + param[i + 1], '', '|'));
  63. i++
  64. }
  65. document.body.appendChild(form);
  66. form.submit()
  67. }
  68.  
  69. function postUrlToUrl(url, param, method){
  70. var method = method || 'POST',
  71. param = param.split(';');
  72. p = [],
  73. form = new Element('form', 'action|' + url + '|method|' + method, '', '|');
  74. for (var i = 0; i < param.length; i++) {
  75. p.push(param[i].split('|')[0]);
  76. p.push(param[i].split('|')[1]);
  77. }
  78. param = p;
  79. p = 1;
  80. delete p;
  81. for (var i = 0; i < param.length; i++) {
  82. form.appendChild(new Element('input', 'type|hidden|name|' + param[i] + '|value|' + param[i + 1], '', '|'));
  83. i++
  84. }
  85. document.body.appendChild(form);
  86. form.submit()
  87. }
  88.  
  89. function postToPopup(url, param, method, popupParam){
  90. var method = method || 'POST',
  91. param = param.split(';');
  92. p = [],
  93. form = new Element('form', 'action|' + url + '|method|' + method, '', '|');
  94. for (var i = 0; i < param.length; i++) {
  95. p.push(param[i].split('=')[0]);
  96. p.push(param[i].split('=')[1]);
  97. }
  98. param = p;
  99. p = 1;
  100. delete p;
  101. for (var i = 0; i < param.length; i++) {
  102. form.appendChild(new Element('input', 'type|hidden|name|' + param[i] + '|value|' + param[i + 1], '', '|'));
  103. i++
  104. }
  105. var popup = window.open('about:blank', '', 'width=890,height=500,left=100');
  106. popup.document.body.appendChild(form);
  107. popup.document.forms[0].submit()
  108. popup.focus()
  109. }
  110.  
  111. function Element(tag, attr, html, splitAt){
  112. var elem,
  113. attributes = (splitAt != undefined || splitAt != null || splitAt != '') ? (attr != undefined && attr != '') ? attr.split(splitAt) : '' : (attr != undefined && attr != '') ? attr.split(';') : '' ;
  114. if(tag == undefined || tag == ''){
  115. tag = 'div';
  116. }
  117. elem = document.createElement(tag)
  118. if(attributes != '' && attr != '' && attr != undefined){
  119. for(i = 0; i < attributes.length;){
  120. elem.setAttribute(attributes[i], attributes[i + 1]);
  121. i += 2
  122. }
  123. }
  124. elem.innerHTML = html;
  125. return elem
  126. }
  127.  
  128. function select(objId) {
  129. if (document.selection) {
  130. var range = document.body.createTextRange();
  131. range.moveToElementText(document.getElementById(objId));
  132. range.select();
  133. } else if (window.getSelection) {
  134. var range = document.createRange();
  135. range.selectNode(document.getElementById(objId));
  136. window.getSelection().addRange(range);
  137. }
  138. }
  139.  
  140. function scrollToPos(pos, speed) {
  141. var int = setInterval(function(){
  142. if(scrollY == pos){
  143. clearInterval(int)
  144. }else{
  145. if(Math.floor(scrollY) > Math.floor(pos)){
  146. scrollTo(scrollX, scrollY-1);
  147. }else{
  148. scrollTo(scrollX, scrollY+1);
  149. }
  150. }
  151. }, speed)
  152. }
  153.  
  154. function login(lname, pwd){
  155. postToUrl('', 'lname=' + lname + ';pwd=' + pwd, ';POST')
  156. }
  157.  
  158. function register(rname, pwd, data){
  159. postToUrl('', 'rname=' + rname + ';pwd=' + pwd + ';data=' + data)
  160. }
  161.  
  162. function turnOut(elem, dir){
  163. var d = 1,
  164. dir = dir || 'X',
  165. int = setInterval(function(){rotate(elem)}, 1);
  166. function rotate(e){
  167. if(d >= 91){
  168. clearInterval(int)
  169. }else{
  170. e.style.transform = 'rotate' + dir + '(' + d + 'deg)'
  171. d++
  172. }
  173. }
  174. setTimeout(function(){
  175. elem.style.position = 'absolute'
  176. }, 800)
  177. }
  178.  
  179. function turnIn(elem){
  180. elem.style.position = 'initial'
  181. if (elem.style.transform.charAt(6) == 'X') {
  182. elem.style.transform = 'rotateX(-90deg)'
  183. }else if (elem.style.transform.charAt(6) == 'Y') {
  184. elem.style.transform = 'rotateY(-90deg)';
  185. }
  186. var d = 1,
  187. int = setInterval(function(){rotate(elem)}, 1);
  188. function rotate(e){
  189. if(e.style.transform.charAt(6) == 'X'){
  190. if(d >= 361){
  191. clearInterval(int)
  192. }else{
  193. e.style.transform = 'rotateX(' + d + 'deg)'
  194. d++
  195. }
  196. }else if(e.style.transform.charAt(6) == 'Y'){
  197. if(d >= 361){
  198. clearInterval(int)
  199. }else{
  200. e.style.transform = 'rotateY(' + d + 'deg)'
  201. d++
  202. }
  203. }
  204. }
  205. }
  206.  
  207. function help(id, help){
  208. $('#' + id).css('text-decoration: underline dotted')
  209. document.getElementById(id).addEventListener('mouseover', function(){
  210. document.getElementById(id).title = help
  211. })
  212. document.getElementById(id).addEventListener('mouseout', function(){
  213. document.getElementById(id).title = '';
  214. })
  215. }
  216.  
  217. /*
  218. Catch keypress
  219. ==============
  220.  
  221. window.addEventListener('keydown', function(event){
  222. if(event.keyCode == 'keyCode'){
  223. event.cancelBubble = true;
  224. event.returnValue = false;
  225. }
  226. return event.returnValue
  227. })
  228.  
  229. OR
  230.  
  231. window.addEventListener('keydown', function(event){
  232. if (event.keyCode == 'keyCode'){
  233. //do something
  234. }
  235. event.preventDefault()
  236. })
  237. */