通过内容脚本删除代码

时间:2012-12-21 02:05:15

标签: google-chrome-extension xss content-script

我正在创建一个Chrome扩展程序,可以在显示Flash内容的标记上方绘制图像。我已经在我的“contentscript.js”文件中编写了内容脚本,该文件只是通过脚本标记将其他脚本文件“backgroundscript.js”插入到页面的正文中。

该代码适用于少数拥有Flash视频的网站,并在Flash Player窗口正上方显示图像,但在其他网站上显示的图像不显示。同样在少数情况下它会出现几分之一秒然后消失。

以下是我的清单文件:

{
  "name": "My First Chrome Extension",
  "version": "1.0.0.0",
  "description": "Tag videos from websites with a single click!",
  "icons": {   "16":"application_16.png","48":"application_48.png","128":"application_128.png" },
  "homepage_url": "http://www.myurl.com",
  "page_action":{
                "default_icon":{
                "19":"application_19.png",
                "38":"application_38.png"
            },
            "default_title":"VDownloader browser plugin"

        },
  "content_scripts": [ { "js": [ "contentscript.js" ], "matches": [ "http://*/*" ], "run_at" : "document_end", "all_frames": true} ],
  "plugins": [ { "path": "myplugin.dll", "public": true } ],
  "manifest_version":2,
  "minimum_chrome_version":"17",
  "offline_enabled":true,
  "permissions":["tabs","http://*/*", "https://*/*","contextMenus"],
  "web_accessible_resources":["application.png","backgroundscript.js"]
}

我也通过插入“调试器”来检查;进入我的脚本并通过控制台监控代码执行;脚本至少添加一次到页面正文中。即使在没有显示图像按钮的情况下也是如此。

这是在没有显示我的按钮的更高级网站上实现的某种anit-XSS控件吗?

我真的很感谢你的帮助,因为我过去几周试图克服这个问题而没有成功:(

修改

请参阅下面的内容脚本:

var s = document.createElement('script');
s.src = chrome.extension.getURL("backgroundscript.js");
s.onload = function() {
    this.parentNode.removeChild(this);
};
(document.head||document.documentElement).appendChild(s);

目标网站:

  1. 不显示按钮:http://www.metacafe.com
  2. 暂时显示按钮:http://www.dailymotion.com
  3. 正确显示按钮:http://www.myreviewalarm.com
  4. 请查看backgroundscript.js内容:

    var vdDelayTimer;
    var vdUpdateInterval;
    var playerButtons;
    
    embedPlugin();
    
    function embedPlugin() {
        var embed = document.createElement('embed');
        embed.setAttribute('type', 'application/x-myplugin');
        embed.setAttribute('id', 'myplugin');
        embed.setAttribute('style',   'width:1px;height:1px;position:absolute;left:0px;top:0px;');
        if(document.body!=null)
    {
        document.body.appendChild(embed);
    }
        testPlugin();
    }
    
    function testPlugin() {
        var plugin = document.getElementById('vdPlugin');
        if (plugin != null) {
            playerButtons = [];
            vdDelayTimer = setTimeout("appendButtons()", 2000);
        }
        else {
            window.alert('Plugin does not exist!');
        }
    }
    
    function updateButtons() {
    
        for (i = 0; i < playerButtons.length; i += 2) {
            updateButtonLocation(playerButtons[i], playerButtons[i + 1]);
        }
    }
    
    function updateButtonLocation(player, button) {
    
        var playerX = getX(player);
        var playerY = getY(player);
        var buttonHeight = 38;
        var buttonWidth = 196;
        var buttonX = playerX + player.offsetWidth - buttonWidth - 12;
        var buttonY = playerY - buttonHeight + 1;
        button.setAttribute('style', 'background-image: url(http://myurl.com/img/browser-download-button.png); width:' + buttonWidth + 'px;height:' + buttonHeight + 'px;position:absolute;left:' + buttonX + 'px;top:' + buttonY + 'px; cursor:pointer; cursor:hand;');
    }
    
    function appendButtons() {
        debugger;
        var objectTags = document.getElementsByTagName('object');
        for (var i = 0; i < objectTags.length; i++) {
            var objectTag = objectTags[i];
            if ((objectTag.hasAttribute('classid') && objectTag.getAttribute('classid') == 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000')
    || (objectTag.hasAttribute('type') && objectTag.getAttribute('type') == 'application/x-shockwave-flash')) {
                if (objectTag.offsetWidth >= 320 && objectTag.offsetHeight >= 240)
                    createButton(objectTag, getX(objectTag), getY(objectTag),  objectTag.offsetWidth);
            }
        }
        var embedTags = document.getElementsByTagName('embed');
        for (var i = 0; i < embedTags.length; i++) {
            try {
                var embedTag = embedTags[i];
                if (embedTag.hasAttribute('type') && embedTag.getAttribute('type') == 'application/x-shockwave-flash') {
                    if (embedTag.offsetWidth >= 320 && embedTag.offsetHeight >= 240)
                        createButton(embedTag, getX(embedTag), getY(embedTag), embedTag.offsetWidth);
                }
            }
            catch (err) { }
        }
        var videoTags = document.getElementsByTagName('video');
        for (var i = 0; i < videoTags.length; i++) {
            try {
                var videoTag = videoTags[i];
                if (videoTag.hasAttribute('src')) {
                    if (videoTag.offsetWidth >= 320 && videoTag.offsetHeight >= 240)
                        createButton(videoTag, getX(videoTag), getY(videoTag), videoTag.offsetWidth);
                }
            }
            catch (err) { }
        }
        vdUpdateInterval = setInterval("updateButtons();", 500);
    }
    
    function createButton(videoTag, playerX, playerY, playerWidth) {
        debugger;
        var buttonHeight = 38;
        var buttonWidth = 196;
        var buttonX = playerX + playerWidth - buttonWidth - 12;
        var buttonY = playerY - buttonHeight + 1;
        var vdPlugin = document.getElementById('vdPlugin');
        var strLocation = window.location.toString();
       // if (isSupported(strLocation)) {
            var downloadButton = document.createElement('img');
            downloadButton.setAttribute('title', 'Tag this video');
            downloadButton.setAttribute('src', 'http://myurl.com/img/browser-download-button.png');
            downloadButton.setAttribute('style', 'background-image: url(http://myurl.com/img/browser-download-button.png); width:' + buttonWidth + 'px;height:' + buttonHeight + 'px;position:absolute;left:' + buttonX + 'px;top:' + buttonY + 'px; cursor:pointer;cursor:hand;z-index:2147483647;');
            downloadButton.setAttribute('onclick', 'javascript:document.getElementById(\'vdPlugin\').downloadNow(window.location.href);');
            downloadButton.setAttribute('oncontextmenu', 'javascript:return false;');
            document.body.appendChild(downloadButton);
            playerButtons.push(videoTag, downloadButton);
       // }
    }
    
    function getY(oElement) {
        var iReturnValue = 0;
        while (oElement != null) {
            iReturnValue += oElement.offsetTop;
            oElement = oElement.offsetParent;
        }
        return iReturnValue;
    }
    
    function getX(oElement) {
        var iReturnValue = 0;
        while (oElement != null) {
            iReturnValue += oElement.offsetLeft;
            oElement = oElement.offsetParent;
        }
        return iReturnValue;
    }
    
    
    function isSupported(url) {
        var regLen = supportedSites.length;
        var regEx;
        var res = false;
        try {
            for (var i = 0; i < regLen && res == false; i++) {
                regEx = new RegExp(supportedSites[i], "i");
                res = regEx.test(url);
            }
        }
        catch (ex) {
        }
        return res;
    }
    

0 个答案:

没有答案