Javascript-форум (https://javascript.ru/forum/)
-   Javascript под браузер (https://javascript.ru/forum/css-html/)
-   -   jQuery и расширение Chrome (https://javascript.ru/forum/css-html/70076-jquery-i-rasshirenie-chrome.html)

Sayler91 08.08.2017 19:09

jQuery и расширение Chrome
 
Добрый день. Не могу подключить jQuery к расширению. Пробовал разные способы, но так и не получилось. Просьба помочь на этом примере.


manifest.json
{
    "background" : {
		"scripts" : ["background.js"]
	},
	"commands" : {
		"startCode" : {
			"description" : "Расширение",
			"suggested_key" : {
				"chromeos" : "Ctrl+Shift+Z",
				"linux" : "Ctrl+Shift+Z",
				"mac" : "Command+Shift+Z",
				"windows" : "Ctrl+Shift+Z"
			}
		}
	},
	"browser_action" : {
		"default_icon" : "48x48.png",
		"default_title" : "Расширение",
		"default_popup" : "popup.html"
	},
	"description" : "Горячая клавиша Ctrl+Shift+Z.",
	"icons" : {
		"128" : "128x128.png",
		"16" : "16x16.png",
		"48" : "48x48.png"
	},
	"manifest_version" : 2,
	"name" : "Расширение",
	"permissions" : ["tabs", "<all_urls>"],
	"version" : "1.0.0"
}

background.js
chrome.commands.onCommand.addListener(function (command) {
	switch (command) {
	case "startCode":
		chrome.tabs.executeScript(null, {file : "script.js"})
	}
});

popup.html
<html> 
    <script src="popup.js"></script> 
</html>


script.js
var m = $('th:contains("Имя")').next();

alert (m.text());


Пример странички.
<body>
    <table class="form-table from-table_size_sm">
<tbody>
<tr>
	<th>Имя</th><td><div class="model-value">Иван</div></td>
	<td class="form-table__gutter">&nbsp;</td><th>Имя (лат.)</th><td><div class="model-value">IVAN</div></td>
</tr>
</tbody>
</table>

Nexus 09.08.2017 09:26

Скрипты подключаются в манифесте.
Чтобы подключить к страничке скрипт «scripts.js», необходимо в манифест добавить "раздел" «content_scripts».

https://developer.chrome.com/extensions/manifest
https://developer.chrome.com/extensions/content_scripts


Часовой пояс GMT +3, время: 09:37.