Greasy Fork is available in English.

Get Youtube current Seconds

Hold ctrl and click(or just click) on the title to get Youtube's current seconds in the clipboard.

  1. // ==UserScript==
  2. // @name Get Youtube current Seconds
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.2.0
  5. // @description Hold ctrl and click(or just click) on the title to get Youtube's current seconds in the clipboard.
  6. // @grant GM_setValue
  7. // @grant GM_getValue
  8. // @grant GM_registerMenuCommand
  9. // @grant GM_unregisterMenuCommand
  10. // @author fengxxc
  11. // @match https://www.youtube.com/*
  12. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  13. // @license MIT
  14.  
  15. // ==/UserScript==
  16.  
  17. (function() {
  18. 'use strict';
  19.  
  20. const defaultConfig = {
  21. requireCtrl: false
  22. };
  23.  
  24. function getConfig() {
  25. return GM_getValue('config', defaultConfig);
  26. }
  27.  
  28. function saveConfig(config) {
  29. GM_setValue('config', config);
  30. }
  31.  
  32. function toggleCtrlClickRequirement() {
  33. const config = getConfig();
  34. config.requireCtrl = !config.requireCtrl;
  35. saveConfig(config);
  36. registerMenuCommand();
  37. console.log(config.requireCtrl ? 'Ctrl + click enabled' : 'Ctrl + click disabled');
  38. }
  39.  
  40. let menuCommandId;
  41.  
  42. function registerMenuCommand() {
  43. if (menuCommandId) {
  44. GM_unregisterMenuCommand(menuCommandId);
  45. }
  46. const config = getConfig();
  47. const menuText = config.requireCtrl
  48. ? 'Toggle Ctrl+click (enabled)'
  49. : 'Toggle Ctrl+click (disabled)';
  50. menuCommandId = GM_registerMenuCommand(menuText, toggleCtrlClickRequirement);
  51. }
  52.  
  53. function getSeconds(timestamp) {
  54. const sp = timestamp.split(":").map(Number);
  55. let seconds = 0;
  56. for (let i = 0; i < sp.length; i++) {
  57. seconds += sp[i] * (60 ** (sp.length - i - 1));
  58. }
  59. return seconds;
  60. }
  61.  
  62. let btn = null;
  63. let intervalId = setInterval(() => {
  64. if (btn != null) {
  65. clearInterval(intervalId);
  66. btn.onclick = () => {
  67. const config = getConfig();
  68. if ( (!config.requireCtrl && !event.ctrlKey) // click
  69. || (config.requireCtrl && event.ctrlKey)){ // ctrl + click
  70. const currenttime = document.querySelector('.ytp-time-current').innerText;
  71. const currentSeconds = getSeconds(currenttime);
  72. const url = `${window.location.href}&t=${currentSeconds}s`;
  73. console.log(`Now Play: ${currenttime}, seconds: ${currentSeconds}, url: ${url}`);
  74. navigator.clipboard.writeText(url).then(() => {
  75. console.log('url copied to clipboard');
  76. }).catch(err => {
  77. console.error('Failed to copy text: ', err);
  78. });
  79. }
  80. }
  81. }
  82. btn = document.querySelector('#above-the-fold > #title');
  83. }, 60)
  84.  
  85. registerMenuCommand();
  86.  
  87. })();