【问题标题】:How do I change the flashvars using the ContentScript of a Chrome Extension?如何使用 Chrome 扩展的 ContentScript 更改 flashvars?
【发布时间】:2012-12-04 19:07:49
【问题描述】:

我想创建一个 chrome 扩展来帮助我调试我的 swf,并且我希望该扩展能够更改一些 flashvar,同时保持其他的不变。

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

我已尝试搜索,但我的 googlefu 似乎已关闭。

我的用例如下。我有一个 Google Chrome 扩展程序,其中有一些带有可能的 flash var 值的下拉菜单。我想更改下拉菜单中的值,然后使用这些新的 flashvar 重新加载 swf,但不更改不在我的下拉菜单中的 flashvar。

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

我曾尝试使用 Jquery 来拉取所有的 flash 变量,如下所示:

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中

【问题讨论】:

  • 分享您的代码,从那里获取。
  • @Sudarshan 谢谢我已经输入了我认为可能是正确解决方案的代码,但我不确定。
  • 嗯,我提出的解决方案的问题是我需要在项目的 htmltemplate 中而不是在 contentScript.js 中定义不会更改的 flashvars 列表 :( 这意味着我无法在 chrome 扩展程序中访问它。
  • 您的意思是要从内容脚本与 chrome html 进行通信吗?您添加的脚本是问题内容脚本吗?
  • 是的,以上代码在内容脚本中。我刚刚看到我无法从 contentScript 中读取 localStorage,所以我现在还要添加一个 background.js

标签: javascript jquery google-chrome-extension flash


【解决方案1】:

在决定你真正想要什么时遇到了一些麻烦。
但是,如果它操纵 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

编辑:回复 cmets
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 年内没有做过任何 flash 的东西),但看了一下,看起来你可以用新的 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);
}​

【讨论】:

  • 看起来我们正在激烈竞争:D
  • @Sudarshan 呵呵,不,不是;)你总是可以得到我真的不在乎的点。因此,请查看我链接到的那个页面并重新查看您的查询。它应该更像document.querySelector('param[name=FlashVars]').value。注意我最后得到了价值,因为我认为(希望他更清楚)他试图改变这些元素的价值。随意将我的答案与该选择器结合到您的答案中,我很乐意删除我的答案。
  • @Sudarshan 哦刚刚在此页面上注意到.. permadi.com/tutorial/flashVars/index.html .. 您的选择器看起来适合嵌入,而我的选择器适合对象。对于嵌入,您可以从 FlashVars 属性中获取值.....我从那个页面有点困惑。
  • @Sudarshan 和这个网站helpx.adobe.com/flash/kb/pass-variables-swfs-flashvars.html 在选择器上确认了它。他们需要两个,一个用于嵌入(根据该页面现在已经过时),一个用于对象。
  • adobe 链接真的很有帮助,让我们等待 Avik 真正需要的东西 :) ..
【解决方案2】:

来源:

a) 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" /> 

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

示例演示

ma​​nifest.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);
});

如果您需要更多信息,请告诉我。

【讨论】:

  • 我认为这看起来很方便。但我想更改 flashvars 中的特定项目,而不是像宽度这样的基本参数。例如,我想将“myVariable”更改为“goodbyeWorld”而不改变“mySecondVariable”的值
猜你喜欢
  • 1970-01-01
  • 2014-06-05
  • 1970-01-01
  • 2013-05-18
  • 1970-01-01
  • 2016-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多