Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用Chrome扩展的ContentScript更改FlashVar?_Javascript_Jquery_Google Chrome Extension_Flash - Fatal编程技术网

Javascript 如何使用Chrome扩展的ContentScript更改FlashVar?

Javascript 如何使用Chrome扩展的ContentScript更改FlashVar?,javascript,jquery,google-chrome-extension,flash,Javascript,Jquery,Google Chrome Extension,Flash,我想创建一个chrome扩展来帮助我调试swf,并且我希望该扩展能够更改一些FlashVar,同时保持其他FlashVar不变 我无法想象这是一个新的或独特的问题,所以在我重新发明轮子之前,我想问这里是否有人有这样做的例子,或者如何做 我试过搜索,但我的谷歌搜索引擎似乎出了问题 我的用例如下。我有一个Google Chrome扩展,它有几个下拉菜单,可能有flash var值。我想更改下拉菜单中的值,然后使用这些新的FlashVar重新加载swf,但不更改不在下拉菜单中的FlashVar 我可以

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

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

我试过搜索,但我的谷歌搜索引擎似乎出了问题

我的用例如下。我有一个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中。js在决定您真正想要什么方面有点困难。 但是如果它操纵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));
资料来源:

a

b

c

d

e

f

假设一个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" /> 
background.js


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

共享您的代码,我会从那里获取。@Sudarshan谢谢,我已经输入了我认为可能是正确解决方案的代码,但我不确定。嗯,我提出的解决方案的问题是,我需要一个FlashVar列表,该列表将不会更改为在项目的htmltemplate中定义,而不是在contentScript.js中定义:这意味着我在chrome扩展中无法访问它。你的意思是想从内容脚本到chrome html进行通信吗?是添加到问题内容脚本的脚本吗?是的,上述代码在内容脚本中。我刚刚看到我无法从contentScript中读取localStorage,所以我现在还添加了一个background.js。看起来我们正在进行激烈的竞争:D@Sudarshan呵呵,不,我们不是;你总是可以得到我真正不在乎的要点。那么,看看我链接到的页面,再看看你的查询。它应该更像document.querySelector'param[name=FlashVars]'。value。注意,我最后得到了价值,因为我想他应该更清楚,他试图改变这些元素的价值。请随意将我的答案与选择器组合到您的答案中,我会很高兴地删除我的答案。@Sudarshan Oh刚刚在本页上注意到。。您的选择器对于嵌入和对象都是正确的。对于嵌入,您可以从FlashVars属性中获取值……我对该页面有点困惑。@Sudarshan,该网站在选择器上确认了这一点。他们需要两个,一个用于嵌入,一个用于对象。adobe链接非常有用,让我们等待Avik他真正需要的:……我认为这看起来很方便。但是我想更改flashvars中的一个特定项,而不是像宽度这样的基本参数。例如,我想将myVariable更改为goodbyeWorld,而不改变mySecondVariable的值
<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" /> 
{
"name":"Flash Vars",
"description":"This demonstrates Flash Vars",
"manifest_version":2,
"version":"1",
"permissions":["<all_urls>"],
"content_scripts": [
    {
      "matches": ["<all_urls>"],
      "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;
//Event Handler for catching messages from content script(s)
chrome.extension.onMessage.addListener(

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