2016-03-24 3 views
0

私は、ユーザーが拡張ボタンをクリックしたときに、ユーザーを変更されたリンクにリダイレクトするクロム拡張を作成しようとしています。
私はmanifest.json、アイコンファイル、popup.html、popup.jsを作成しました
しかし、私のコードは機能しません。拡張機能経由でChromeをクリックしたときに新しいリンクにユーザーをリダイレクトする方法はありますか?

私は同様の質問に対する回答を読みましたが、それでも問題を解決できません。 リンク - 私がやろうとしています何の>How to modify current url location in chrome via extensions

Pseducode:現在のタブの
し1.GetのURL(WWWものと想定[ドット] xyz.com)
2.Modify URL([ドット] ABCXYZ COM)
3.updateリンクして、新しいリンクにリダイレクトする([ドット]コムをABCXYZに行く)

これは私がこれまでに書かれたものです....

// To get the url 

chrome.tabs.query({ active: true, lastFocusedWindow: true }, function (tabs) { 
    // Do something 
    var targ=tabs[0].url; 

}); 


var toBeOmitted="xyz"; 
    var toBeAddded="abc"; 
    var newTarg=targ.replace(toBeOmitted,toBeAddded); 

//to update to new url 
chrome.tabs.query({ active: true, lastFocusedWindow: true }, function (tab) { 
     chrome.tabs.update(tab.id, {url:newTarg}); 
}); 

私はそれをデバッグすることはできません。それはまだ動作しない場合は

答えて

0

変数の宣言に問題があります。
最終的なコードは次のようになります。 - >

//Declaring variables 
var targ,newTarg; 

chrome.tabs.query({ active: true, lastFocusedWindow: true }, function (tabs) { 
    // Do something 
    targ=tabs[0].url; 
//defining variables here 
    var toBeOmitted="xyz"; 
    var toBeAddded="abc"; 
//define newTarg without var(to make it global) or just declare outside 
//and define here 
    newTarg=targ.replace(toBeOmitted,toBeAddded); 
    return newTarg; 
}); 





    chrome.tabs.query({ active: true, lastFocusedWindow: true }, function (tabs) { 
     chrome.tabs.update({url:newTarg}); 
}); 
0

あなたは関数にchrome.tabs.query

を閉じるのを忘れて、nullであなたもchrome.tabs.query

例を使用する必要はありませんそのようにtab.idを交換してみてください。

chrome.tabs.update(null, {url:newTarg}); 
+0

それは(ヌル一部を)働いていません。 実際に私はここに閉会の部分を追加することを忘れましたが、私は自分のコードでそれを書いていました – Divyanshu

関連する問題