使用lodash/underscore的网站在Opera中失效的排查与解决求助
Opera更新后Underscore常用函数失效的解决思路求助
不知为何,Opera某次更新后,我近一周都被这个问题折腾——StreamTime、用Elementor搭建的WordPress站点这类生产力网站,在Opera里完全没法正常运行。
已经定位到问题根源:这些网站都用到了Underscore库(可能也混了Lodash),报错全集中在_.findWhere、_.pluck这类常用函数上。
我试过用TamperMonkey写猴子补丁,但Opera里导致问题的因素,明显比我注入原生垫片的速度更快,补丁根本赶不上覆盖。以下是我当前的TamperMonkey脚本:
// ==UserScript== // @name Fix issues with Opera Compatibility. // @namespace https://www.hazrpg.co.uk // @version 0.1 // @description Trying to fix an opera issue that causes _.pluck and _.findWhere to not work. // @author hazrpg // @match https://*.app.streamtime.net/* // @icon https://www.google.com/s2/favicons?sz=64&domain=streamtime.net // @grant none // @run-at document-start // ==/UserScript== (function() { 'use strict'; console.log('Fixing opera issues...'); let underscoreSrc_v1_13_6 = ` if (typeof _ !== 'undefined' || typeof _ === 'function') { console.log('Looks like its already there jim, but lets shim shim shim!'); // The '_' variable is already defined // You can now define your custom _.findWhere function _.findWhere = function (array, criteria) { //console.log('shimmed _.findWhere'); return array.find(item => Object.keys(criteria).every(key => item[key] === criteria[key])) }; _.pluck = function (collection, propertyName) { //console.log('shimmed _.pluck'); return collection.map(item => item[propertyName]); }; } else { console.log('Looks like we need to shimmy for Opera!'); // The '_' variable is not defined // You can create your own namespace and define _.findWhere in it var _ = { findWhere: function (array, criteria) { //console.log('shimmed _.findWhere'); return array.find(item => Object.keys(criteria).every(key => item[key] === criteria[key])) }, pluck: function (collection, propertyName) { //console.log('shimmed _.pluck'); return collection.map(item => item[propertyName]); }, }; } `; // Create a new script element for Underscore library var underscoreScript = document.createElement('script'); //underscoreScript.src = 'https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.13.6/underscore-min.js'; // Adjust the URL as needed //underscoreScript.async = true; underscoreScript.textContent = underscoreSrc_v1_13_6; // Listen for the DOMContentLoaded event to inject the Underscore library document.addEventListener('DOMContentLoaded', function() { document.head.appendChild(underscoreScript); }); })();
我还试过用setTimeout设置50毫秒延迟来强制注入垫片,但覆盖它的操作速度更快,而且我不想把延迟设得低于50毫秒。
求各位提供其他解决思路?
内容的提问来源于stack exchange,提问作者hazrpg
相关产品推荐
相关产品推荐

