Reputation: 11
The current version of my chrome extension extracts a set of strings from the current tab, opens a new tabs with a specific url and proceeds to insert the strings and submit the form using jQuery. The problem that I have is if I open a separate tab afterwards and go to the URL with the form, it proceeds to insert the data again and submit the form. It will do this continuously, so I can never use the website outside of automating it with the chrome extension.
This is the function in the background.js that sends the strings.
chrome.browserAction.onClicked.addListener(function (tab) { //Fired when User Clicks ICON
if(zip){
zipCode = zip;
zip = null;
}else{
zipCode = prompt('Zip code');
}
if(/^\d{5}(-\d{4})?$/.test(zipCode)){
var newURL = "https://www.medicare.gov/find-a-plan/questions/home.aspx?search";
chrome.tabs.create({ url: newURL });
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
id = "2136760320";
if (changeInfo.status == 'complete') {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
chrome.tabs.sendMessage(tabId, {zip: zipCode, pass: pwd, drugID: id}, function(response) {});
});
}
});
}
});
This is the content Script that matches the URL and is inserted along with jquery. I would need this to only run if the chrome extension was the one that opened the page.
var pwd = "";
var id = "";
var onMessageHandler = function(message, sender, sendResponse){
pwd = message.pass;
id = message.drugID;
$("#saveWorkID").val(id);
var date = pwd.split("/");
var month = date[0];
if (month.length < 2) month = "0" + month;
var day = date[1];
var year = date[2];
$("#month").val(month);
$("#day").val(day);
$("#year").val(year);
$("#lnkFind span").trigger("click");
}
chrome.runtime.onMessage.addListener(onMessageHandler);
There are two other content scripts running for the pages prior to the above page, but not sure if they are necessary.
This is the manigest.json
{
"manifest_version": 2,
"name": "PDP Lookup",
"description": "Lookup the PDP druglist of a current user",
"version": "1.0",
"icons": {"48": "icon.png",
"128": "icon128.png" },
"browser_action":
{
"default_icon": "icon.png"
},
"background":{
"scripts": [
"jquery.js",
"background.js"
]
},
"content_scripts": [
{
"matches": [
"https://www.medicare.gov/find-a-plan/questions/home.aspx?search"
],
"js": [
"jquery.js",
"stage1.js"
]
},
{
"matches": [
"https://www.medicare.gov/find-a-plan/questions/enter-your-information.aspx"
],
"js": [
"jquery.js",
"stage2.js"
]
},
{
"matches": [
"https://XXX.XXX.XXX/XX*"
],
"js": [
"jquery.js",
"search.js"
]
},
{
"matches": [
"https://plancompare.medicare.gov/pfdn/PlanFinder/DrugSearch"
],
"js": [
"jquery.js",
"stage3.js"
]
}
],
"permissions": [
"tabs",
"https://*/*",
"http://*/*"
]
}
Any help on how to prevent the content script from trying to read the sent message would be appreciated.
Upvotes: 1
Views: 401
Reputation: 5118
Instead of injecting the content script through the manifest file, try injecting it in the callback function of chrome.tabs.create
, something like
chrome.tabs.create({ url: newURL }, function(tabcreated){
chrome.tabs.executeScript(tabcreated,{file:"jquery.js"},function(){
chrome.tabs.executeScript(tabcreated,{file:"stage1.js"});
});
})
This way it will be injected only after your extension creates the tab, and not when a tab with that URL is opened in any other way.
Upvotes: 1