// ==UserScript== // @name leetcode 增加英文讨论区按钮 // @namespace ljybill // @version 1.0.0 // @description 学算法要博众家之长 - 基于 @Aloxaf 大佬作品修改 // @author ljybill // @match https://leetcode-cn.com/problems/* // @run-at document-idle // @grant window.onurlchange // @downloadURL none // ==/UserScript== /* jshint esversion: 6 */ (function () { 'use strict'; // https://stackoverflow.com/questions/22125865/wait-until-flag-true function waitFor(condition, callback) { if (!condition()) { window.setTimeout(waitFor.bind(null, condition, callback), 500); /* this checks the flag every 100 milliseconds*/ } else { callback(); } } function add_button() { // single if (document.querySelector('#btn__jump2eng')) { return } function htmlToElement(html) { let template = document.createElement('template'); html = html.trim(); // Never return a text node of whitespace as the result template.innerHTML = html; return template.content.firstChild; } const problem_name = location.href.match(/problems\/([^\/]+)/)[1]; const button = ``; document.querySelector('h4[class*="-Title"]').append(htmlToElement(button)); } waitFor(() => { let node = document.querySelector('div[class*="-Tools"]'); return (node && node.childElementCount > 0); }, add_button); // for spa if (window.onurlchange === null) { // feature is supported window.addEventListener('urlchange', function (evt) { console.log('evt'); if (evt.url) { waitFor(() => { let node = document.querySelector('div[class*="-Tools"]'); return (node && node.childElementCount > 0); }, add_button); } }); } })();