A userscript require jQuery, but on huya.com, console log many TypeError like this:
Uncaught (in promise) TypeError: Cannot read property 'msie' of undefined
Uncaught TypeError: checkRoom.live is not a function
Uncaught (in promise) TypeError: Cannot read property 'dialog' of undefined
Uncaught TypeError: Cannot read property 'setBitRateList' of null
Uncaught (in promise) TypeError: Cannot read property 'speak' of undefined
Uncaught (in promise) TypeError: Cannot read property 'toast' of undefined
Uncaught (in promise) TypeError: Cannot read property 'dialog' of undefined
......
Some Live room function not work, like 鍓у満妯″紡.
Remove @require jQuery , no errors log, 鍓у満妯″紡 work fine.
// @require https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js)huya.com (// @match *://www.huya.com/*)huya.com live room, and check ConsoleUserscript require jQuery, huya.com no errors log, all huya.com functions work fine.
huya.com some function not work. (like 鍓у満妯″紡)
This is most likely a problem of interaction between jQuery and huya.com, not Violentmonkey. Your script uses @grant none (or no @grant at all) so jQuery loads into the page context and does something that site doesn't like.
The workaround is to remove@grant none and specify some @grant value like @grant GM_info.
The version of jQuery on huya.com is 1.11.1, and your [email protected] loaded by ViolentMonkey overrided the original jQuery on the page.
To resolve this, jQuery provides a noConflict function to prevent this:
// ==UserScript==
// @name Huya test
// @namespace Violentmonkey Scripts
// @match https://www.huya.com/*
// @require https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js
// @grant none
// ==/UserScript==
console.log(jQuery.fn.jquery) // 3.4.1
const $=jQuery.noConflict(true)
console.log(jQuery.fn.jquery) // 1.11.1
// write your code here using $
Reply @tophf
It works. you are right.
By default, Violentmonkey use @grant none and Tampermonkey use @grant GM_info (with no @grant metadata block).
(Maybe it is better use @grant GM_info by default when no @grant metadata give.)
Reply @maple3142
Thanks for your answer. that works too.
And one more way, use @inject-into content force inject to content also work.
Thanks for your answer. 馃榿
Most helpful comment
The version of jQuery on
huya.comis1.11.1, and your [email protected] loaded by ViolentMonkey overrided the original jQuery on the page.To resolve this, jQuery provides a
noConflictfunction to prevent this: