How to include JavaScript file or library in Chrome console?

后端 未结 10 1895
广开言路
广开言路 2020-11-27 08:58

Is there a simpler (native perhaps?) way to include an external script file in the Google Chrome browser?

Currently I’m doing it like this:

document.         


        
10条回答
  •  我在风中等你
    2020-11-27 09:46

    Install tampermonkey and add the following UserScript with one (or more) @match with specific page url (or a match of all pages: https://*) e.g.:

    // ==UserScript==
    // @name         inject-rx
    // @namespace    http://tampermonkey.net/
    // @version      0.1
    // @description  Inject rx library on the page
    // @author       Me
    // @match        https://www.some-website.com/*
    // @require      https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.5.4/rxjs.umd.min.js
    // @grant        none
    // ==/UserScript==
    
    (function() {
        'use strict';
         window.injectedRx = rxjs;
         //Or even:  window.rxjs = rxjs;
    
    })();
    

    Whenever you need the library on the console, or on a snippet enable the specific UserScript and refresh.

    This solution prevents namespace pollution. You can use custom namespaces to avoid accidental overwrite of existing global variables on the page.

提交回复
热议问题