首页 > 解决方案 > 使用 chrome 扩展的重定向循环问题

问题描述

我已经构建了一个 chrome 扩展,它将某些链接转换为附属链接,然后重定向到该附属链接。

更好理解的步骤:
1. 有人访问www.amazon.in,扩展程序通过chrome.webRequest.onBeforeRequest.addListener
2. 转换为另一个 url,如www.linkly.com?q1=param&q2=param
3. 该链接再次将请求发送回原始 url,即www.amazon.in

在这里,它一次又一次地进行重定向循环。我试过使用types: ['main_frame']但没有得到解决方案。

清单.json:

{
	"manifest_version":2,
  	"name": "ExtName",
  	"description": "Ext Description",
  	"version":"1.0",
  	"browser_action":{
		"default_popup":"index.html"
	},
	"icons": {
	    "16": "ic_launcher.png",
	    "48": "ic_launcher.png",
		"128": "ic_launcher.png"
	},
	"background": {
    	"scripts": ["app/background.js","lib/jquery.min.js"],
    	"persistent": true
	},
	"content_scripts":[
		{
			"matches": ["http://*/*", "https://*/*", "file:///*/*"],
			"css":["css/styles.css"],
			"js":["lib/jquery.min.js"]
		}
	],
	"web_accessible_resources": ["css/styles.css"],
  	"permissions": ["tabs","activeTab", "http://*/*", "https://*/*", "file:///*/*", "storage","webRequest","webRequestBlocking"]
}

背景.js:

chrome.storage.sync.get(["prefs"],function(data){
	var obj=JSON.parse(data.prefs);
	uid=obj._userID;				
	updateLinks(uid);
});
function updateLinks(uid){
	var redirUrl;
	chrome.webRequest.onBeforeRequest.addListener(
		function(details) {
			if(typeof uid!=="undefined"){
				redirUrl = "https://xredir.com/?pub_id=xxxx&subid="+uid+"&source=xxxx&url="+encodeURI(details.url);
			}else{
				redirUrl = "https://xredir.com/?pub_id=xxxx&source=xxxx&url="+encodeURI(details.url);
			}
			if(details.type=="main_frame"){
					return {redirectUrl: redirUrl};
			}
		},
	    {urls: ["*://www.flipkart.com/*", "*://www.amazon.com/*", "*://www.amazon.in/*","*://www.jabong.com/*"],types: ['main_frame']},
	    ["blocking"]
	);		
}

请帮我解决这个问题。我提前感谢您的时间和支持。

标签: javascripthtmlgoogle-chrome-extensionwebbrowser-control

解决方案


推荐阅读