EnstylerJS

MyDealz Enstyler Frontend and enhanced features

当前为 2016-11-02 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name EnstylerJS
  3. // @namespace Enstyler
  4. // @description MyDealz Enstyler Frontend and enhanced features
  5. // @include http://www.mydealz.de/*
  6. // @include https://www.mydealz.de/*
  7. // @version 2.11.023
  8. // @grant GM_getValue
  9. // @grant GM_setValue
  10. // @grant GM_log
  11. // @grant GM_xmlhttpRequest
  12. // @require http://code.jquery.com/jquery-3.1.1.min.js
  13. // @require http://cdnjs.cloudflare.com/ajax/libs/jquery-throttle-debounce/1.1/jquery.ba-throttle-debounce.min.js
  14. // @require http://openuserjs.org/src/libs/sizzle/GM_config.js
  15. // ==/UserScript==
  16.  
  17. // ========== RUN EnstylerJS =====================================
  18. // create object for enstyler "Menu Button"
  19. var input = document.createElement('input');
  20. input.type = 'button';
  21. input.value = 'Enstyler';
  22. input.onclick = showEnstylerConfig;
  23.  
  24. function EnstylerInit () {
  25. // hide Enstyler2 CSS (c) text because we have a button now
  26. addStyleString('.threadWidget-footer::after {display: none !important};');
  27.  
  28. // basic config panel formatting, everything else is formatted by enstyler
  29. var enCSS = ['#GM_config {left: 5% !important; top: 9% !important; height: auto !important; max-width: 35em !important;}',
  30. '#GM_config input, #GM_config button { border: 1px solid; margin: 0.5em 0em 0.2em 1em; padding: 0.1em;}',
  31. '#GM_config .reset { font-size: 9pt; padding-right: 1em; }',
  32. ].join(" ");
  33. addStyleString(enCSS);
  34. }
  35.  
  36. function EnstylerJS () {
  37. // add Enstyler Button to ...
  38. var myElement;
  39. if (GM_config.get('enConfBtn') && $(window).width() > GM_config.get('enConfBtnMinWidth')) {
  40. // add button to MainNav
  41. var Elements = document.getElementsByClassName("navMenu-trigger");
  42. myElement = Elements[0];
  43. input.className = 'nav-link-text vAlign--all-m';
  44. } else {
  45. // add button to SubNav
  46. myElement = document.getElementById('tour-filter');
  47. input.className = 'box--all-i subNavMenu-link';
  48. input.setAttribute('style', 'font-size: 1.28571em; font-weight: 700; top: 3px; left: -0.7em');
  49. }
  50. // only if myElement exist
  51. if (myElement !== null) {
  52. myElement.appendChild(input);
  53. }
  54.  
  55. // ============ functions to modify page content =============================
  56.  
  57. // REGEX to detect external URL
  58. var REGEX_THREAD = /^https?:\/\/www\.mydealz\.de\/visit\/thread(image)?\/\d+$/;
  59. var REGEX_DESC = /^https?:\/\/www\.mydealz\.de\/visit\/threaddesc\/\d+\/\d+$/;
  60. var REGEX_COMMENT = /^https?:\/\/www\.mydealz\.de\/visit\/comment\/\d+\/\d+$/;
  61. var REGEX_AMAZONMOB = /^https?:\/\/www\.amazon\..*\/gp\/aw\/.*$/;
  62. //var REGEX_AMAZON = /^https?:\/\/www\.amazon\..*$/;
  63.  
  64. //* GM_xmlhhtpREquest not supported by firefox mobile :-(((
  65. // abfangen aller links mit jQuery
  66. $('a').bind('click', function(){
  67. var url = this.href;
  68.  
  69. // externer Link mit Redirect ...
  70. // Match REDIRECT URLs and open new Window with finalUrl
  71. if (GM_config.get('enConfFilterLink') && url.match(REGEX_THREAD) || url.match(REGEX_DESC) || url.match(REGEX_COMMENT)) {
  72. // Workaround: pre open window because of popup blockers
  73. var asyncWindow = window.open(url, '_blank');
  74. // now lookup redirecd external URL ...
  75. // alert("external URL detected");
  76. GM_xmlhttpRequest({
  77. method: 'GET',
  78. url: url,
  79. // here we get the final URL from redirect
  80. onload: function (response) {
  81. // process final URL
  82. var newUrl = response.finalUrl;
  83. //alert(newUrl);
  84. // lets see ...
  85. if (newUrl.match(REGEX_AMAZONMOB)) {
  86. newUrl = newUrl.replace("/gp/aw/d/", "/dp/");
  87. newUrl = newUrl.replace("/gp/aw/ol/", "/gp/offer-listing/");
  88. //alert("Amazon rewritten URL: " + newUrl);
  89. }
  90. // load processed URL in preopened window
  91. asyncWindow.location = newUrl;
  92. }
  93. });
  94. // return without link processing by Browser
  95. return false;
  96. }
  97. // return with link processing in Browser
  98. return true;
  99. }); // - END GM_xmlhttpRequest */
  100. } // END Enstyler
  101.  
  102. function EnstylerAvatarPopup() {
  103. // show popup user info while click on avatar ...
  104. // code used for MyDealz avatar popup, thanks to mydealz :-)
  105. var enPopupUser = ['<button data-handler="track popover" data-track="{&quot;action&quot;:&quot;show_short_user_profile&quot;,&quot;label&quot;:&quot;engagement&quot;}"data-popover="{&quot;endpoint&quot;:&quot;',
  106. '/short&quot;,&quot;target&quot;:&quot;#template-popoverLoader&quot;,&quot;layout&quot;:[{&quot;preset&quot;:&quot;e&quot;,&quot;y&quot;:&quot;50%&quot;,&quot;left&quot;:{&quot;offset&quot;:0},&quot;width&quot;:300,&quot;maxWidth&quot;:&quot;100%&quot;}]}">',
  107. '</button>',
  108. ];
  109. // remove second image from cardview
  110. if (GM_config.get('enConfUser')) { addStyleString('.thread-footer-cell a img.avatar.vAlign--all-m.space--mr-1.thread-avatar {display: none;}'); }
  111.  
  112. // replace every avatar link without popup
  113. $('.thread-footer-cell a.user.linkPlain, .user.linkPlain.thread-user').each(function () {
  114. if (GM_config.get('enConfUser')) {
  115. var Iam = $(this);
  116. // get inner html and link to user profile
  117. var myHtml = $(this).html();
  118. var mysrc = $(this).attr('href');
  119. //GM_log(myHtml);
  120. // seperate user name from image and add class user
  121. var myAvatar1 = myHtml.replace(/<span.*/,'');
  122. var myAvatar2 = myHtml.replace(/.*<span class=".* space--mr-1">/,'<span class=" space--mr-1 user link-plain">');
  123.  
  124. // show small / medium sized Avatar
  125. myAvatar1 = myAvatar1.replace('avatar--type-s','avatar--type-m'); //in Dealz
  126. if (GM_config.get('enConfAvatar')) { myAvatar1 = myAvatar1.replace('thread-avatar','avatar--type-m'); }
  127. // compose popup
  128. var myPopup = enPopupUser[0] + Iam.attr('href') + enPopupUser[1] + myAvatar1 + enPopupUser[2] + '<a href="' + Iam.attr('href') + '">'+ myAvatar2 + '</a>';
  129. //GM_log(myPopup);
  130. Iam.replaceWith(myPopup);
  131. }
  132. });
  133. }
  134.  
  135.  
  136. // ============= GM_config functions =======================================
  137.  
  138. // define GM_config elements
  139. var fieldDefs = {
  140. // Part one: load external content --------
  141. 'enstyler': {
  142. 'section': ['additonal features for Enstyler', ''],
  143. 'label': 'Install / Update CSS...', // Appears on the button
  144. 'type': 'button', // Makes this setting a button input
  145. 'click': function() { // Function to call when button is clicked
  146. showUrl('https://userstyles.org/styles/128262#style-info'); }
  147. },
  148.  
  149. /* disable in standard conf, only FF mobile
  150. 'mobileRedirect': {
  151. 'label': 'Amazon mobile redirect...', // Appears on the button
  152. 'type': 'button', // Makes this setting a button input
  153. 'click': function() { // Function to call when button is clicked
  154. showUrl('https://greasyfork.org/de/scripts/19700'); }
  155. }, // */
  156.  
  157. 'dontCookies': {
  158. 'label': 'Mozilla no cookies...', // Appears on the button
  159. 'type': 'button', // Makes this setting a button input
  160. 'click': function() { // Function to call when button is clicked
  161. showUrl('https://addons.mozilla.org/de/addon/i-dont-care-about-cookies/'); }
  162. },
  163.  
  164. // part two: EnstylerJS internal configuration options ------
  165. 'hidden1': { // display next section, dont kow why ...
  166. 'section': ['Configuration', ''],
  167. 'type': 'hidden', // Makes this setting a hidden input
  168. 'value': 'Some hidden value' // Value stored
  169. },
  170.  
  171. // postion of enstyler "button"
  172. 'enConfBtn': {
  173. 'label': 'Show Enstyler in MainNav', // Appears next to field
  174. 'type': 'checkbox', // Makes this setting a checkbox input
  175. 'default': false // Default value if user doesn't change it
  176. },
  177. 'enConfBtnMinWidth': {
  178. 'label': 'only if width is bigger than ', // Appears next to field
  179. 'type': 'int', // Makes this setting a text input
  180. 'min': 600, // Optional lower range limit
  181. 'max': 1200, // Optional upper range limit
  182. 'size': 4, // Limit length of input (default is 25)
  183. 'default': 850 // Default value if user doesn't change it
  184. },
  185.  
  186. // ehanced USerInfo
  187. 'enConfUser': {
  188. 'label': 'Show Popuop Userinfo', // Appears next to field
  189. 'type': 'checkbox', // Makes this setting a checkbox input
  190. 'default': true // Default value if user doesn't change it
  191. },
  192. 'enConfAvatar': {
  193. 'label': 'bigger Avatar for Popuop', // Appears next to field
  194. 'type': 'checkbox', // Makes this setting a checkbox input
  195. 'default': true // Default value if user doesn't change it
  196. },
  197. //* disable for FF Mobile
  198. // enable filtering of external links
  199. 'enConfFilterLink': {
  200. 'label': 'Amazon mobile redirect', // Appears next to field
  201. 'type': 'checkbox', // Makes this setting a checkbox input
  202. 'default': true // Default value if user doesn't change it
  203. }, // */
  204.  
  205. // more Deal actions
  206. 'enConfMoreDeal': {
  207. 'label': 'additional Deal actions', // Appears next to field
  208. 'type': 'checkbox', // Makes this setting a checkbox input
  209. 'default': true // Default value if user doesn't change it
  210. },
  211.  
  212. // display copy message at end of section ...
  213. 'copy': {
  214. 'section': ['', '(c) Gnadelwartz - <a target="blank" href="https://www.mydealz.de/diskussion/enstyler2-style-your-mydealz-incl-pepper-sites-736219">Enstyler2 - Style your MyDealz</a>'],
  215. 'type': 'hidden', // Makes this setting a hidden input
  216. 'value': 'Some hidden value' // Value stored
  217. },
  218. };
  219.  
  220.  
  221. // display GM_copnfig as div, so we cam apply CSS!!
  222. var enGMOptChange = false;
  223. var enGMFrame = document.createElement('div');
  224. document.body.appendChild(enGMFrame);
  225.  
  226. GM_config.init(
  227. {
  228. id: 'GM_config',
  229. title: 'EnstylerJS - Settings',
  230. fields: fieldDefs,
  231. 'events': // Callback functions object
  232. {
  233. //'init': function() { alert('onInit()'); },
  234. //'open': function() { alert('onOpen()'); },
  235. //'reset': function() { enGMOptChange = true; },
  236. // relaod page on close after save
  237. 'save': function() { enGMOptChange = true; },
  238. 'close': function() { if (enGMOptChange) { location.reload(); enGMOptChange = false;} },
  239. },
  240. 'frame': enGMFrame // Element used for the panel
  241. }
  242. );
  243.  
  244.  
  245. // EnstylerJS Config Panel anzeigen
  246. function showEnstylerConfig() {
  247. GM_config.open();
  248. }
  249.  
  250. //=========== Support functions for actual use =======
  251.  
  252. // display website in external window
  253.  
  254. function showUrl(str) {
  255. var myDeco = "innerheight=800,innerwidth=600";
  256. var myName = "enstyler";
  257. // workaround for not working window.focus(): close an existing window first
  258. var myWindowShow = window.open('', myName, "width=100,height=100").close();
  259. myWindowShow = window.open(str, myName, myDeco);
  260. }
  261.  
  262. // add CSS in to document
  263. function addStyleString(str) {
  264. var node = document.createElement('style');
  265. node.innerHTML = str;
  266. document.body.appendChild(node);
  267. }
  268. // sleep time expects milliseconds, then execute code
  269. // NOTE: code runs in parallel (asnyc)!
  270. // Usage!
  271. // sleepAsync(500).then(() => {
  272. // Do something after the sleep!
  273. // });
  274.  
  275. function sleepAsync (time) {
  276. return new Promise((resolve) => setTimeout(resolve, time));
  277. }
  278.  
  279. // ============== START EnstyerJS =============
  280. EnstylerInit();
  281. EnstylerJS();
  282. EnstylerAvatarPopup();
  283.  
  284. // track DOM change Events, debounce: wait 500ms after mutiple events
  285. // then re-apply (somse) changes to dynamic loaded content,
  286. $('.fGrid-last2, .thread-list--type-card').bind("DOMSubtreeModified",$.debounce( 500, function(){
  287. //GM_log('DOMSubtreeModified detected!!!');
  288. EnstylerAvatarPopup();
  289.  
  290. }));
  291.