如何使用Chrome扩展程序的ContentScript更改Flashvars?

时间:2012-12-03 13:58:00

标签: javascript jquery google-chrome-extension flash

我想创建一个chrome扩展来帮助我调试我的swf,我希望扩展能够更改一些flashvars,同时保持其他人不受影响。

我无法想象这是一个新的或独特的问题,所以在我重新发明轮子之前,我问这里是否有人有这样做的例子,或者怎么做。

我试过搜索,但我的googlefu似乎已经关闭了。

我的用例如下。我有一个Google Chrome扩展程序,它有一些下拉菜单,可能包含flash var值。我希望更改下拉列表中的值,然后使用这些新的flashvars重新加载swf,但不更改不在我的下拉菜单中的flashvars。

我可以使用下拉菜单中的值轻松地在页面上注入新的swf,但是我希望能够重新加载,而不是重新创建swf。

我已经尝试过使用Jquery拉出所有闪存变量:

var flashVars = $("param[name=flashvars]", gameSwfContainer).val();

但是,我很难更改或替换几个值,然后将它们注入对象。 (除非有更好的方法,否则正则表达式在这里可能会有所帮助?)

感谢您的帮助。

目前,我正在努力做到以下几点,但我不确定这是否是正确的方向。 ContentScript.js

//Setup
var swfVersionStr = "10.1.0";
var xiSwfUrlStr = "playerProductInstall.swf";
var params = {};
    params.quality = "high";
    params.bgcolor = "#000000";
    params.allowscriptaccess = "always";
    params.allowfullscreen = "true";
var attributes = {};
    attributes.id = "game_swf_con";
    attributes.name = "game_swf_con";
    attributes.class = "game_swf_con";
    attributes.align = "middle";
var InjectedFlashvars = {
    "run_locally": "true",
        //.. some other flash vars that I won't be changing with my extension
    "swf_object_name":"game_swf"
};
// Injection code called when correct page and div detected;    
var injectScriptText = function(text)
    { 
                loopThroughLocalStorage();
                swfobject.embedSWF(
                    swfName, "game_swf_con",  
                    "760", "625", 
                    swfVersionStr, xiSwfUrlStr, 
                    InjectedFlashvars, params, attributes);
       swfobject.createCSS("#game_swf_con", "display:block;text-align:left;");
    };
        function loopThroughLocalStorage()
    {
        for(key in localStorage){
            try{
            var optionArray = JSON.parse(localStorage[key]);
            var option = returnSelectedFlashVar(optionArray);
            var value = option.value ? option.value : "";
            InjectedFlashvars[key] = value;
            } catch(err){}

        }
    }

    function returnSelectedFlashVar(optionArray){
        for(var i= 0; i < optionArray.length;i++)
        {
            if(optionArray[i].selected == true)
            {
                return optionArray[i];
            }
        }
    }

总的来说,我目前有contentscript.js,background.js,options.js,options.html,popup.html和popup.js。到目前为止,上面的代码只存在于contentscript.js中

2 个答案:

答案 0 :(得分:0)

确定你真正想要的东西时有点麻烦。
但如果它操纵flashvar中的值可能会有所帮助......

queryToObject = function(queryString) {
    var jsonObj = {};
    var e, a = /\+/g,
        r = /([^&=]+)=?([^&]*)/g,
        d = function(s) {
            return decodeURIComponent(s.replace(a, " "));
        };

    while(e = r.exec(queryString)) {
        jsonObj[d(e[1])] = d(e[2]);
    }

    return jsonObj;
}

objectToQuery = function(object) {
    var keys = Object.keys(object),
        key;
    var value, query = '';
    for(var i = 0; i < keys.length; i++) {
        key = keys[i];
        value = object[key];
        query += (query.length > 0 ? '&' : '') + key + '=' + encodeURIComponent(value);
    }
    return query;
}

// this is what a flashvar value looks like according to this page...http://www.mediacollege.com/adobe/flash/actionscript/flashvars.html
var testQuery = 'myvar1=value1&myvar2=value2&myvar3=a+space';

var testObject = queryToObject(testQuery);

console.debug(testObject);

testQuery = objectToQuery(testObject);

console.debug(testQuery);

testObject.myvar0 = 'bleh';

delete testObject.myvar2;

console.debug(objectToQuery(testObject));

如果您在内容脚本中使用localStorage,那么请注意该存储将保留在页面的上下文中。
请尝试查看chrome.storage

编辑:回复评论
This looks correct, but how do I "reload" the object so that those new flashvars are the ones that are used by the swf?

我从未使用过swfObject(在5年内没有做任何闪存的东西),但看了一下,看起来你可以用新的flashVars重新运行swfobject.embedSWF,它将用新的替换旧对象一。如果您替换自己添加的那个,因为您可以提供所有详细信息,如果您将其他人放在那里,您可以克隆该对象,更改一些内容并将原始内容替换为克隆,那么这是好的。克隆不会克隆附加到元素的任何事件,但我不认为这是你的问题。这是一个例子(我无法测试,因为我找不到任何只打印flashvars的简单示例swf)....

var target = document.querySelector('#game_swf_con');
// Cloning the element will not clone any event listners attached to this element, but I dont think that's a prob for you
var clone = target.cloneNode(true);
var flashvarsAttribute = clone.querySelector('param[name=FlashVars]');
if (flashvarsAttribute) {
    var flashvars = flashvarsAttribute.value;
    flashvars = queryToObject(flashvars);
    // add a new value
    flashvars.newValue = "something";
    // update the clone with the new FlashVars
    flashvarsAttribute.value = objectToQuery(flashvars);
    // replace the original object with the clone
    target.parentNode.replaceChild(clone, target);
}​

答案 1 :(得分:0)

来源:

一) Content Scripts

B) Background Pages

c)Message Passing

d)tabs

e)Browser Action

f) API()'s

假设html页面包含以下代码

<embed src="uploadify.swf" quality="high" bgcolor="#ffffff" width="550"
height="400" name="myFlashMovie" FlashVars="myVariable=Hello%20World&mySecondVariable=Goodbye"
align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash"
pluginspage="http://www.adobe.com/go/getflash" /> 

我尝试获取嵌入对象的高度并更改它。

示例演示

的manifest.json

{
"name":"Flash Vars",
"description":"This demonstrates Flash Vars",
"manifest_version":2,
"version":"1",
"permissions":["<all_urls>"],
"content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["myscript.js"]
    }
       ]
}

<强> myscript.js

// Fetches Current Width
width = document.querySelector("embed[flashvars]:not([flashvars=''])").width;
console.log("Width is  " + width);

// Passing width to background.js
chrome.extension.sendMessage(width);

//Performing some trivial calcualtions
width = width + 10;

//Modifying parameters
document.querySelector("embed[flashvars]:not([flashvars=''])").width = width;

<强> background.js

//Event Handler for catching messages from content script(s)
chrome.extension.onMessage.addListener(

function (message, sender, sendResponse) {
    console.log("Width recieved is " + message);
});

如果您需要更多信息,请与我们联系。