无论出于何种原因,Opera 的某些更新让我沮丧了大约一周,现在我试图找出 Opera 不再在我的生产力网站上运行的原因 - 例如 StreamTime 或 Elementor(在任何 WordPress 网站上)。
我已经将范围缩小到这些网站都使用 underscore 库(可能还有 lodash)。因为错误似乎总是与常用函数
_.findWhere
或_.pluck
有关。
我尝试用 Tamper Monkey 对其进行猴子修补,但无论 Opera 中是什么导致了这种情况,在我有机会用本机版本完全注入这些功能的垫片之前,它显然已经发生了。
这是迄今为止我在 Tamper Monkey 中得到的内容:
// ==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。
大家还有其他想法吗?
事实证明……是 Opera 附带的一个名为“Opera Wallet”的扩展引起了这个问题。如果您禁用它,它会立即解决所有问题!
因此,如果有人遇到任何问题 - 并且您已经像我一样感到沮丧很长一段时间了。这可能是您的解决方案!
下一步可能是向 Opera 发起请求,让他们修复破坏用户体验的扩展!