Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

javascript - 在JavaScript控制台中包含jQuery(Include jQuery in the JavaScript Console)

Is there an easy way to include jQuery in the Chrome JavaScript console for sites that do not use it?

(对于不使用jQuery的网站,是否有简便的方法将jQuery包含在Chrome JavaScript控制台中?)

For example, on a website I would like to get the number of rows in a table.

(例如,在一个网站上,我想获取表中的行数。)

I know this is really easy with jQuery.

(我知道使用jQuery确实很容易。)

$('element').length;

The site does not use jQuery.

(该网站不使用jQuery。)

Can I add it in from the command line?

(我可以从命令行添加它吗?)

  ask by mrtsherman translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Run this in your browser's JavaScript console, then jQuery should be available...

(在浏览器的JavaScript控制台中运行它,然后jQuery应该可用...)

var jq = document.createElement('script');
jq.src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
// ... give time for script to load, then type (or see below for non wait option)
jQuery.noConflict();

NOTE: if the site has scripts that conflict with jQuery (other libs, etc.) you could still run into problems.

(注意:如果站点上的脚本与jQuery(其他库等)冲突,您仍然可能会遇到问题。)

Update: (更新:)

Making the best better, creating a Bookmark makes it really convenient, let's do it, and a little feedback is great too:

(让最好的东西变得更好,创建一个书签确实很方便,让我们去做,一点反馈也很棒:)

  1. Right click the Bookmarks Bar, and click Add Page

    (右键单击“书签栏”,然后单击“ 添加页面”)

  2. Name it as you like, eg Inject jQuery, and use the following line for URL:

    (随意命名,例如注入jQuery,并使用以下行作为URL:)

javascript:(function(e,s){e.src=s;e.onload=function(){jQuery.noConflict();console.log('jQuery injected')};document.head.appendChild(e);})(document.createElement('script'),'//code.jquery.com/jquery-latest.min.js')

(javascript:(function(e,s){e.src = s; e.onload = function(){jQuery.noConflict(); console.log('jQuery injection')}}; document.head.appendChild(e); })(document.createElement('script'),'// code.jquery.com/jquery-latest.min.js'))

Below is the formatted code:

(以下是格式化的代码:)

javascript: (function(e, s) {
    e.src = s;
    e.onload = function() {
        jQuery.noConflict();
        console.log('jQuery injected');
    };
    document.head.appendChild(e);
})(document.createElement('script'), '//code.jquery.com/jquery-latest.min.js')

Here the official jQuery CDN URL is used, feel free to use your own CDN/version.

(这里使用官方的jQuery CDN URL,请随意使用您自己的CDN /版本。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...