Violentmonkey: Problems require jQuery in violentmonkey on huya.com

Created on 27 Aug 2019  路  3Comments  路  Source: violentmonkey/violentmonkey

What is the problem?

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.

How to reproduce it?

  1. empty script require jQuery (// @require https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js)
  2. empty script match huya.com (// @match *://www.huya.com/*)
  3. open huya.com live room, and check Console

What is the expected result?

Userscript require jQuery, huya.com no errors log, all huya.com functions work fine.

What is the actual result?

huya.com some function not work. (like 鍓у満妯″紡)

Environment

  • Browser: Chrome Win / Chromium Linux
  • Browser version: 76.0.3809.100
  • Violentmonkey version: 2.11.2
  • OS: Windows 10 1903 / Arch Linux
external

Most helpful comment

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 $

All 3 comments

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. 馃榿

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Riajyuu picture Riajyuu  路  5Comments

ale82to picture ale82to  路  6Comments

Luke-Baker picture Luke-Baker  路  4Comments

rayman89 picture rayman89  路  4Comments

cyberrufus picture cyberrufus  路  6Comments