标签: userscripts

使我的用户脚本等待加载其他脚本

[ 编辑:我用一个简单的例子来代替原来的,令人困惑的问题来证明这个问题.]

背景

我正在尝试编写一个将在Chrome中运行的用户脚本.这个脚本需要调用一个AlertMe()在usercript之外的JavaScript函数- 这个函数是页面的一部分,包含在服务器端动态生成的变量,所以不能在我的函数中重写这个函数. userscript.

页面上的脚本(访问页面):

<script type="text/javascript">
    function AlertMe()
    {
        alert("Function AlertMe was called!");
        // then do stuff with strings that were dynamically generated
        // on the server so that I can't easily rewrite this into the userscript
    }
</script>
Run Code Online (Sandbox Code Playgroud)

我的用户脚本(在Chrome中安装):

function tryAlert()
{
    if (typeof AlertMe == "undefined") {
        console.log('AlertMe is undefined.');
        window.setTimeout(tryAlert, 100);
    }
    else {
        AlertMe();
    }
}

tryAlert();
Run Code Online (Sandbox Code Playgroud)

问题

当我试图简单地调用该功能时,Chrome的控制台让我知道AlertMe is not defined.认为这是因为我的用户脚本在加载所有其他脚本之前运行,我曾经setTimeout …

javascript google-chrome userscripts

7
推荐指数
2
解决办法
9869
查看次数

IE有一个简单的,javascript驱动的扩展开发方式,如Chrome,FF,Safari和Opera吗?

在放弃我的广泛谷歌搜索之前,我想我会做出最后的努力并在这里问...

在Chrome,Safari,Firefox和Opera中 - 使用javascript(以及每个浏览器的一些原生javascript函数)编写浏览器扩展非常容易......

我似乎无法找到IE的这种等价物.我见过Greasemonkey替代品 - 其中大部分仅适用于非常简单的脚本.

IE9或10是否支持使用javascript而不是C等扩展开发?

我有一个非常大的扩展工作在FF,Chrome,Safari和Opera,我很乐意支持IE,如果它不意味着完全重写为不同的语言,但我似乎找不到任何类型的IE相当于"内容脚本"或"用户javascript"类型扩展名...

javascript internet-explorer userscripts browser-extension

7
推荐指数
1
解决办法
3430
查看次数

使用userscript捕获页面xmlhttp请求

我有一个用户脚本(用于chrome和FF),它为页面添加了重要功能,但最近因为开发人员向页面添加了一些AJAX而被破坏.我想修改脚本来监听页面xmlhttp请求,这样我就可以根据responseText页面接收的JSON格式动态更新我添加的内容.

搜索已经发现了许多应该工作的功能,并且在控制台中运行时可以正常工作.但是,它们不会从用户脚本的上下文中执行任何操作.

(function(open) {

    XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {

        this.addEventListener("readystatechange", function() {
            console.log(this.readyState);
        }, false);

        open.call(this, method, url, async, user, pass);
    };

})(XMLHttpRequest.prototype.open);
Run Code Online (Sandbox Code Playgroud)

来自:如何从Greasemonkey脚本拦截XMLHttpRequests?

这工作完全在控制台中,我可以改变this.readyState,以this.responseText和它的伟大工程(尽管在剧本我需要它来打开JSON数据转换成一个对象,然后让我userscript内操纵它.不只是写到控制台) .但是,如果我将其粘贴到用户脚本中没有任何反应.页面上的xmlhttp请求似乎没有被usercript中的事件处理程序检测到.

执行请求的页面使用jquery $ .get()函数,如果它可能与它有任何关系.虽然我不这么认为.

我无法想象没有办法,似乎在AJAX页面上运行的任何用户脚本都需要这种能力.

ajax xmlhttprequest userscripts

7
推荐指数
1
解决办法
4718
查看次数

没有提示,将图像保存到硬盘?

我用twitter.有些人的推文包含照片,我想保存它们.

我检查了ifttt,其中twitter不是触发器.因此,ifttt无法帮助我做到这一点.

一个想法可能是使用JavaScript.我使用Firefox并安装了Greasemonkey.我可以写一个在twitter网站上运行的Greasemonkey脚本(JavaScript).点击"转发"链接或我的脚本添加的其他按钮后,我的脚本会检查推文的内容,找到照片的网址,然后将其保存到我的磁盘中.

一个问题是如何保存图像.我搜索了互联网.一些使用win.document.execCommand("SaveAs"),它将显示"另存为"窗口.现在窗口显示,为什么不直接单击图像并选择手动保存?所以我不喜欢这种方法.

有什么建议?

javascript greasemonkey filesystem-access userscripts tampermonkey

7
推荐指数
2
解决办法
5873
查看次数

window.onload在Firefox + Greasemonkey脚本中有效但在Chrome用户脚本中无效吗?

有一个页面http://example.com/1.php像往常一样包含javascript文件:

<script type="text/javascript" src="/util.js?1354729400"></script>
Run Code Online (Sandbox Code Playgroud)

这个文件包含名为exampleFunction的函数,我需要在我的用户脚本中使用它.我还有一个用户脚本:

// ==UserScript==
// @name          SomeName
// @namespace     http://example.com/userscripts
// @description   Greets the world
// @include       http://example.com/*
// ==/UserScript==
window.onload = function () {
        console.log(exampleFunction);
      alert("LOADED!");
}
Run Code Online (Sandbox Code Playgroud)

在Firefox中完美运行并在Chrome中返回错误:

Uncaught ReferenceError: exampleFunction is not defined 
Run Code Online (Sandbox Code Playgroud)

我如何使其工作?

javascript greasemonkey google-chrome cross-browser userscripts

7
推荐指数
1
解决办法
9338
查看次数

Tampermonkey脚本不更新

我在让脚本自动更新时遇到问题.如果我强制检查更新,那么它就可以很好地获取新版本.但是,我无法在新的更新可用时自动通知我.

我的脚本具有以下元数据:

// ==UserScript==
// @name         PortalScript
// @namespace    PortalScript
// @version      2.1.0.9
// @description  Contains customizations to be applied to the portal
// @author       Tristan Lee
// @match        https://test-support.portal.dev
// @grant        none
// @downloadURL https://rawgit.com/tristanlee85/supportportal/master/supportportal.js
// @updateURL https://rawgit.com/tristanlee85/supportportal/master/supportportal.js
// ==/UserScript==
Run Code Online (Sandbox Code Playgroud)

Tampermonkey设置为每小时检查更新.但是,存储库中的版本可能是2.1.0.10,但自版本更改后数小时后,我从未收到有关更新可用的通知.

我也不认为这@updateURL是必要的,因为@downloadURL指定,但我添加它作为检查,因为只是@downloadURL没有工作.

userscripts tampermonkey

7
推荐指数
0
解决办法
900
查看次数

如何禁用具有特定id模式的div块?

我想写一个greasemonkey脚本来禁用某个页面上的div.在页面的任何给定负载上,我不知道div在div中的位置,但我知道它总是被调用<div id = "alertPanel"> ....</div>

我该怎么做才能禁用这个div?

我的初步想法是:

var myDivs= document.getElementsByTagName('div');

for (i=0; i<myDivs.length; i++)
{

  if (myDivs[i].<get id property somehow> = "alertPanel")
     myDivs[i].style.visibility = 'hidden';
 }
Run Code Online (Sandbox Code Playgroud)

但是你可以告诉我,我一直在访问id属性以进行相等检查.

很明显,我正在使用文本编辑器来编写这个 - 我猜测标准的javascript编辑器在输入后会给出一个自动完成列表 myDivs[i].

html javascript dom userscripts

6
推荐指数
1
解决办法
1万
查看次数

Javascript:keydown 事件未触发

我正在使用以下代码编写用户脚本:

(function() {
    'use strict';
    window.addEventListener("keydown", arrows, false);
    function arrows(e) {
        debugger;
        switch(e.keycode) {
             case 37: alert("Left"); break;
             case 39: alert("Right"); break;
        }
    }
})();
Run Code Online (Sandbox Code Playgroud)

最终,左侧和右侧案例将分别导航到系列中的上一篇和下一篇文章,例如:

window.location = String(parseInt(window.location.href.match(/\d+$/))-1);
Run Code Online (Sandbox Code Playgroud)

但是,按箭头键不会引起警报。脚本已明确加载,Chrome 开发人员菜单显示该arrows()函数已注册为 的事件侦听器window.keydown,但该函数从未触发。我添加debugger;了该arrows()功能,但是当我按箭头键时,调试器没有显示。

javascript google-chrome userscripts

6
推荐指数
2
解决办法
6488
查看次数

如何@match 用户脚本中的 URL 哈希?

我有很多打开的网站标签 - 我想将其限制为只有一个 URL。

问题是 URL 包含#在其中,我似乎无法 @match 它。

该网址如下所示: https://xxx.xxxxxx.xxx/#/xxxxxx

我试过:

// @match https://xxx.xxxxxx.xxx/#/xxxxxx - doesnt work

// @match https://xxx.xxxxxx.xxx/* - works - but works on all tabs

// @match https://xxx.xxxxxx.xxx/*/xxxxxx -doesnt work;/

// @match https://xxx.xxxxxx.xxx/\#/xxxxxx -doesnt work;/
Run Code Online (Sandbox Code Playgroud)

* 和后面的任何组合都不起作用。:(

url greasemonkey match userscripts tampermonkey

6
推荐指数
1
解决办法
3960
查看次数

如何在用户脚本中使用 Service Worker

我想在某人的网站中缓存特定请求,然后我发现 service worker 是一个不错的选择。但是我找不到任何通过 tampermonkey 注入 service worker 的方法。那么有没有什么技巧可以做到这一点?

userscripts tampermonkey service-worker

6
推荐指数
1
解决办法
239
查看次数