OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

I made a browser extension that detected emails for scams. But, for some reason, it is only working on gmail and not any other websites?

  • Thread starter Thread starter Varun Nukala
  • Start date Start date
V

Varun Nukala

Guest
I made it in the beginning for gmail, but now I want it to work for other websites like google slides or notes. But, the extension isn't picking up for scams on any other website besides email? I tried everything, and asked ChatGPT, but nothing worked.

**The extension works when the user highlights the text and clicks the extension in the toolbar, but only on gmail and not any other urls.

I tried managing the extension, but it is set to every URL. Even the JSON is set to "ALL URLS". Yet, even after saving and reloading, the extension only works on GMAIL.

Code:
//manifest.json
{
  "manifest_version": 2,
  "version": "1.0",
  "name": "Email Scam Detector",
  "permissions": ["activeTab", "contextMenus"],
  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["main.js"]
    }
  ],
  "browser_action": {
    "default_popup": "popup.html"
  }
}

Code:
//main.js
document.addEventListener('mouseup', function() {
  let selectedText = window.getSelection().toString().trim();
  if (selectedText.length > 0) {
    chrome.runtime.sendMessage({text: selectedText}, function(response) {
      console.log(response);
    });
  }
});

Code:
//background.js
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    const scamKeywords = [
      "urgent", "immediately", "bank account", "login", "password", "verify", 
      "click here", "suspended", "lottery", "prize", "winner", "inheritance", 
      "funds", "Nigerian prince", "transfer", "limited time", "offer", "risk-free", "validate", 
      "Customer", "Friend", "Sir", "Ma'am", "Trusted customer"

    ];
    
    let keywordCount = scamKeywords.reduce((count, keyword) => {
      if (request.text.toLowerCase().includes(keyword)) {
        count += 1;
      }
      return count;
    }, 0);
  
    let isScam = keywordCount >= 2;
  
    sendResponse({isScam: isScam});
  });

Code:
//popup.html
<!DOCTYPE html>
<html>
<head>
  <title>Email Scam Detector</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      width: 250px;
      padding: 10px;
      background-color: #f9f9f9;
    }
    h1 {
      font-size: 16px;
      margin-bottom: 10px;
      text-align: center;
    }
    .result {
      margin-top: 20px;
      padding: 10px;
      border-radius: 5px;
      text-align: center;
    }
    .safe {
      background-color: #d4edda;
      color: #155724;
    }
    .scam {
      background-color: #f8d7da;
      color: #721c24;
    }
  </style>
</head>
<body>
  <h1>Email Scam Detector</h1>
  <div id="result" class="result">Highlight text to analyze</div>
  <script src="popup.js"></script>
</body>
</html>

Code:
//popup.js
chrome.tabs.executeScript({
  code: "window.getSelection().toString();"
}, function(selection) {
  let selectedText = selection[0].trim();
  if (selectedText.length > 0) {
    chrome.runtime.sendMessage({text: selectedText}, function(response) {
      let resultDiv = document.getElementById('result');
      if (response.isScam) {
        resultDiv.innerHTML = "⚠️ This text might be a scam!";
        resultDiv.className = 'result scam';
      } else {
        resultDiv.innerHTML = "✅ This text seems safe.";
        resultDiv.className = 'result safe';
      }
    });
  }
});

<p>I made it in the beginning for gmail, but now I want it to work for other websites like google slides or notes. But, the extension isn't picking up for scams on any other website besides email? I tried everything, and asked ChatGPT, but nothing worked.</p>
<p>**The extension works when the user highlights the text and clicks the extension in the toolbar, but only on gmail and not any other urls.</p>
<p>I tried managing the extension, but it is set to every URL. Even the JSON is set to "ALL URLS". Yet, even after saving and reloading, the extension only works on GMAIL.</p>
<pre><code>//manifest.json
{
"manifest_version": 2,
"version": "1.0",
"name": "Email Scam Detector",
"permissions": ["activeTab", "contextMenus"],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["main.js"]
}
],
"browser_action": {
"default_popup": "popup.html"
}
}

</code></pre>
<pre><code>//main.js
document.addEventListener('mouseup', function() {
let selectedText = window.getSelection().toString().trim();
if (selectedText.length > 0) {
chrome.runtime.sendMessage({text: selectedText}, function(response) {
console.log(response);
});
}
});

</code></pre>
<pre><code>//background.js
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
const scamKeywords = [
"urgent", "immediately", "bank account", "login", "password", "verify",
"click here", "suspended", "lottery", "prize", "winner", "inheritance",
"funds", "Nigerian prince", "transfer", "limited time", "offer", "risk-free", "validate",
"Customer", "Friend", "Sir", "Ma'am", "Trusted customer"

];

let keywordCount = scamKeywords.reduce((count, keyword) => {
if (request.text.toLowerCase().includes(keyword)) {
count += 1;
}
return count;
}, 0);

let isScam = keywordCount >= 2;

sendResponse({isScam: isScam});
});
</code></pre>
<pre><code>
//popup.html
<!DOCTYPE html>
<html>
<head>
<title>Email Scam Detector</title>
<style>
body {
font-family: Arial, sans-serif;
width: 250px;
padding: 10px;
background-color: #f9f9f9;
}
h1 {
font-size: 16px;
margin-bottom: 10px;
text-align: center;
}
.result {
margin-top: 20px;
padding: 10px;
border-radius: 5px;
text-align: center;
}
.safe {
background-color: #d4edda;
color: #155724;
}
.scam {
background-color: #f8d7da;
color: #721c24;
}
</style>
</head>
<body>
<h1>Email Scam Detector</h1>
<div id="result" class="result">Highlight text to analyze</div>
<script src="popup.js"></script>
</body>
</html>

</code></pre>
<pre><code>//popup.js
chrome.tabs.executeScript({
code: "window.getSelection().toString();"
}, function(selection) {
let selectedText = selection[0].trim();
if (selectedText.length > 0) {
chrome.runtime.sendMessage({text: selectedText}, function(response) {
let resultDiv = document.getElementById('result');
if (response.isScam) {
resultDiv.innerHTML = "⚠️ This text might be a scam!";
resultDiv.className = 'result scam';
} else {
resultDiv.innerHTML = "✅ This text seems safe.";
resultDiv.className = 'result safe';
}
});
}
});

</code></pre>
 
Top