1

我正在创建一个 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

请看一下 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;
}
4

0 回答 0