Reputation: 504
So I wrote a redirect script and unfortunately it redirects every single frame and I am new to java script and want to figure this simple extension out.
manifest.json
{
"name": "Flannel is #Winning ",
"version": "0.2",
"description": "Checks URL and redirects as required.",
"background": {
"page":"bg.html"
},
"manifest_version": 2,
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["content.js", "bg.js"]
}
],
"permissions": ["tabs","http://*/*", "https://*/*"]
}
bg.html
<html>
<script src="redirect.js"></script>
</html>
content.js
chrome.extension.sendRequest({redirect: newurl});
redirect.js
var url;
var newurl = "http://google.com/";
var newurl1 = "google.com/";
chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function (tabs) {
"use strict";
url = tabs[0].url;
});
function myFunction(url) {
"use strict";
if (url !== newurl || url !== newurl1) {
chrome.extension.onRequest.addListener(function (request, sender) {
chrome.tabs.update(sender.tab.id, {url: request.redirect});
});
}
}
chrome.tabs.getSelected(null, function (tab) {
"use strict";
myFunction(tab.url);
});
unfortunately it refreshes every frame and I have tried to create a function that checks if the current URL is that one, and maybe it us just my inexperience in JavaScript but I wanted to challenge myself by writing my first chrome extension and I'm stumped. Any pointers or pointing to any sort of documentation to point me in the write direction would be appreciated.
Update
I've tried this and additional variations of it as well, it straight up hasn't redirected at all with this code injected in the content.js script
chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function (tabs) {
"use strict";
url = tabs[0].url;
myFunction(url);
});
function myFunction(url){
if(url !== newurl){
chrome.extension.sendRequest({redirect: newurl});
}
}
Upvotes: 0
Views: 232
Reputation: 504
I figured it out by doing this in the content.js
if (window.location.href !== newurl) {
window.location.href = newurl;
}
Thank you for all of your help everyone!
Upvotes: 1