更改window.location.href后继续运行

时间:2015-11-14 21:43:51

标签: javascript jquery google-chrome google-chrome-extension

我想制作一个执行某些脚本的chrome扩展程序。 我的问题是当我更改文档位置时页面加载并且不继续执行脚本。一切都停止,不要再次更改页面位置。谢谢你的帮助。

清单

                 {
                "manifest_version": 2,
                "name": "My APP",  
                "description": "My APP",  
                "version": "1.0",    
                "permissions": ["tabs", "<all_urls>"], 

                 "background": {
                 "scripts": ["jquery-1.11.3.min.js"],

                "browser_action": { 
                    "default_popup": "popup.html"  
                } 
            }

popup.html

            <!doctype html>  
            <html>  
                <head><title>My App</title></head>  
            <body>  
                <button id="click">click</button>  
                <script src="popup.js"></script> 
            </body>
            </html>  

popup.js

                function myScript() {
                    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
                    chrome.tabs.executeScript(tabs[0].id, {file: "content_script.js"});
                });
            }

            document.getElementById('click').addEventListener('click', myScript);

content_script.js

            var Arr = ["xx1","xx2","xx3"];
            var Counter=0;

            function run() {
                window.location.href="http://www.cadpay.ir/page/request/" + Arr[++Counter] + "/";
                $(window).ready(function()
                {
                            document.body.getElementsByTagName('#send').click();    
                            alert('ok');                        
                });
                if( Arr.length > Counter)
                    run();
                else
                    alert('Finish');
            }

            run();

我更改了一些代码,但没有用。

popup.js

                function myScript() {

                var activeTabId;

                chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {


                    chrome.tabs.executeScript(tab.id, {code: 'window.location.href="https://www.cadpay.ir/req/22"'}, function (){

                    activeTabId = tab.id;
                });


                chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
                    if( tabId === activeTabId )
                    {
                        activeTabId = null; 
                        chrome.tabs.executeScript(tabId, {file:"content_script.js"});
                    }
                });

            }

            document.getElementById('click').addEventListener('click', myScript);

以及如何控制循环的问题?

0 个答案:

没有答案