PlayIt-Monkey-Script

Configurable frontend for PlayIt Service

目前为 2016-09-18 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name PlayIt-Monkey-Script
  3. // @namespace https://github.com/rkaradas
  4. // @version 0.4
  5. // @description Configurable frontend for PlayIt Service
  6. // @author Recep Karadas
  7. // @include http*
  8. // @include https*
  9. // @connect *
  10. // @require https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js
  11. // @resource customCSS https://raw.githubusercontent.com/rkaradas/PlayIt-Monkey-Script/master/playit.css
  12. // @grant GM_addStyle
  13. // @grant GM_getResourceText
  14. // @grant GM_xmlhttpRequest
  15. // @grant GM_getValue
  16. // @grant GM_setValue
  17. // @grant GM_deleteValue
  18. // ==/UserScript==
  19.  
  20.  
  21. /**/
  22. var newCSS = GM_getResourceText ("customCSS");
  23. GM_addStyle (newCSS);
  24.  
  25.  
  26.  
  27. // runs at the end
  28. (function() {
  29. 'use strict';
  30. var connections = [];
  31. var selectedConnectionIdx = -1;
  32. var editingObject;
  33. var selectedElement;
  34. $("head").append (`<link href="//cdnjs.cloudflare.com/ajax/libs/octicons/3.5.0/octicons.min.css" rel="stylesheet" type="text/css">`);
  35. /*$("head").append (`<link href="//localhost/PlayIt_Tampermonkey/playit.css" rel="stylesheet" type="text/css">`);*/
  36. var container = `
  37. <div class='play-it-container'>
  38. <div class='octicon octicon-triangle-right play-it-play-btn'> </div>
  39. <div class='octicon octicon-settings play-it-settings-btn'> </div>
  40. <div class='play-it-settings' >
  41. <div class='play-it-settings-wrapper'>
  42. <div class='play-it-settings-header'>SETTINGS</div>
  43. <div class='play-it-settings-content'>
  44. <div class='play-it-settings-add-connection'>
  45. <div class='play-it-settings-add-connection-row'>
  46. <div><input type='text' class="play-it-settings-add-name" placeholder='Connection name, e.g. Kitchen' /></div>
  47. <div><input type='text' class="play-it-settings-add-ip" placeholder='Ip address, e.g. 192.168.1.49' /></div>
  48. <div><input type='text' class="play-it-settings-add-port" placeholder='Port (default:8181)' /></div>
  49. <div class='play-it-settings-add-connection-actions'><button class='play-it-settings-add octicon octicon-plus'></button></div>
  50. </div>
  51. </div>
  52. <div class='play-it-settings-connections'>
  53.  
  54. </div>
  55.  
  56. </div>
  57. <div class='play-it-settings-actions'>
  58. <button class='play-it-settings-save'>SAVE</button>
  59. <button class='play-it-settings-close'>CLOSE</button>
  60. <button class='play-it-settings-clear'>CLEAR ALL</button>
  61.  
  62. </div>
  63. </div>
  64. </div>
  65. </div>
  66. `;
  67. Init();
  68.  
  69. function Init()
  70. {
  71. editingObject = null;
  72. let containerEl = $(container);//.addClass("play-it-play-btn");
  73. containerEl.find(".play-it-settings-btn").click(function(){
  74. toggleSettings();
  75. });
  76. containerEl.find(".play-it-play-btn").click(function(){
  77. sendToKodi(location.href);
  78. });
  79. containerEl.find(".play-it-settings-close").click(function(){
  80. toggleSettings();
  81. });
  82. containerEl.find(".play-it-settings-save").click(function(){
  83. saveSettings(true);
  84. });
  85. containerEl.find(".play-it-settings-add").click(function(){
  86. addConnection();
  87. });
  88. containerEl.find(".play-it-settings-clear").click(function(){
  89. clearSettings();
  90. });
  91. $("body").prepend(containerEl);
  92. }
  93. // Functions
  94. function sendToKodi(videoUrl)
  95. {
  96. if(!selectedElement)
  97. loadSelectedElement();
  98. // if
  99. if(selectedElement){
  100. var url= "http://" + selectedElement.ip +":"+selectedElement.port + "/PlayIt";//'http://192.168.1.102:8181/PlayIt';
  101. console.log(url);
  102. var request={version:'1.1',
  103. method:'playHostedVideo',
  104. id:1,
  105. params:{videoLink:videoUrl}
  106. };
  107. GM_xmlhttpRequest({
  108. method: "POST",
  109. url: url,
  110. data: JSON.stringify(request),
  111. headers: {
  112. "Content-Type": "application/x-www-form-urlencoded"
  113. },
  114. onload: function(response) {
  115. console.log("onload");
  116. }
  117. });
  118. }
  119. else{
  120. alert("Please select a connection first");
  121. }
  122. }
  123.  
  124.  
  125. function toggleSettings()
  126. {
  127. let value = $('.play-it-settings').css('opacity') == 1 ? 0 : 1;
  128. let disp = (value==1?"block":"none");
  129. console.log("value: " + value + " - " + disp);
  130. if(disp=="block"){
  131. loadSettings();
  132. $('body').css("overflow","hidden");
  133. $('.play-it-settings').css("display",disp).animate({
  134. opacity: value
  135. });
  136. }else{
  137. $('.play-it-settings').animate({
  138. opacity: value
  139. },function(){ $(this).css("display",disp); $('body').css("overflow","visible"); } );
  140. }
  141. }
  142. function saveSettings(toggle)
  143. {
  144. GM_setValue("connections", JSON.stringify(connections));
  145. GM_setValue("selectedConnectionIdx", selectedConnectionIdx);
  146.  
  147. if(toggle)
  148. {
  149. toggleSettings();
  150. }
  151. }
  152. // this function is needed, if the user does not go to the settings
  153. function loadSelectedElement()
  154. {
  155. connections = JSON.parse(GM_getValue("connections","[]"));
  156. selectedConnectionIdx = GM_getValue("selectedConnectionIdx",-1);
  157. connections.forEach((connection, idx)=>{
  158. if(selectedConnectionIdx == idx)
  159. selectedElement = connection;
  160. });
  161. }
  162. function loadSettings()
  163. {
  164. editingObject = null;
  165. $(".play-it-settings-connections").html("");
  166. connections = JSON.parse(GM_getValue("connections","[]"));
  167. selectedConnectionIdx = GM_getValue("selectedConnectionIdx",-1);
  168. connections.forEach((connection, idx)=>{
  169. console.log("selectedConnectionIdx: "+ selectedConnectionIdx + "|idx: "+idx);
  170. if(selectedConnectionIdx == idx){
  171. selectedElement = connection;
  172. }
  173. addConnectionVisual(connection, (selectedConnectionIdx == idx) );
  174. });
  175. }
  176. function clearSettings()
  177. {
  178. $(".play-it-settings-connections").html("");
  179. connections = [];
  180. selectedConnectionIdx = -1;
  181. GM_deleteValue("connections");
  182. GM_deleteValue("selectedConnectionIdx");
  183. }
  184. function addConnection()
  185. {
  186. let ip = $(".play-it-settings-add-ip").val().trim();
  187. if(isIpV4(ip)){
  188. let name = $(".play-it-settings-add-name").val().trim();
  189. let port = parseInt($(".play-it-settings-add-port").val().trim(),10);
  190. name = name?name:"Kodi";
  191. port = isNaN(port) ? 8181 : port;
  192. let connection = {
  193. name: name,
  194. ip: ip,
  195. port: port
  196. };
  197. addConnectionVisual(connection, true);
  198. connections.push(connection);
  199.  
  200. $(".play-it-settings-add-name").val("");
  201. $(".play-it-settings-add-ip").val("");
  202. $(".play-it-settings-add-port").val("");
  203. saveSettings();
  204. }else
  205. {
  206. alert("Ip address must be a valid ipv4 address.");
  207. }
  208. }
  209. // UI functions
  210. function addConnectionVisual(connection, isSelected)
  211. {
  212. if(isSelected)
  213. {
  214. setSelectedConnection();
  215. }
  216. let connectionContainer = $(`
  217. <div class='play-it-settings-connection' >
  218. <div title='`+connection.name+ `'><span class='play-it-settings-connection-name'>`+connection.name+ `</span></div>
  219. <div title='`+connection.ip+ `'><span class='play-it-settings-connection-ip'>`+connection.ip+ `</span></div>
  220. <div title='`+connection.port+ `'><span class='play-it-settings-connection-port'>`+connection.port+ `</span></div>
  221. <div class="play-it-settings-connection-actions">
  222. <button title='Remove Connection' class='play-it-settings-connection-remove octicon octicon-trashcan'></button>
  223. <button title='Edit Connection' class='play-it-settings-connection-edit octicon octicon-pencil'></button>
  224. <button title='Save' class='play-it-settings-connection-save octicon octicon-check' style='display:none' ></button>
  225. <button title='Select' class='play-it-settings-connection-select octicon octicon-verified' `+(isSelected?`disabled`:``)+` ></button>
  226. </div>
  227. </div>
  228. `);
  229. $(".play-it-settings-connections").append(connectionContainer);
  230. connectionContainer.find(".play-it-settings-connection-remove").click(function(){
  231. connectionRemoveClick($(this));
  232. });
  233. connectionContainer.find(".play-it-settings-connection-edit").click(function(){
  234. connectionEditClick($(this));
  235. });
  236. connectionContainer.find(".play-it-settings-connection-save").click(function(){
  237. connectionSaveClick($(this));
  238. });
  239. connectionContainer.find(".play-it-settings-connection-select").click(function(){
  240. connectionSelectClick($(this));
  241. });
  242. if(isSelected)
  243. {
  244. selectedElement = connections[selectedConnectionIdx];
  245. saveSettings();
  246. }
  247. }
  248. function clearSelectedConnection()
  249. {
  250. selectedElement = null;
  251. selectedConnectionIdx = -1;
  252. $('.play-it-settings-connection-select').prop("disabled",false);
  253. }
  254. function setSelectedConnection(el)
  255. {
  256. $('.play-it-settings-connection-select').prop("disabled",false);
  257. if(el)
  258. el.prop("disabled", true);
  259. }
  260. /**
  261. BEGIN Connection editing click functions
  262. **/
  263. function connectionRemoveClick(el)
  264. {
  265. if(isInEditMode())
  266. return;
  267. let parent = el.closest(".play-it-settings-connection");
  268. let idx = $('.play-it-settings-connection').index(parent);
  269. if(selectedConnectionIdx == idx)
  270. {
  271. clearSelectedConnection();
  272. }
  273. console.log(idx);
  274. el.closest(".play-it-settings-connection").remove();
  275. connections.splice(idx,1);
  276. printConnections();
  277. }
  278. function connectionSelectClick(el)
  279. {
  280. if(isInEditMode())
  281. return;
  282. let parent = el.closest(".play-it-settings-connection");
  283. let idx = $('.play-it-settings-connection').index(parent);
  284. console.log(idx);
  285. selectedConnectionIdx = idx;
  286.  
  287. setSelectedConnection(el);
  288. selectedElement = connections[idx];
  289. saveSettings();
  290. }
  291. function connectionSaveClick(el)
  292. {
  293. let parent = el.closest(".play-it-settings-connection");
  294. let ip = parent.find(".play-it-settings-connection-ip").val().trim();
  295. if(isIpV4(ip)){
  296. let name = parent.find(".play-it-settings-connection-name").val().trim();
  297. let port = parseInt(parent.find(".play-it-settings-connection-port").val().trim(),10);
  298. name = name?name:"Kodi";
  299. port = isNaN(port) ? 8181 : port;
  300.  
  301. let connection = {
  302. name: name,
  303. ip: ip,
  304. port: port
  305. };
  306. if(editingObject.name != connection.name ||editingObject.ip != connection.ip ||editingObject.port != connection.port)
  307. {
  308. connections[editingObject.idx].name = connection.name;
  309. connections[editingObject.idx].ip = connection.ip;
  310. connections[editingObject.idx].port = connection.port;
  311. }
  312. replaceInputWithSpan(parent, "play-it-settings-connection-name");
  313. replaceInputWithSpan(parent, "play-it-settings-connection-ip");
  314. replaceInputWithSpan(parent, "play-it-settings-connection-port");
  315. editingObject = null;
  316. saveSettings();
  317.  
  318. parent.find(".play-it-settings-connection-save").hide();
  319. $(".play-it-settings-connection-edit").show();
  320. }else
  321. {
  322. alert("something went wrong please fix the issues");
  323. }
  324. }
  325. function connectionEditClick(el)
  326. {
  327. if(isInEditMode())
  328. return;
  329.  
  330. let parent = el.closest(".play-it-settings-connection");
  331. let idx = $('.play-it-settings-connection').index(parent);
  332. console.log(idx);
  333. editingObject = {idx:idx,
  334. name: parent.find(".play-it-settings-connection-name").html(),
  335. ip: parent.find(".play-it-settings-connection-ip").html(),
  336. port: parent.find(".play-it-settings-connection-port").html()
  337. };
  338. replaceSpanWithInput(parent, "play-it-settings-connection-name");
  339. replaceSpanWithInput(parent, "play-it-settings-connection-ip");
  340. replaceSpanWithInput(parent, "play-it-settings-connection-port");
  341.  
  342. parent.find(".play-it-settings-connection-save").show();
  343. $(".play-it-settings-connection-edit").hide();
  344. }
  345. function isInEditMode()
  346. {
  347. if(editingObject){
  348. alert("Already in editing mode.\nPlease save changes, before you start editing the next element. ");
  349. return true;
  350. }
  351. return false;
  352. }
  353. /**
  354. END Connection editing click functions
  355. **/
  356. function replaceSpanWithInput(parent, spanClass)
  357. {
  358. var spanEl = parent.find("." + spanClass);
  359. let inp = $(`<input type='text' value='`+spanEl.html()+`' />`).addClass(spanClass);
  360. spanEl.replaceWith(inp);
  361. }
  362. function replaceInputWithSpan(parent, inputClass)
  363. {
  364. var inputEl = parent.find("." + inputClass);
  365. let span = $(`<span>`+inputEl.val()+`</span>`).addClass(inputClass);
  366. inputEl.replaceWith(span);
  367. }
  368. function printConnections()
  369. {
  370. connections.forEach((connection)=>{
  371. console.log(connection.name);
  372. });
  373. }
  374. function isIpV4(ip) {
  375. var x = ip.split("."), x1, x2, x3, x4;
  376.  
  377. if (x.length == 4) {
  378. x1 = parseInt(x[0], 10);
  379. x2 = parseInt(x[1], 10);
  380. x3 = parseInt(x[2], 10);
  381. x4 = parseInt(x[3], 10);
  382.  
  383. if (isNaN(x1) || isNaN(x2) || isNaN(x3) || isNaN(x4)) {
  384. return false;
  385. }
  386.  
  387. if ((x1 >= 0 && x1 <= 255) && (x2 >= 0 && x2 <= 255) && (x3 >= 0 && x3 <= 255) && (x4 >= 0 && x4 <= 255)) {
  388. return true;
  389. }
  390. }
  391. return false;
  392. }
  393. })();
  394.