Skip to content

Commit

Permalink
Only toggle editor on rules page
Browse files Browse the repository at this point in the history
  • Loading branch information
Ludonope committed Nov 21, 2021
1 parent 401ff32 commit 94e0140
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
42 changes: 37 additions & 5 deletions extension/src/content/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
if (shouldLaunchEditor(document.body)) {
const interval = setInterval(() => {
const app = document.getElementsByClassName('app')[0];
if (app) {
const rules = document.getElementsByClassName('rules');
if (app && rules.length > 0) {
clearInterval(interval);
prepareEditorLaunch(app);
prepareEditorLaunch(app, rules);
}
}, 100);
}
Expand All @@ -14,7 +15,7 @@ const req = document.createElement('script')
req.src = 'require.js'
document.body.appendChild(req)

function prepareEditorLaunch(app) {
function prepareEditorLaunch(app, rules) {
// Hide contents to avoid flickering while the rest of the script loads
// asynchronously.
// element.style.setProperty('display', 'none', '');
Expand All @@ -30,6 +31,16 @@ function prepareEditorLaunch(app) {
chrome.runtime.sendMessage({
action: 'show_page_action'
});

let rulesLen = rules.length;
setInterval(() => {
const rules = document.getElementsByClassName('rules');
if (rules.length != rulesLen) {
const app = document.getElementsByClassName('app')[0];
toggleEditor(app);
rulesLen = rules.length;
}
}, 100);
}

function shouldLaunchEditor(body) {
Expand All @@ -49,7 +60,7 @@ function listenToEditorMessages(element) {
// Always call mod.init before anything else
mod.init()
mod.onPlayerJoinGame('New Rule', (eventPlayer) => ({
mod.onPlayerJoinGame('Welcome new player', (eventPlayer) => ({
conditions: [],
actions: () => {
ui.ShowEventGameModeMessage(ui.Message("Welcome", eventPlayer))
Expand All @@ -58,6 +69,8 @@ mod.onPlayerJoinGame('New Rule', (eventPlayer) => ({
filename: 'main.ts'
};
message.source.postMessage(response, chrome.runtime.getURL(''));
} else if (message.data === 'toggleEditor') {
toggleEditor(app);
} else if (message.data.startsWith('run+')) {
console.log(message)
let source = `import * as __portal from '${chrome.runtime.getURL('../lib/portal-unleashed/dist/unleash.js')}'\n` +
Expand Down Expand Up @@ -130,4 +143,23 @@ function toggleEditor(app) {
iframe.setAttribute('style', 'border: 0px none; width: 100%; height: 100%; grid-row: 1;');
app.appendChild(iframe);
}
}
}

// (function(history) {
// var pushState = history.pushState;
// history.pushState = function(state) {
// if (typeof history.onpushstate == "function") {
// history.onpushstate({ state: state });
// }
// // ... whatever else you want to do
// // maybe call onhashchange e.handler
// return pushState.apply(history, arguments);
// };
// })(window.history);

// window.onpopstate = history.onpushstate = function(event) {
// console.log(event)
// };
browser.webNavigation.onHistoryStateUpdated.addListener((e) => {
console.log(e)
})
5 changes: 2 additions & 3 deletions extension/src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"content/content.js"
],
"matches": [
"<all_urls>"
"https://portal.battlefield.com/*"
],
"run_at": "document_end"
}],
Expand All @@ -30,8 +30,7 @@
"storage",
"webRequest",
"tabs",
"<all_urls>",
"https://y7mfvajmhh.execute-api.us-east-1.amazonaws.com/*"
"<all_urls>"
],
"icons": {
"128": "images/portal_unchained_logo.png"
Expand Down

0 comments on commit 94e0140

Please sign in to comment.