diff --git a/background.js b/background.js index 8bd0679..95aed10 100644 --- a/background.js +++ b/background.js @@ -1,9 +1,7 @@ -$.get(chrome.extension.getURL('/injected.js'), - function(data) { - var script = document.createElement("script"); - script.setAttribute("type", "text/javascript"); - script.innerHTML = data; - document.getElementsByTagName("head")[0].appendChild(script); - document.getElementsByTagName("body")[0].setAttribute("onLoad", "injected_main();"); - } -); +chrome.browserAction.setBadgeText({text: "on"}); +//При нажатии иконки расширения вызывается данная функция +chrome.browserAction.onClicked.addListener(function() { + chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { + chrome.tabs.sendMessage(tabs[0].id, {command: "show"}); + }); +}); diff --git a/inject.js b/inject.js new file mode 100644 index 0000000..be77c60 --- /dev/null +++ b/inject.js @@ -0,0 +1,19 @@ +//По окончании загрузки документа вызывается функция +$(document).ready(function() { + var timerId = setInterval(show, 2000); + setTimeout(function() { + clearInterval(timerId); + }, 7000); +}) + +//При получении команды 'show' показывает ответы +chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { + if(request.command==="show") show(); +}); + +//Показать ответы +function show() { + $('*[class^="correct"]').show().removeClass("ng-hide").parent().show().removeClass("ng-hide"); + $("table.ng-hide").removeClass("ng-hide"); + $("section").css("user-select","initial"); +} \ No newline at end of file diff --git a/injected.js b/injected.js deleted file mode 100644 index 7fdd136..0000000 --- a/injected.js +++ /dev/null @@ -1,11 +0,0 @@ -function injected_main() { - var timerId = setInterval(show, 2000); - setTimeout(function() { - clearInterval(timerId); - }, 7000); -} -function show() { - $('*[class^="correct"]').show().removeClass("ng-hide").parent().show().removeClass("ng-hide"); - $("table.ng-hide").removeClass("ng-hide"); - $("section").css("user-select","initial"); -} diff --git a/manifest.json b/manifest.json index 49f1300..3223216 100644 --- a/manifest.json +++ b/manifest.json @@ -12,15 +12,18 @@ "browser_action": { "default_icon": "icon.png" }, + "background": { + "scripts": ["background.js"] + }, "content_scripts": [ { "matches": [ "*://www.cambridgelms.org/*" ], - "js": [ "jquery.js", "background.js" ], + "js": [ "jquery.js", "inject.js" ], "run_at": "document_end", "all_frames": true } ], - "web_accessible_resources": [ - "/injected.js" + "permissions": [ + "tabs" ] } \ No newline at end of file