1

Flashコンテンツを示すタグの上に画像を描画するChrome拡張機能を作成しています。「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"]
}

また、「デバッガ」を挿入して確認しました。スクリプトに挿入し、コンソールを介してコードの実行を監視します。スクリプトは少なくとも1回はページ本文に追加されます。画像ボタンが表示されていない場合でも。

これは、ボタンが表示されないより高度なサイトに実装されている、ある種の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