试图在Chrome扩展中添加一个功能,以防止scmplayer在页面访问时自动播放

Trying to add a feature to a Chrome Extension that will prevent scmplayer from autoplaying on page visits

本文关键字:scmplayer 自动播放 访问 功能 一个 扩展 Chrome 添加      更新时间:2023-09-26

我正在使用JavaScript和jQuery编写Chrome扩展,除其他外,我希望它能防止scmplayer在用户访问的页面上自动播放。以下是在启用自动播放设置的情况下向页面添加scmplayer的典型示例:

<script type="text/javascript" src="http://scmplayer.net/script.js"
    data-config="{
        'skin':'skins/cyber/skin.css',
        'volume':50,
        'autoplay':true, <!-- The autoplay setting -->
        'shuffle':true,
        'repeat':1,
        'placement':'top',
        'showplaylist':false,
        'playlist':[{'title':'Bauhaus, %22Antonin Artaud%22','url':'http://www.youtube.com/watch?v=VJS9SKz7yog'},
        ...
        ">
</script>

我认为应该在脚本有机会运行之前编辑脚本的data-config属性,将'autoplay':true更改为'autoplay':false

如何使用jQuery或Javascript完成这一点?

那么,有没有一种特别好的方法可以在Chrome扩展中修改这个脚本的数据配置属性?或者,如果有更好的方法来防止scmplayer自动播放,我该怎么做?

您可以使用SCM API在像这样启动之前暂停/停止,

SCM.pause();

SCM在这里是一个全局范围变量。

但是chrome扩展的内容脚本是在一个孤立的环境中执行的,所以你不能直接从你的扩展content.js.访问这个变量

您必须向页面注入javascript代码。

这是我的扩展文件,

content.js

var actualCode = '(' + function() {
    function stopScm() {
        try {
            SCM.pause();
            alert("scm player was stopped");
        } catch(e) { 
            setTimeout(stopScm, 1000);
        }
    }
    stopScm();
} + ')();';
var script = document.createElement('script');
script.textContent = actualCode;
(document.head||document.documentElement).appendChild(script);
script.parentNode.removeChild(script);

manifest.json

{
  "name": "Stops SCM player",
  "version": "0.2",
  "manifest_version": 2,
  "description": "Example",
  "permissions": [
    "http://dl.dropboxusercontent.com/u/39457223/bountify/16/"
  ],
  "content_scripts": [ {
       "matches": ["http://dl.dropboxusercontent.com/u/39457223/bountify/16/*"],
       "js": ["content.js"],
       "run_at": "document_end"
    }
  ] 
}
  • 这是压缩的扩展名文件(.crx)
  • 这是运行扩展的演示页面

编辑:

在你分享了那个网址之后,我在那个网址上尝试了我的扩展。是的,它不起作用。

问题是扩展代码在加载scmframe之前运行。

scmframe是由SCM脚本附加的<iframe>的静态id。

所以等待加载iframe会更好,试试这个内容脚本,它也在处理你的URL。

content.js

$("#scmframe").load(function() {
    var actualCode = '(' + function() {
        function stopScm() {
            try {
                SCM.pause();
            } catch(e) {
                setTimeout(stopScm(), 1000);
            }
        }
        stopScm();
    } + ')();';
    var script = document.createElement('script');
    script.textContent = actualCode;
    (document.head||document.documentElement).appendChild(script);
    script.parentNode.removeChild(script);
});