Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome Extension not loading/ code environment not selectable in Inspector/ Dev tools

I am developing a chrome extension to highlight the Facebook Notifications using jQuery. I can get it to load, when Facebook loads first time, but after a while it stops working. In manifest I have tried persistent set to true and false, no difference. I have tried using background.js.

I have been fiddling with chrome.tabs.onActivated and .onHighlighted and can get an alert to show up, but my code or the jQuery $ isn't seen.

In dev tools, my extension isn't listed in the environments I can choose to use here Chrome Dev Tools environment chooser

My Code: manifest.json

{
    "name": "Facebook Your notification highlight",
    "version": "0.3.2",
    "description": "Highlights notifications with 'your' in the notification!",
	"background": {
      "scripts": ["jquery.min.js","background.js"],
      "persistent": false
		},
	 "browser_action": {
          "default_icon": "icon48.png"
		  },
	"icons": {
		"48": "icon48.png",
		"128": "icon128.png" },
	"permissions": [ "tabs", "webNavigation", "https://www.facebook.com/" , "https://facebook.com/", "http://www.facebook.com/"],
	"manifest_version": 2
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>

New manifest.js

{
    "name": "Facebook Your notification highlight",
    "version": "0.3.3",
    "description": "Highlights notifications with 'your' in the notification!", 
	"background": {
      "scripts": ["jquery.min.js","background.js"]
		},
	
	 "browser_action": {
          "default_icon": "icon48.png"
		  },
	"content_scripts": [
        {
          "matches": ["https://*.facebook.com/"],
          "js": ["jquery.min.js","inject.js"]
        }
		],
	"web_accessible_resources": [
		"jquery.min.js",
		"inject.js"
		],
	"icons": {
		"48": "icon48.png",
		"128": "icon128.png" },
	"permissions": [ "activeTab", "tabs", "webNavigation", "https://www.facebook.com/" , "https://facebook.com/", "http://www.facebook.com/"],
	"manifest_version": 2
  }
  

new inject.js

//add listeners when injected


$(document).ready(function() {
    AddFbListeners();
});



function AddFbListeners() {
    $('div#js_11.uiScrollableAreaWrap').scroll( function() {
        HighlightFb(); 
    });

    $('#fbNotificationsJewel').click ( function () {
        setTimeout( HighlightFb(), 250);
    });
}

function HighlightFb() {
    //alert("highlight");
    //highlight you
     $("._33c:contains('you')").find('*').css("background-color", "#CCCC00"); //greeny yellow
    //highlight your
     $("._33c:contains('your')").find('*').css("background-color", "66CC00"); //darker yellow
    //highlight reacted or liked
     $("._33c:contains('liked')").find('*').css("background-color", "#b2b300");  //mustard
     $("._33c:contains('reacted')").find('*').css("background-color", "#b2b300"); //mustard
     //mentioned
     $("._33c:contains('mentioned')").find('*').css("background-color", "#62b300"); //green
    }
like image 495
Craig Lambie Avatar asked Nov 16 '18 22:11

Craig Lambie


People also ask

Why inspect is not working in Chrome?

Quit Chrome then delete /Applications/Google Chrome. app. Delete these folders: ~/Library/Application Support/Google/Chrome ~/Library/Caches/Google/Chrome/Default. Re-install Google Chrome.

How do I enable dev tools in Chrome?

To open the developer console in Google Chrome, open the Chrome Menu in the upper-right-hand corner of the browser window and select More Tools > Developer Tools. You can also use Option + ⌘ + J (on macOS), or Shift + CTRL + J (on Windows/Linux).

Where we can see your code in dev tool?

Access Developer ToolsRight-click a page and select "Inspect Element". This displays the HTML code for the element you clicked. Select View > Developer > Developer tools.


1 Answers

You can inspect your background page by going to chrome://extensions and clicking the background page or background page (inactive) link for your extension. (Chrome runs your background scripts on a minimal implicitly generated background page background.html.)

Your general logic is flawed though, extension pages (and scripts - typically a background page, options page, pop-up etc.) run in a separate environment and have no access to the DOM of regular pages. You need a content script for your HighlightFb() and AddFbListeners() to let them access the DOM of a regular (in your case Facebook) page. (BTW, content scripts will be listed in the environment selector in Chrome DevTools of a regular page.)

The differences between the context of extension pages vs. content scripts vs. regular pages have been addressed many times on StackOverflow, but it might be best to read up on extension architecture in general first:

Extension architecture (Chrome)

Anatomy of a web extension (Firefox, but the architecture is essentially identical)

like image 154
Petr Srníček Avatar answered Nov 09 '22 03:11

Petr Srníček