简体   繁体   English

在Tampermonkey中实现clippy.js

[英]Implement clippyjs in Tampermonkey

My aim is to write a Tampermonkey script that puts the Clippy assistant from Microsoft Office on any webpage I choose. 我的目的是编写一个Tampermonkey脚本,将Microsoft Office的Clippy助手放在我选择的任何网页上。 For this, I am using https://github.com/pi0/clippyjs . 为此,我正在使用https://github.com/pi0/clippyjs Here is what I've got so far: 这是到目前为止我得到的:

// ==UserScript==
// @name        Clippy!
// @include     https://stackoverflow.com/questions/*
// @require     https://unpkg.com/jquery@3.2.1/dist/jquery.js
// @require     https://unpkg.com/clippyjs@0.0.3/dist/clippy.js
// @grant       GM_addStyle
// ==/UserScript==

/*--- For this to work well, we must also add-in the jQuery-UI CSS.
    We add the CSS this way so that the embedded, relatively linked images load correctly.
    (Use //ajax... so that https or http is selected as appropriate to avoid "mixed content".)
*/
$("head").append (
    '<link '
  + 'href="//gitcdn.xyz/repo/pi0/clippyjs/master/assets/clippy.css" '
  + 'rel="stylesheet" type="text/css">'
);

clippy.load('Merlin', function(agent){
    // Do anything with the loaded agent
    agent.show();
});

This results in: 结果是:

Uncaught ReferenceError: clippy is not defined 未捕获的ReferenceError:未定义clippy

Is there any way to make Clippyjs work as a Tampermonkey script? 有什么方法可以使Clippyjs用作Tampermonkey脚本?

Clippyjs is not well designed for Tampermonkey scripts, it ajaxes in other JS files like agent.js that expect clippy to be defined in the page's window scope. Clippyjs并不是为Tampermonkey脚本设计的很好,它会在其他JS文件(例如agent.js中关闭,这些JS文件希望在页面的window范围内定义clippy

This means that you must inject everything into the page's scope, like so: 这意味着您必须所有内容注入页面的范围,如下所示:

// ==UserScript==
// @name        Clippy script demo injection
// @include     https://stackoverflow.com/questions/*
// @grant       none
// ==/UserScript==
/* global $, clippy */
/* eslint-disable no-multi-spaces */

//-- These particular target page(s) already have jQuery, otherwise addJS_Node() it too.

$("head").append ( `
    <link  href="//gitcdn.xyz/repo/pi0/clippyjs/master/assets/clippy.css"
    rel="stylesheet" type="text/css">
` );

addJS_Node (null, "https://unpkg.com/clippyjs@latest", null, startClippy);

//-- addJS_Node waits for clippyjs to load before running this... 
function startClippy () {
    clippy.load ('Merlin', function (agent) {
        agent.show ();
    } );
}

function addJS_Node (text, s_URL, funcToRun, runOnLoad) {
    var D                                   = document;
    var scriptNode                          = D.createElement ('script');
    if (runOnLoad) {
        scriptNode.addEventListener ("load", runOnLoad, false);
    }
    scriptNode.type                         = "text/javascript";
    if (text)       scriptNode.textContent  = text;
    if (s_URL)      scriptNode.src          = s_URL;
    if (funcToRun)  scriptNode.textContent  = '(' + funcToRun.toString() + ')()';

    var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
    targ.appendChild (scriptNode);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM